首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在tableView中创建tableView

在tableView中创建tableView
EN

Stack Overflow用户
提问于 2020-01-15 08:23:55
回答 1查看 233关注 0票数 1

我已经用原型细胞创建了一个tableView。这些原型细胞中的每一个都是另一个具有不同原型细胞的tableView。我已经很好地把这一切联系在一起了,但我在修改最里面的原型细胞时遇到了麻烦。这就是为什么。相关代码如下:

代码语言:javascript
复制
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代码如下所示:

代码语言:javascript
复制
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
    }


}

看,主要的问题是,这两个表视图都工作得很好,但是,在第一段代码中,如果我只做一些像这样的事情,

代码语言:javascript
复制
if tableView == self.tableView{
    return cell }
else ...

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

如何修改代码才能真正影响同一代码块中显示在内部单元格和外部单元格中的文本?

另外,请注意,我知道,根据这里给出的示例,不需要这些嵌套的单元格。我只是简化了这里的代码,把重点放在重要的地方--我的实际代码在内部和外部单元格中都有很多东西发生。

谢谢,任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2020-01-15 11:16:40

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

代码语言:javascript
复制
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 
}

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

代码语言:javascript
复制
var responseCreateBookingObj : APIServiceSuccessCallback?

//发送您想发送的回调

代码语言:javascript
复制
guard let callBack = self.responseCreateBookingObj else{
        return
    }
    callBack(true as AnyObject)

//当用户滚动它将被管理时,也在做

代码语言:javascript
复制
tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath){
 DispatchQueue.main.async {
                    tableView.beginUpdates()
                    }

//做你的逻辑

代码语言:javascript
复制
        DispatchQueue.main.async {
                    cell.contentView.layoutIfNeeded()
                    tableView.endUpdates()
                }

}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59743623

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档