【问题标题】:swift tableview delegate method did not response in another classswift tableview委托方法在另一个类中没有响应
【发布时间】:2017-09-15 10:10:32
【问题描述】:

我创建了一个名为 xxxtableviewdelegate 的新类,并将 uitableviewdelgate 和 uitableviewdatasource 放入该类中,然后,我制作

tableView1?.delegate = tableView1Delegate
tableView1?.dataSource = tableView1Delegate

但是委托方法

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)

不叫,请帮帮我

这些是详细代码

let tableView1Delegate = ArticleTableViewDelegate()
    tableView1Delegate.cellIdentifier = cellIdentifier
    tableView1Delegate.headerIdentifier = headerIdentifier
    tableView1Delegate.sectionTitleArray = self.getSectionData()
    tableView1Delegate.cellDatasource = self.getData()

    tableView1 = UITableView(frame: CGRect.init(x: 0, y: 0, width: zkj_width(object: mainScrollView!), height: zkj_height(object: mainScrollView!)), style: .plain)
    mainScrollView?.addSubview(tableView1!)
    tableView1?.delegate = tableView1Delegate
    tableView1?.dataSource = tableView1Delegate
    tableView1?.separatorStyle = .none
    tableView1?.register(ArticleTableViewCell.classForCoder(), forCellReuseIdentifier: cellIdentifier)

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell: ArticleTableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier!, for: indexPath) as! ArticleTableViewCell
    return cell
}

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    let aCell = cell as! ArticleTableViewCell
    let model: ArticleModel = cellDatasource![indexPath.row] as! ArticleModel
    aCell.setModel(articleModel: model)
}

【问题讨论】:

  • 用什么方法设置委托?
  • @just_just_just 你能显示代码吗?使用扩展而不是在不同的类中重构你的表方法会更好。
  • @MaxPevsner UITableViewDelegate 和 UITableViewDataSource,有些方法有效,但这两种方法都无效
  • @just_just_just 都在这里解释-:clean-swift.com/…
  • @TusharSharma 你的意思是在扩展中实现委托吗?但这听起来很奇怪,并不是所有的方法都不起作用,只是这两个委托方法不起作用

标签: ios swift mvvm delegates tableview


【解决方案1】:

tableView1Delegate 似乎有范围问题,请确保它被声明为类实例而不是函数内部。

class YOUR_CONTROLLER {

 var tableView1Delegate : xxxtableviewdelegate

 func YOUR_FUNCTION {
   tableView1 = xxxtableviewdelegate()
   tableView1?.delegate = tableView1Delegate
   tableView1?.dataSource = tableView1Delegate
 }

}

【讨论】:

  • 谢谢,你帮了我,但为什么只是一些委托方法没有被调用?
  • 我相信这应该是随机的行为,不仅仅是两个代表......因为代表参考同时被解除分配。
【解决方案2】:

创建您的 xxxtableviewdelegate 类的对象。并将委托分配给该对象。

var object = xxxtableviewdelegate()
yourTableview.datasourse = object
yourTableview.delegate = object

你应该已经在你的类中实现了 UITableViewDataSource、UITableViewDelegate 协议。

class xxxtableviewdelegate: NSObject,UITableViewDataSource,UITableViewDelegate {
}

【讨论】:

  • 对不起,我不知道如何在 cmets 中发布代码
  • @just_just_just 你在调用重载函数吗??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多