带有复选框的Tableview [英] Tableview with checkbox

查看:90
本文介绍了带有复选框的Tableview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将此代码用于带有复选框的tableview。

I am using this code for tableview with checkbox.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    cell.accessoryType = UITableViewCellAccessoryNone;

            cell.textLabel.text =@"a";
            int flag = (1 << indexPath.row);
            if (_checkboxSelections & flag) 
            {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }


    return cell;
}



#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
               _checkboxSelections ^= (1 << indexPath.row);
    [tableView reloadData];
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

当我单击某些按钮

推荐答案

您可以使用以下方法访问按钮操作上的tableView单元格。并且您可以使用 if(cell.accessoryType == UITableViewCellAccessoryCheckmark)条件来检查选择,因为您正在为选定的单元格设置 UITableViewCellAccessoryCheckmark

You can use the following method to access the tableView cells on a button's action. And you can check for the selection using if (cell.accessoryType == UITableViewCellAccessoryCheckmark) condition because you are setting UITableViewCellAccessoryCheckmark for the selected cells.

- (void)onButtonClick {

    int numberOfSections = [tableView numberOfSections];

    for (int section = 0; section < numberOfSections; section++) {

        int numberOfRows = [tableView numberOfRowsInSection:section];

        for (int row = 0; row < numberOfRows; row++) {

            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

            if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {

                // Cell is selected

            } else {

                // Cell is not selected
            }
        }
    }
}

这篇关于带有复选框的Tableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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