表视图中的复选框单元格:用户无法检查它 [英] Checkbox cell in a table view: User can't check it

查看:81
本文介绍了表视图中的复选框单元格:用户无法检查它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助使用checkbox cell。我目前将对象添加到tableview。它看起来确定,直到我尝试构建和运行程序,我不能选中该复选框。我目前使用一个tableview,显示项目运行时,每个项目的复选框,所以我可以有多个选择。

I need help in using checkbox cell. I currently added the object to tableview. It looks ok until i tried building and running the program where I cannot check the checkbox. I am currently using a tableview which displays items runtime with a checkbox for each item so i can have multiple selections.

我是新的xcode,周有这个问题。我尝试google但仍然没有运气。

I am new to xcode and I have been stuck for a week with this problem. i tried google but still no luck.

任何代码段,答案或解释都非常感谢。

Any snippets, answers, or explanations is very much appreciated.

推荐答案

首先我们需要编辑这个方法: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 。假设您生成了一个基于导航的应用程序,这个方法应该已经存在,只有注释掉。我不知道你的实现的确切细节,但你必须跟踪tableView中每个单元格的复选框状态。例如,如果您有一个BOOL数组,以下代码将工作:

First we need to edit this method: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath. Assuming you generated a Navigation-based application, this method should already be there, only commented out. I don't know the exact details of your implementation, but you somehow have to keep track of the checkbox state for each cell in the tableView. For example, if you had a BOOL array, the following code would work:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

 if (checkboxArray[indexPath.row])
  checkboxArray[indexPath.row] = NO;
 else 
  checkboxArray[indexPath.row] = YES;

 [self.tableView reloadData];
}



现在我们知道单元格旁边是否有复选标记下一步是修改单元格的显示方式。 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 处理每个单元格的绘图。除了前面的例子,这是如何显示复选框:

Now that we know what cells need to have a checkmark next to them, the next step is to modify how the cell is displayed. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath handles the drawing of each cell. Building off the previous example, this is how you would display the 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];
    }

 if (checkboxArray[indexPath.row]) {
  cell.accessoryType = UITableViewCellAccessoryCheckmark;
 }
 else
  cell.accessoryType = UITableViewCellAccessoryNone;

 // Configure the cell.

    return cell;
}

如果我们不调用reloadData,复选标记将不会显示离屏并重新出现。您需要每次显式地设置accessoryType,因为单元格被重用的方式。如果您仅在选中单元格时设置样式,则当您滚动时,可能不一定要选中的其他单元格将具有复选标记。希望这给你一个关于如何使用复选标记的一般想法。

If we don't call reloadData, the checkmark will not show up until it goes off-screen and reappears. You need to explicitly set the accessoryType each time because of the way cells are reused. If you set the style only when a cell is checked, other cells that may not necessarily be checked will have a checkmark when you go to scroll. Hopefully this gives you a general idea on how to use checkmarks.

这篇关于表视图中的复选框单元格:用户无法检查它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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