【问题标题】:UITAbleview Cell For Row at Indexpath Not Called未调用 Indexpath 处的行的 UITableview 单元格
【发布时间】: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


【解决方案1】:

问题已解决。上面的代码实际上都是正确的,它是在导致错误的应用程序的另一部分。基本上该表没有被正确地添加到视图层次结构中,因此在 tableReload 时没有调用 CellForRow。同样,表格的高度在初始化时实际上为零并保持为零。如果回调决定的每个TableCell的高度heightForRowAtIndexPath超过了表格的高度,则永远不会调用CellForRow。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 2013-04-05
    • 1970-01-01
    相关资源
    最近更新 更多