【发布时间】:2016-06-20 11:46:56
【问题描述】:
我有一个 ViewController,它有一个集合视图。在每个集合视图单元格内,它们是一个表格视图。由于那个tableView,我面临着一个性能问题。当调用其 cellforItemAtIndexPath 时,我尝试为每个 CollectionViewCell 重新加载数据。因此,我的 CPU 使用率提高了 90%。我的 ViewController 是这样的
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCustomCollectionViewCell", forIndexPath: indexPath) as! MyCustomCollectionViewCell
// This line is causing performance issue
cell.tableView.reloadData()
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(40, 40)
}
MyCustomCollectionViewCell 看起来像这样
class ChannelCollectionViewCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {
@IBOutlet weak var tableView: UITableView!
var delegate: ChannelCollectionViewCellDelegate?
// MARK: - Table view data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 10
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 120
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCustomTableViewCell", forIndexPath:
indexPath) as! MyCustomTableViewCell
cell.title = "Testt Title"
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 50
}
}
能否将 cell.tableView.reloadData() 移到更好的位置,以免导致性能问题
【问题讨论】:
标签: ios swift uitableview uicollectionview uicollectionviewcell