Ale*_*son 0 datasource uitableview reloaddata swift
出于某种原因,尽管没有调用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]
}
Run Code Online (Sandbox Code Playgroud)
}
问题已经解决了.上面的代码实际上都是正确的,它是在导致错误的应用程序的另一部分.基本上表没有正确地添加到视图层次结构中,因此在tableReload时没有调用CellForRow.同样,表的高度在初始化时实际上为零,并且保持为零.如果每个TableCell的高度(由回调,heightForRowAtIndexPath确定)超过表高度,则永远不会调用CellForRow.
| 归档时间: |
|
| 查看次数: |
3587 次 |
| 最近记录: |