如何在选择单元格时设置accessoryType

时间:2011-09-08 03:59:16

标签: iphone ios accessorytype

我一直试图弄清楚如何在选择单元格时将accessoryType设置为UITableViewCellAccessoryCheckmark但是我找不到合适的例子。

如果你知道如何做到这一点,或者你可以告诉我一个很好的教程。

5 个答案:

答案 0 :(得分:10)

要将用户限制为只有一个选项,即只创建一个选项的独占列表,您可以按照以下步骤操作;

首先,在.h文件中声明一个全局索引路径,以跟踪已选择的单元格 - >

NSIndexPath *oldIndexPath;

创建单元格时,请务必将附件类型设置为none,以便在看到表格时默认不选择单元格;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CallIdentifier"];
        cell.accessoryType = UITableViewCellAccessoryNone;      
    }
    return cell; 
}

最后,在didSelectRowAtIndexPath委托方法中,添加以下代码,该代码将从已选择的单元格中删除复选标记,并为新选择的单元格添加复选标记。

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

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (oldIndexPath==nil) { // No selection made yet
        oldIndexPath=indexPath;
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }
    else {
        UITableViewCell *formerSelectedcell = [tableView cellForRowAtIndexPath:oldIndexPath]; // finding the already selected cell
        [formerSelectedcell setAccessoryType:UITableViewCellAccessoryNone];

        [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; // 'select' the new cell
        oldIndexPath=indexPath;
    }   
}

希望这会成功! :)

答案 1 :(得分:2)

这样的事情可能有用:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self tableView:myTableView cellForRowAtIndexPath:indexPath];
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}

要回答下面的评论,只需按照以下方法推送viewController:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self tableView:myTableView cellForRowAtIndexPath:indexPath];
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];

    // Then push a new view
    iPhoneCustomViewController *myVC = [[iPhoneCustomViewController alloc] initWithNibName:@"iPhoneCustomViewController" bundle:nil];
    [self.navigationController pushViewController:myVC animated:YES];
    [myVC release];

    // And deselect the row (if desired)
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

答案 2 :(得分:1)

你知道吗:

1。)UITableView会跟踪已选择行的索引路径吗?它位于一个名为indexPathsForSelectedRows

的数组中

2.。)UITableView有一个标志,你可以设置它来进行单选或多选。您可以通过调用setter setAllowsMultipleSelection:(BOOL)来更改它。

因此,假设表已设置为单选,我们可以在tableView中执行以下操作:CellForRowAtIndexPath方法......

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *simpleTableIdentifier = @"CellIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

[[cell textLabel] setText:@"Some Text"];

NSArray *selectedIndexPaths = [tableView indexPathsForSelectedRows];

if ([selectedIndexPaths containsObject:indexPath]) {
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}else{
    [cell setAccessoryType:UITableViewCellAccessoryNone];
}

return cell;}

当选择单元格时,CellForRowAtIndexPath的这种实现将为您提供一个没有灰色背景的干净复选标记。您需要在didSelectRowAtIndexPath委托方法中设置复选标记,以确保单元格在被选中时获得复选标记。

无需创建单独的ivars或其他任何内容来跟踪未选择的内容。它完全包含在苹果公司想要的UITableView中。

答案 3 :(得分:0)

UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
 newCell.accessoryType=UITableViewCellAccessoryCheckmark;

在didSelectRowAtIndexPath委托方法

中实现此功能

答案 4 :(得分:0)

来自docs

  

委托处理此方法中的选择。其中一件事   可以做的是专门分配复选标记图像   (UITableViewCellAccessoryCheckmark)到一个部分中的一行   (电台列表样式)。编辑属性时不调用此方法   表的设置为YES(即表视图正在编辑中)   模式)。请参阅“表视图编程指南”中的“管理选择”   iOS提供与此相关的更多信息(和代码示例)   方法

以下是一个例子:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]
   cell.UITableViewAccessoryType = UITableViewCellAccessoryCheckmark;
}