如何修改DidSelectRowAtIndexPath内的自定义单元格

时间:2016-07-24 09:23:54

标签: swift uitableview tableviewcell

自定义单元格类具有覆盖func layoutSubviews(),其中每个单元格的detailTextLabel被赋予标题“Jim”。在点击DidSelectRowAtIndexPath时,有没有办法永久地更改单元格的详细文本(以阻止单元格不断地制作细节Jim),让我们说“Bob”?

//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 */

}

2 个答案:

答案 0 :(得分:0)

很简单,就这样做:

var array = {name:'dua',email:'dua@gmail.com'};

$.post('index.php?route=module/newsletters/news', {
    data : JSON.stringify(array)
}, function(success) {
    console.log(success)
});

答案 1 :(得分:0)

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

UITableViewDelegate协议外,还要研究UITableViewDataSource协议。默认情况下,UITableViewController类符合这两个协议,并在.tableView属性中分配为每个协议(如果您反省其self.tableView.delegateself.tableView.datasource值,则会收到原始的UITableViewController)。如果手动创建自己的继承自UIViewController的tableview控制器类,则需要在tableView上分配这两个属性才能使其正常运行。