传递参数到自定义的UITableViewCell

3
我正在TableViewController中注册一个来自Nib的自定义单元格。
tableView.registerNib(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "Cell")

但是我的CustomCell有些IBAction方法需要访问tableView本身和fetchedResultsController。如何以一种清晰的方式将这些参数传递到CustomCell中?

在以下内容中设置这些内容:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) { 
   return tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 
}

这种方式不太干净,对我来说?


哦,你是对的,我太蠢了 :-( - NSCode
2个回答

3

这个单元格不应该知道关于表视图或fetchedResultsController的任何信息。一种做法是在cellForRowAtIndexPath中添加动作并将控制器作为目标。

另一种选择是在自定义单元格类中添加操作方法,并调用您在单元格类中创建的委托协议方法。在cellForRowAtIndexPath中,让控制器将自己设置为委托。


它是如何工作的?我什么也没听懂。 - Abhishek Mitra

0
您可以选择负责自定义单元格IBAction示例的类。
IBAction Func enterKeyButton (sender: AnyObject) {
    println(sender)
}

在tableview中实现该方法,这就是自定义单元格的原因。

func tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.dequeueReusableCellWithIdentifier ("Cell", forIndexPath: indexPath) as UITableViewCell

    cell.enterKeyButton ("hello")

    return cell
}

希望我能帮到你!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接