2016-03-15 46 views
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] 
} 

}

+0

您应该使用(表)视图控制器来处理这些事情。如果需要的话,查找一些MVC教程。 – Eendje

+0

其实我正在使用一个TableViewController,它包含一个表格,也就是其中一个单元格中的一个。 –

回答

2

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