简体   繁体   English

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

[英]Expanded and Swipe-able cell in TableView

I have TableView , where users can click and expand each cell (UI: click ). 我有TableView ,用户可以在其中单击并展开每个单元格(UI: 单击 )。 This is the code for that (also I created .xib file to layout this cell): 这是该代码(也是我创建了.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
    }
}

Also, all Cells are swipe-able. 此外,所有单元格均可滑动。 This is the code: 这是代码:

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
    }

The problem: 问题:

When I swipe Cell, it always expends in a half, this is my UI after swiping: click . 滑动Cell时,它总是消耗一半,这是滑动后的UI: 单击 Is it possible not to expand Cell if I swipe them? 滑动它们是否可以不展开Cell?

Excuse me, but the problem was that I haven't set constraints for expanded version of the cell. 对不起,但是问题是我没有为单元的扩展版本设置约束。 That's why this labels were moving with swipe menu. 这就是为什么此标签随滑动菜单一起移动的原因。 Now it works. 现在可以了。 Thank you for all your replies! 感谢您的所有答复!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM