我已经用原型细胞创建了一个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。
如何修改代码才能真正影响同一代码块中显示在内部单元格和外部单元格中的文本?
另外,请注意,我知道,根据这里给出的示例,不需要这些嵌套的单元格。我只是简化了这里的代码,把重点放在重要的地方--我的实际代码在内部和外部单元格中都有很多东西发生。
谢谢,任何帮助都将不胜感激。
发布于 2020-01-15 11:16:40
您需要首先创建两个不同的单元格类。在外部类中:
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()
}}
https://stackoverflow.com/questions/59743623
复制相似问题