TableView中的扩展和可滑动单元格

时间:2017-08-10 10:15:02

标签: ios swift uitableview

我有TableView,用户可以点击并展开每个单元格(UI:click)。这是代码(我也创建了.xib文件来布局这个单元格):

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating  {
 var selectedIndex: IndexPath?
    var isExpended = false

    func didExpandCell() {
        self.isExpended = !isExpended
        self.tableView.reloadRows(at: [selectedIndex!], with: .automatic)

    }
    @IBOutlet weak var tableView: UITableView!
...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "wordCell", for: indexPath) as! CellController
        if searchController.isActive {
            cell.word.text = filtered[indexPath.row].word
            cell.translation.text = filtered[indexPath.row].translation
            cell.date.text = filtered[indexPath.row].fullDate
            cell.exOne.text = filtered[indexPath.row].exOne
            cell.exTwo.text = filtered[indexPath.row].exTwo
        } else {
            cell.word.text = words[indexPath.row].word
            cell.translation.text = words[indexPath.row].translation
            cell.date.text = words[indexPath.row].fullDate
            cell.exOne.text = words[indexPath.row].exOne
            cell.exTwo.text = words[indexPath.row].exTwo
        }
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.selectedIndex = indexPath
        self.didExpandCell()
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if isExpended && self.selectedIndex == indexPath {
            return 180
        }
        return 70
    }
}

此外,所有单元格都可以滑动。这是代码:

func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {
        let edit = UITableViewRowAction(style: .normal, title: "Edit") { action, index in
            print("Edit")
        }
        edit.backgroundColor = .blue

        let delete = UITableViewRowAction(style: .normal, title: "Delete") { action, index in
            print("Delete")
        }
        delete.backgroundColor = .red

        return [delete, edit]
    }

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

问题:

当我滑动Cell时,它总是花费一半,这是我刷完后的UI:click。如果我刷它们,是否有可能不扩展Cell?

1 个答案:

答案 0 :(得分:0)

不好意思,但问题是我没有为扩展版本的单元格设置约束。这就是为什么这些标签随着滑动菜单移动的原因。现在它有效。 感谢您的所有回复!

相关问题