簡體   English   中英

永遠不會調用TableView cellForRowAtIndexPath(iOS,Swift)

[英]TableView cellForRowAtIndexPath is never called (iOS, Swift)

我在UITableViewController中創建表視圖時遇到問題。 我已經嘗試了我發現的所有內容,正確設置了reuseIdentifier,行數返回非零數字,並在storyboard中設置了高度。 僅調用cellForRowAtIndexPath,其他表函數正確運行。

這是代碼:

TableViewController:

import UIKit
import Photos

class CropTableViewController: UITableViewController {

var photos = [Photo]()
var photoAssets = [PHAsset]()


override func viewDidLoad() {
    super.viewDidLoad()

}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return photos.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cellIdentifier = "Cell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CropTableViewCell

    let photo = photos[indexPath.row]

    cell.photoImageView.image = photo.photo

    return cell
}
}

TableViewCell:

import UIKit

class CropTableViewCell: UITableViewCell {


@IBOutlet weak var photoImageView: UIImageView!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}
}

photos數組為空,因此photos.count為零,因此numberOfRowsInSection返回0 ,因此運行時知道沒有單元格,並且不需要調用cellForRowAtIndexPath

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM