2016-07-24 34 views
0

自定义单元类具有override func layoutSubviews(),其中每个单元格的detailTextLabel都被赋予标题“Jim”。单击DidSelectRowAtIndexPath后,是否有方法永久更改单元格的细节文本(以阻止单元格不断细化Jim),让我们说“Bob”?如何修改DidSelectRowAtIndexPath中的自定义单元格

//This function is in the custom cell UserCell 
 

 
class CustomCell: UITableViewCell { 
 
override func layoutSubviews() { 
 
     super.layoutSubviews() 
 
     
 

 
     
 
      detailTextLabel?.text = "Jim" 
 
    } 
 

 
///........ 
 
} 
 

 

 
//In the viewController with the tableView outlet 
 
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 
 

 
//..... 
 

 

 
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
 
     let cell = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath) as! CustomCell 
 
     
 
     //...... 
 
     
 
     
 
     return cell 
 
    } 
 

 

 
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
 

 
/* Code I need to convert the detailTextLabel.text to equal "Bob" upon clicking on certain cell */ 
 

 
}

回答

0

很简单,像这样做:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

detailTextLabel?.text = "Bob" 

} 
+0

谢谢你的回答,尽管我已经尝试过了,它仍然是Jim,重写func layoutSubviews在tableView重新载入时被调用,因此在我将它声明为我的didSelectRow中的Bob后将它放回到Jim。 ... – slimboy

0

本身不应该被用来保持状态的任何数据,而只是将其显示在单元格。在控制器上创建一个可变数组属性以保存下层数据(字符串)。通过读取此数组来设置新单元格的文本属性,并在tableView:didSelectRowAtIndexPath:中将数组中"Bob"索引处的值更改为"Jim"。每当tableView重新加载时,它现在将从dataSource中读取更新后的值。

除了UITableViewDelegate协议还研究了UITableViewDataSource协议。默认情况下,UITableViewController类符合这两种协议,并分配为属性的.tableView属性(如果您反思其self.tableView.delegateself.tableView.datasource值,您将收到原始UITableViewController)。如果您手动创建了自己的从UIViewController继承的tableview控制器类,那么您将需要在tableView上分配这两个属性,以使其正常工作。

相关问题