繁体   English   中英

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

[英]how to set accessoryType when cell has been selected

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

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

要将用户限制为仅一个选择,意味着仅创建一个选项的独占列表,您可以按照以下步骤操作;

首先,在.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;
    }   
}

希望这会成功! :)

这样的事情可能有用:

- (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];
}

你知道吗:

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或其他任何内容来跟踪已选择或未选择的内容。 它完全包含在Apple想要的UITableView中。

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

在didSelectRowAtIndexPath委托方法中实现它

来自文档

委托处理此方法中的选择。 它可以做的一件事就是将复选标记图像(UITableViewCellAccessoryCheckmark)专门分配给一个部分中的一行(无线电列表样式)。 当表的编辑属性设置为YES(即表视图处于编辑模式)时,不会调用此方法。 有关此方法的详细信息(和代码示例),请参阅“表视图编程指南(适用于iOS)”中的“管理选择”。

这是一个例子:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM