如何以编程方式选择uitableview行

S.J*_*S.J 2 cocoa-touch objective-c uitableview ios

我想尝试在uitableview中选择所有功能的电子邮件,在同一个按钮点击用户可以勾选或删除所有复选标记,此外用户还可以选择/取消选择行(在didSelectRowAtIndexPath上).我试着做但不能正常工作,这是我的代码.

- (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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Vik*_*ica 8

我写了一个适合您需求的示例代码.

基本上是

-(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];
   }
}
Run Code Online (Sandbox Code Playgroud)

在行动:

在此输入图像描述

你在打电话

[[self.iTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] setAccessoryType:UITableViewCellAccessoryNone];
Run Code Online (Sandbox Code Playgroud)

对于tableView中每个部分的每一行.如果你有很多行,这是低效的,因为它将处理不在屏幕上的行.但这不是必需的.只需将每个选定的索引路径放入一个数组,并告诉tableView重新加载.这将重新加载可见单元格,并且由于单元格的实现,-tableView:cellForRowAtIndexPath:新行将被正确地重新配置.


das*_*ght 5

设置附件视图需要在方法内部进行tableView:cellForRowAtIndexPath:.如果你想改变来自外部的配件,外面的方法需要改变模型首先表明,复选标记必须放置在特定的细胞中,然后调用reloadDataUITableView.

存储哪些单元格的一种方法是NSIndexSet对象数组- 每个部分一个索引集.在下面的示例中,我显示了单个部分的代码,但您应该了解如何使多个部分工作.

// This variable needs to be declared in a place where your data source can get it
NSMutableIndexSet *selected;

// You need to initialize it in the designated initializer, like this:
selected = [[NSMutableIndexSet alloc] init];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    if ([selected containsIndex:indexPath.row]) {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    } else {
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
    // Do the rest of your code
    return cell;
}
Run Code Online (Sandbox Code Playgroud)

现在,在您要设置选定或未选择行的代码中,您只需要调用[selected addIndex:rowToSelect][selected removeIndex:rowToUnselect]调用表格reloadData.