Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

ios - How do you select uitableview rows programmatically

I am trying to achive a email like select all functionality in uitableview where on same button tap user can checkmark or remove all checkmark and additionally user can also select/deselect rows(on didSelectRowAtIndexPath). I tried to do but its not working properly, here is my code.

- (IBAction)selectAll:(id)sender
{
    if(myBoolean)
    {
        for (NSInteger s = 0; s < self.iTable.numberOfSections; s++)
        {
            for (NSInteger r = 0; r < [self.iTable numberOfRowsInSection:s]; r++)
            {
                [[self.iTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] setAccessoryType:UITableViewCellAccessoryNone];
            }
        }

        myBoolean = NO;

        [_selectUnselectButton setTitle:@"Select all Friends" forState:UIControlStateNormal];
    }
    else
    {
        for (NSInteger s = 0; s < self.iTable.numberOfSections; s++)
        {
            for (NSInteger r = 0; r < [self.iTable numberOfRowsInSection:s]; r++)
            {
                [[self.iTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] setAccessoryType:UITableViewCellAccessoryCheckmark];
                NSLog(@"%d-%d",s,r);
            }
        }

        myBoolean = YES;

        [_selectUnselectButton setTitle:@"Unselect all Friends" forState:UIControlStateNormal];
    }
}


-(void)tableView:(UITableView *)tableView_ didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView_ cellForRowAtIndexPath:indexPath];

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I wrote a sample code that I adapted to your needs.

Basically it is

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *unifiedID = @"aCellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:unifiedID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:unifiedID];
    }

    cell.textLabel.text = [self.states objectAtIndex:indexPath.row];

    //if the indexPath was found among the selected ones, set the checkmark on the cell
    cell.accessoryType = ([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *state = [self.states objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]){
        [self.selectedCells removeObject:indexPath];
        [self.selecedStates removeObject:state];
        cell.accessoryType = UITableViewCellAccessoryNone;
    } else {
        [self.selectedCells addObject:indexPath];
        [self.selecedStates addObject:state];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }

    NSLog(@"%@", self.selecedStates);
}


-(BOOL)isRowSelectedOnTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath
{
    return ([self.selectedCells containsObject:indexPath]) ? YES : NO;
}


- (IBAction)selectAll:(id)sender {

    [self.selecedStates removeAllObjects];
    [self.selectedCells removeAllObjects];
    NSUInteger numberOfSections = [self.tableView numberOfSections];
    for (NSUInteger s = 0; s < numberOfSections; ++s) {
        NSUInteger numberOfRowsInSection = [self.tableView numberOfRowsInSection:s];
        for (NSUInteger r = 0; r < numberOfRowsInSection; ++r) {
            NSIndexPath *idxPath = [NSIndexPath indexPathForRow:r inSection:s];
            [self.selectedCells addObject:idxPath];
            [self.selecedStates addObject:self.states[idxPath.row]];
        }
    }
    [self.tableView reloadData];
}



- (IBAction)deselectAll:(id)sender {
    [self.selecedStates removeAllObjects];
    [self.selectedCells removeAllObjects];
    [self.tableView reloadData];
}


- (IBAction)toggleAll:(id)sender {
    if ([self.states count] == [self.selecedStates count]) {
        [sender setTitle:@"select all"];
        [self deselectAll:sender];
    } else {
        [sender setTitle:@"deselect all"];
        [self selectAll:sender];
   }
}

in action:

enter image description here

You are calling

[[self.iTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] setAccessoryType:UITableViewCellAccessoryNone];

for every row in every section within the tableView. if you have many row, this is ver inefficient, as it will deal with rows not on the screen. But this is not needed. just put every selected index path into an array and tell the tableView to reload. This will reload the visible cells and due to the implementation of -tableView:cellForRowAtIndexPath: cells wfor new rows will be correctly re-conigured.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...