Alex Harrison
Alex Harrison

Reputation: 63

UITAbleview Cell For Row at Indexpath Not Called

For some reason, the tableview reloadData function is being called despite the fact that cellForRowAtIndexPath function is not being called. I have posted the full code below. In a nutshell, using the videos as the data source, each time the values of videos changes, the table reloads its data. I have confirmed that this part of functionality works as I have debugged it and the number of videos outputs each time that function occurs. Despite this, cellForRowAtIndexPath fails to be called even once.

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]
}

}

Upvotes: 0

Views: 4691

Answers (1)

Alex Harrison
Alex Harrison

Reputation: 63

Problem has been solved. The code above is actually all correct, it was in another part of the application that was causing the error. Basically the table wasn't being correctly added to the view hierarchy and thus CellForRow was not being called while tableReload was. Likewise, the height of the table was actually zero upon initialization and remained zero. If the height of each TableCell, which is determined by the callback, heightForRowAtIndexPath exceeds that of the table height, CellForRow will never be called.

Upvotes: 5

Related Questions