UITableView SelectAll - willDeselectRowAtIndexPath alternative
I built a multi-selection table, I did that using willSelectRowAtIndexPath and willDeselectRowAtIndexPath. It is working fine. Now I want to programatically selectALL or Select None. Is there a way to call
[tableView selectRowAtIndexPath: [NSIndexPath indexPathForRow:i inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
without having the currently selected row unselected? I read in documentation, this method does not call delegate's willDeselectRowAtIndexPath.
Here's all code to allow multiple selection. The problem is now that I cannot select programmatically without deselecting previous.
- (NSIndexPath *)tableView:(UITableView *)aTableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary* friend = [friendsData objectAtIndex:[indexPath row]];
if (![selectedIndexes containsObject:friend]) {
[selectedIndexes addObject:friend];
}
else {
[selectedIndexes removeObject:friend];
[tab开发者_JAVA百科leView deselectRowAtIndexPath:indexPath animated:NO];
return nil;
}
return indexPath;
}
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary* friend = [friendsData objectAtIndex:[indexPath row]];
if (![selectedIndexes containsObject:friend]) {
return indexPath;
}
// do not select so we can have multiple select
return nil;
}
You'll have to change your data source to reflect the select all or select none, then refresh the rows in question to update them to their new status.
I can't post any specific code because it really depends on how you've set up your multi-selection.
Edit
It seems that you can select all by setting selectedIndexes
to friendsData
and select none by clearing the contents of selectedIndexes
.
Select All:
[selectedIndexes setArray: friendsData];
Select None:
[selectedIndexes removeAllObjects];
Then you can reload your table view. If you want it to be instant, execute [tableView reloadData]
otherwise you can use the reload methods with animation.
Here is method to traverse a table, it works well.
Select all the cells in UITableView
for (int i = 0; i < [ptableView numberOfSections]; i++) {
for (int j = 0; j < [ptableView numberOfRowsInSection:i]; j++) {
NSUInteger ints[2] = {i,j};
NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:ints length:2];
UITableViewCell *cell = [ptableView cellForRowAtIndexPath:indexPath];
//Here is your code
}
}
精彩评论