【问题标题】:How to make Checkmark (one at a time) selected in UITableViewCell?如何在 UITableViewCell 中选中复选标记(一次一个)?
【发布时间】:2016-04-07 23:25:17
【问题描述】:

嘿嘿, 我想制作一个表格视图,当我点击单元格时,它将转到下一个视图,我可以选择一个带有复选标记的特定选项(一次只有一个)以及所选选项中的任何文本,它应该显示在上一个视图中(哪个也是一个tableview)

请帮帮我。

当使用下面的方法时,它会选择我正在点击的所有选项,但我希望只有一个单元格会有我点击过的复选标记。

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

提前致谢

【问题讨论】:

  • 请更具体一点,您是从一个表格视图导航到另一个表格视图,您需要在哪个表格视图中打勾?
  • 我相信正确的类型转换将解决您面临的问题。使用: ((UITableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath]).accessoryType = UITableViewCellAccessoryCheckmark;

标签: ios uitableview


【解决方案1】:

我在 Xcode 中编写了一个示例项目,您可能会觉得它很有用。

CheckedTableView.

这样做的方法是更新数据源,然后重新加载表。试图直接改变 tableview 的状态是行不通的。

【讨论】:

    【解决方案2】:

    您可以在NSIndexPath 类型的类属性中跟踪当前选定的项目。首先将此属性初始化为nil 或之前选择的任何值。您还可以修复您的 cellForRowAtIndexPath 以将单元格的 indexPath 与“当前选定的”索引路径属性进行比较,并在它们匹配时添加复选标记。现在你在你的didSelectRowAtIndexPath 中要做的就是这样(代码没有用编译器检查,可能包含小错误):

    NSIndexPath *oldIndexPath = self.selectedIndexPath; // retain this if not using ARC!
    self.selectedIndexPath = indexPath; // passed in from didSelectRowAtIndexPath
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects: oldIndexPath, self.selectedIndexPath, nil], UITableViewRowAnimationNone];
    // release oldIndexpath if noct using ARC
    

    由于cellForRowAtIndexPath 应该根据self.selectedIndexPath 创建带有或不带有复选标记的单元格,这将相应地更新单元格(如果应该已经选择了某些内容,还会在开始时创建正确的视图)。

    奖励:您有self.selectedIndexPath 可用于确定应返回“调用”视图的内容,即选择哪一行。

    【讨论】:

      【解决方案3】:

      让我们使用 A 和 B 来表示您的第一个表视图和第二个表视图。并假设您可以从 A 导航到 B。

      你将需要这些东西:

      1. 您的 uitableview 单元格表示的自定义对象,或 NSIndexPath,甚至是用作 A 索引的整数。在 A 中设置值 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{},因为稍后在 B 中进行选择后,您想知道将文本附加到的位置。

      2. 在 B 中显示复选标记 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{},设置为当前选中的那个。你必须想办法记录信息,这应该来自你从 A 到 B 的导航过程。我通常做的是创建 B 的自定义 init 函数,从 A 调用。你也可以制作当前选定的对象B 中的属性,并在从 A 导航之前在初始化 B 时从 A 进行设置。无论您做什么,您都可能无论如何都想创建该属性。

      3. 让 A 成为 B 的代表,实现一个函数 -(void)getSelectionFromB:(SomeClass *)obj{} 并在 B 的内部 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{},调用[delegate getSelectionFromB:selectedObject],应该没问题。

      4. 在 A 的 -(void)getSelectionFromB:(SomeClass *)obj{} 实现中,要么在 tableview 中找到单元格并更改它,要么更改 NSArray 或用于填充 table view 的任何内容,并且然后重新加载数据。

      希望这会有所帮助。

      【讨论】:

        【解决方案4】:

        只需将tableView:didSelectRowAtIndexPathtableView:cellForRowAtIndexPath方法这样一起使用即可:

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

        而且,如果您不想让您的用户看到仍突出显示的单元格的不良景象,只需在右大括号之前添加这行代码:

        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        

        附:我制作动画只是为了让它看起来不错。

        参考资料:

        这里的一半:iPhone :UITableView CellAccessory Checkmark

        还有一半来自这里:How to deselect a selected UITableView cell?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-02-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多