【发布时间】:2016-07-01 00:49:51
【问题描述】:
由于某种原因,尽管没有调用 cellForRowAtIndexPath 函数,但仍调用了 tableview reloadData 函数。我已经在下面发布了完整的代码。简而言之,使用视频作为数据源,每次视频的值发生变化时,表都会重新加载其数据。我已经确认这部分功能在我调试时有效,并且每次该功能发生时输出的视频数量。尽管如此,cellForRowAtIndexPath 甚至一次都无法调用。
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 教程。
-
实际上我正在使用一个包含一个表格的 TableViewController,即在它的一个单元格中的这个表格。
标签: swift uitableview datasource reloaddata