簡體   English   中英

在 tableView 中創建 tableView

[英]Creating tableView inside tableView

我創建了一個帶有原型單元格的 tableView。 在這些原型單元格中,每個原型單元格內都有另一個具有不同原型單元格的 tableView。 我已經很好地將這些鏈接在一起,但是我在修改最里面的原型單元格時遇到了麻煩。 這是為什么。 這是相關的代碼:

class ViewController: UIViewController, AVAudioRecorderDelegate, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!
 override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "outerCell") as! outerCell
//would obviously make some modification to cell here, like cell.title = "test" or something
            let cell2 = cell.commentTableView.dequeueReusableCell(withIdentifier: "innerCell") as! innerCell
            cell2.commentText.text = "sus"

        //NEED TO DIFFERENTIATE HERE ON HOW TO KNOW WHICH CELL TO RETURN
//e.g. NEED TO RETURN either cell1 or cell2, depending on the tableView

    }

我的 outerCell 代碼如下所示:

import UIKit

class outerCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var commentTableView: UITableView!


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        commentTableView.delegate = self
        commentTableView.dataSource = self

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "innerCell", for: indexPath) as! commentCell
        return cell
    }

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


}

看,主要問題是,這兩個表視圖都可以正常工作,但是,在第一段代碼中,如果我只是做類似的事情,

if tableView == self.tableView{
    return cell }
else ...

這行不通,因為 tableView 似乎總是 self.tableView。

如何修改我的代碼,以便在同一代碼塊中實際影響顯示在內部單元格和外部單元格中的文本?

另外,請注意,我知道,根據此處給出的示例,不需要這些嵌套單元格。 我只是簡化了這里的代碼,專注於重要的事情 - 我的實際代碼在內部和外部單元格中發生了很多事情。

謝謝,任何幫助將不勝感激。

您需要首先創建兩個不同的單元格類。 在外部類:

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! SearchPreferredJobTableViewCell
            cell.responseCreateBookingObj = { [unowned self] (returnObject) in
            DispatchQueue.main.async {
                    tableView.beginUpdates()
                    }
// do your logic

            DispatchQueue.main.async {
                        cell.contentView.layoutIfNeeded()
                        tableView.endUpdates()
                    } }

       return cell 
}

// 其他單元格類聲明變量

var responseCreateBookingObj : APIServiceSuccessCallback?

// 發送你想要發送的回調

guard let callBack = self.responseCreateBookingObj else{
        return
    }
    callBack(true as AnyObject)

// 也可以在用戶滾動時執行

tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath){
 DispatchQueue.main.async {
                    tableView.beginUpdates()
                    }

// 做你的邏輯

        DispatchQueue.main.async {
                    cell.contentView.layoutIfNeeded()
                    tableView.endUpdates()
                }

}

暫無
暫無

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

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