在TableView中更新一个单元格

时间:2016-12-27 04:17:09

标签: ios swift uitableview

我希望当我触摸按钮时,它会导致标签扩展。

我尝试使用numberOfLines并且它有效,但在滚动表格视图时,标签和其他单元格也会扩展:(

如何只更新一个单元格?

我使用table.beginUpdates但不是答案,因为当向下滚动时,其他单元格也会被更改。

这是我的代码,例如:

extension ViewController: UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(indexPath: indexPath) as     FoodTableViewCell
        cell.title.text = "adshflakdsjfhalkdsjfhladksjfhadlkfhadlkfhaldkfhdlkfhadjfhaldfhaldfhaldskfhdslkhflakdshfladksfhladfhladfhal"
        cell.delegate = self
        return cell
    }
}

extension ViewController: CellDelegate {

   func action(cell: FoodTableViewCell) {
       cell.title.numberOfLines = 0
   }
}

我想要这个:

enter image description here

1 个答案:

答案 0 :(得分:0)

我解决了。我在模型中设置了一个标志并用它来检查