如何以编程方式选择uitableview行 [英] How do you select uitableview rows programmatically

查看:115
本文介绍了如何以编程方式选择uitableview行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
    }
}


推荐答案

附件视图需要在 tableView:cellForRowAtIndexPath:方法内部 当您想从外部更改附件时,外部方法需要首先更改模型,以指示检查标记必须放置在某些单元格中,然后在 reloadData UITableView

Setting the accessory view needs to happen inside the tableView:cellForRowAtIndexPath: method. When you want to change the accessories from outside, the outside method needs to change the model first to indicate that check marks must be placed in certain cells, and then call reloadData on the UITableView.

一个存储被检查单元格的方法是 NSIndexSet objects - 每个部分一个索引集。在下面的示例中,我将显示单个部分的代码,但您应该了解如何使多个部分工作。

One way to store what cells are checked is an array of NSIndexSet objects - one index set per section. In the example below I show code for a single section, but you should get an idea of how to make multiple sections work.

// 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;
}

现在,在要设置所选行或未选择行的代码中,调用 [selected addIndex:rowToSelect] [selected removeIndex:rowToUnselect] c> reloadData 。

Now in the code where you want to set rows selected or unselected you just need to call [selected addIndex:rowToSelect] or [selected removeIndex:rowToUnselect], and call your table's reloadData.

这篇关于如何以编程方式选择uitableview行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆