簡體   English   中英

TableView中可擴展且可滑動的單元格

[英]Expanded and Swipe-able cell in TableView

我有TableView ,用戶可以在其中單擊並展開每個單元格(UI: 單擊 )。 這是該代碼(也是我創建了.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: 單擊 滑動它們是否可以不展開Cell?

對不起,但是問題是我沒有為單元的擴展版本設置約束。 這就是為什么此標簽隨滑動菜單一起移動的原因。 現在可以了。 感謝您的所有答復!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM