在 tableView 中创建 tableView [英] Creating tableView inside tableView

查看:23
本文介绍了在 tableView 中创建 tableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个带有原型单元格的 tableView.在这些原型单元格中,每个原型单元格内都有另一个具有不同原型单元格的 tableView.我已经很好地将这些链接在一起,但是我在修改最里面的原型单元格时遇到了麻烦.这是为什么.相关代码如下:

I've created a tableView with prototype cells. Inside each of these prototype cells is another tableView with different prototype cells. I've linked this all together fine, but I'm having trouble modifying the innermost prototype cells. Here is why. Here is the relevant code:

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代码如下所示:

My code for outerCell looks like this:

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
    }


}

看,主要问题是,这两个表视图都可以正常工作,但是,在第一段代码中,如果我只是做类似的事情,

See, the main problem is, both these table views work fine and all, but, in the first chunk of code, if I just do something like,

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

这行不通,因为 tableView 似乎总是 self.tableView.

this won't work, as tableView always seems to be self.tableView.

如何修改我的代码,以便在同一代码块中实际影响显示在内部单元格和外部单元格中的文本?

How can I modify my code so that I can actually impact the text displayed in the inner cell, and the outer cell, in the same block of code?

另外,请注意,我知道,根据此处给出的示例,不需要这些嵌套单元格.我只是简化了这里的代码,专注于重要的事情 - 我的实际代码在内部和外部单元格中发生了很多事情.

Also, please note, I know that, based on the example given here, there is no need for these nested cells. I've just simplified the code here to focus on what's important - my actual code has a lot of stuff happening in both the inner and outer cell.

谢谢,任何帮助将不胜感激.

Thank you, any help would be appreciated.

推荐答案

您需要先创建两个不同的单元格类.在外部类:

you need to first create two different cell classes. In outer class :

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 
}

//其他单元格类声明变量

// other cell class Declare variable

var responseCreateBookingObj : APIServiceSuccessCallback?

//发送你想要发送的回调

// send callback from you want to send

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

//也可以在用户滚动时执行

// also do in when user scroll it'll manage

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

//做你的逻辑

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

}

这篇关于在 tableView 中创建 tableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆