0
由于某种原因,尽管事实上没有调用cellForRowAtIndexPath函数,但仍会调用tableview reloadData函数。我已经发布了下面的完整代码。简而言之,使用视频作为数据源,每当视频值发生变化时,表格就会重新载入其数据。我已经确认,这部分功能可以在我调试完成的情况下工作,并且每次发生此功能时都会输出视频的数量。尽管如此,cellForRowAtIndexPath仍然无法被调用一次。对于索引路径上的行,UITableview单元格不叫
import Foundation
import UIKit
import ReactiveCocoa
import enum Result.NoError
public typealias NoError = Result.NoError
public class VideosTable: UITableView, UITableViewDataSource, UITableViewDelegate {
// MARK: Public variables
public var currentVideo: Video!
// MARK: Private variables
private let videos = MutableProperty<[Video]?>(nil)
private let cellId = "cell"
// MARK Initializers
public required init(signal: Signal<[Video]?, NoError>, frame: CGRect) {
super.init(frame: frame, style: .Plain)
self.dataSource = self
self.delegate = self
self.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellId)
videos <~ signal
videos.signal.observeNext{_ in
self.reloadData()
}
}
videos <~ signal
videos.signal.observeNext{_ in
print("values changed")
self.reloadData()
}
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
// MARK: DataSource
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let videoCount = videos.value?.count ?? 0
print("Video Count: \(videoCount)")
return videoCount
}
public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.dequeueReusableCellWithIdentifier(cellId) ?? UITableViewCell()
cell.textLabel?.text = videos.value![indexPath.row].title
return cell
}
public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 60.0
}
// MARK: Delegate
public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("User selected row at \(indexPath.row)")
currentVideo = videos.value![indexPath.row]
}
}
您应该使用(表)视图控制器来处理这些事情。如果需要的话,查找一些MVC教程。 – Eendje
其实我正在使用一个TableViewController,它包含一个表格,也就是其中一个单元格中的一个。 –