xcode 带有复选标记的 UITableView 单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5702417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:51:01  来源:igfitidea点击:

UITableView Cell with CheckMarks

iphonexcodeuitableview

提问by Ibz

I have got an iphone project that has a UITableViewwhich is populated from Core Data..

我有一个 iphone 项目,它有一个UITableView从核心数据填充的..

When a cell is selected, the user is directed to a new view controller (see code line below) which shows additional information about that cell entry (from core data)

当一个单元格被选中时,用户被引导到一个新的视图控制器(见下面的代码行),它显示了关于该单元格条目的附加信息(来自核心数据)

[self presentModalViewController:noteViewController animated:YES]

What i want to do is to be able to set some sort of checkmark on either side of each cell to represent whether the task is complete of incomplete..

我想要做的是能够在每个单元格的两侧设置某种复选标记来表示任务是否完成或未完成。

I have read that i could do this using:

我已经读到我可以使用以下方法做到这一点:

UITableViewCellAccessoryCheckmark

but i am not sure how to implement is as currently

但我不确定目前如何实施

didSelectRowAtIndexPath:

is being used to present the modal view controller..

用于呈现模态视图控制器..

So essentially i want to know if there is a method for changing the state of a cell's accessory from checkmark to nothing (and vice versa) independently.. in the sense that if the actual checkmark is touch.. it shows it.. and if it is touched again, it hides it..

所以基本上我想知道是否有一种方法可以独立地将单元格附件的状态从复选标记更改为无(反之亦然)。它再次被触摸,它隐藏它..

Any help would be greatly appreciated!

任何帮助将不胜感激!

回答by MarkPowell

You will need to track selected rows externally from Cell presentation. This means, your model (that you used to build the cells in the first place) will need to track some sort of boolean. Once you have that you set accessoryTypeto the proper type.

您将需要从单元格演示文稿外部跟踪选定的行。这意味着,您的模型(您首先用于构建单元格)将需要跟踪某种布尔值。一旦你有你设置accessoryType为正确的类型。

i.e.

IE

if ([[data objectAtIndex:indexPath.row] isSelected]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}

This would be some of the logic in your:

这将是您的一些逻辑:

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

回答by visakh7

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

UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

  if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
  {
    selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
  }
  else 
  if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
  {
   selectedCell.accessoryType = UITableViewCellAccessoryNone;
  }
   //Do something
  }

Hope this helps

希望这可以帮助

UPDATE

更新

This tutorialwill give you a basic info on custom table view cell. Another Reference.

教程将为您提供有关自定义表格视图单元格的基本信息。另一个参考

I think thiswill give you the solution.

我认为将为您提供解决方案。