【问题标题】:Checkbox cell in a table view: User can't check it表格视图中的复选框单元格:用户无法选中
【发布时间】:2009-10-16 07:40:00
【问题描述】:

我在使用复选框单元格时需要帮助。我目前将该对象添加到 tableview。在我尝试构建和运行无法选中复选框的程序之前,它看起来还可以。我目前正在使用一个 tableview,它显示项目运行时,每个项目都有一个复选框,所以我可以有多个选择。

我是 xcode 的新手,我被这个问题困扰了一周。我尝试了谷歌,但仍然没有运气。

非常感谢任何 sn-ps、答案或解释。

【问题讨论】:

    标签: iphone cocoa-touch uitableview uikit checkbox


    【解决方案1】:

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

    - (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 处理每个单元格的绘制。以前面的示例为基础,这是显示复选框的方式:

    - (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,复选标记将不会显示,直到它离开屏幕并重新出现。由于重用单元格的方式,您每次都需要显式设置附件类型。如果仅在选中某个单元格时设置样式,则可能未选中的其他单元格将在您滚动时带有选中标记。希望这能让您大致了解如何使用复选标记。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-04
      • 1970-01-01
      • 2012-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多