2009-10-16 36 views
2

我需要使用复选框单元格的帮助。我目前将该对象添加到tableview。它看起来不错,直到我试图建立和运行程序,我无法检查复选框。我目前使用的tableview显示项目运行时每个项目的复选框,所以我可以有多个选择。表格视图中的复选框单元格:用户无法检查它

我是新来的xcode和我一直卡住一个星期这个问题。我试过谷歌,但仍然没有运气。

任何片段,答案或解释非常感谢。

回答

5

首先我们需要编辑这个方法:- (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,复选标记不会显示出来,直到它熄灭屏幕和重新出现。由于单元格被重用的方式,每次需要明确设置accessoryType。如果仅在选中单元格时设置样式,则可能不必检查的其他单元格在滚动时会出现复选标记。希望这给你一个关于如何使用复选标记的总体思路。

相关问题