3

I have a UITableView with custom cells. Usually when the user taps the cell, it triggers this function:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     //Segue to another view
}

If in that view they mark the cell as finished, when they return the code will add the standard check accessory.

When the cell isn't finished, I'd like an empty check that can be tapped (I know I can add a custom image accessory), with the tap allowing the user to skip the segue and quickly mark the cell as finished. But I can't seem to make both work:

  1. Tapping on the main body of the cell should call didSelectRowAtIndexPath
  2. Tapping on the accessory view (empty check mark) should call a different set of code.

I've tried accessoryButtonTappedForRowWithIndexPath but it doesn't seem to even get called.

func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) { 
     //Shortcut code to change the cell to "finished"
}

Is it possible to have clicking on the main body trigger one set of code and clicking on the accessory view trigger another? If so, how?

4

3 回答 3

1

您应该将 UIButton 添加到您的自定义 UITableViewCell。然后,您可以为该按钮添加一个名为 pressFinished 的目标,方法是说类似cell.button.addTarget(self, action: "finishedPress:", forControlEvents: .TouchUpInside) 或类似的内容,然后在 pressFinished 中您可以说以下内容:

func pressedFinished(sender:UIButton)
{
   let location = self.tableView.convertPoint(sender.bounds.origin, fromView: sender)
   let indexPath = self.tableView.indexPathForRowAtPoint(location)
   //update your model to reflect task at indexPath is finished and reloadData
}

使用标签通常不是一个好习惯,因为它们没有内在含义。此外,映射到 indexPath.row 的标签仅在表有一个部分时才有效。

另一种选择可能是使用 UITableViewRowAction:

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath:      NSIndexPath) -> [AnyObject]? {
  let rowAction : UITableViewRowAction
  if model[indexPath].finished
  {
      rowAction = UITableViewRowAction(style: .Normal, title: "Mark as Unfinished", handler: {(rowAction:UITableViewRowAction,indexPath:NSIndexPath)->() in
      //mark as unfinished in model and reload cell
      })
 }else{
      rowAction = UITableViewRowAction(style: .Normal, title: "Mark as Finished", handler: {(rowAction:UITableViewRowAction,indexPath:NSIndexPath)->() in
      //mark as finished in model and reload cell
      })
  }
  return [rowAction]
}
于 2016-01-21T15:24:37.870 回答
1

它是一个 Objective-C 解决方案。当您添加自己的accessoryButton时,不会调用tableviewDelegate“accessoryButtonTappedForRowWithIndexPath”的方法。您应该做的是,为该方法创建一个UIButtonand addTarget,然后将其添加为accessoryButtonon tableViewCell。还将tag值设置为按钮,index path.row以便您了解单击了哪一行。

于 2016-01-21T15:06:24.380 回答
0

我选择了我认为给出了彻底答案的答案,这对我有所帮助。但是,我稍微偏离了这个答案,也想分享一下:

使附件视图出现/消失

我将几个列表加载到同一个故事板表中。有时列表应该显示指标,有时不需要附件。为此,我在情节提要中添加了一个按钮,垂直居中并在右侧容器视图中尾随 = 0。然后我给它一个宽度 0 并在我的代码中给这个约束一个 IBOutlet。当我想要配件时,我只是给它一个宽度。为了让它消失,我将它的宽度设置回 0。

配件和核心数据

配件有点让人头疼,因为如果用户勾选某些内容然后关闭应用程序,他们希望它会被记住。因此,对于每次更改,您都需要将单元格状态保存在 CoreData 中。我添加了一个名为“state”的属性,并在配件应该显示为已填充时给它一个“selected”值。

这也意味着我在检索列表时必须按该属性进行排序。如果您已经有一个排序描述符,那么您现在需要几个。

于 2016-02-01T12:15:13.377 回答