[英]iOS Swift: Pass data from ViewController to UITableViewCell
我正在嘗試將數據從ViewController傳遞到自定義UITableViewCell,但它無法正常工作。 當我從ViewController.swift
打印data
,一切正常,但是當我從CustomCell.swift
打印data
時,數組為空。 這是我的代碼:
ViewController.swift
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as! CustomCell
cell.data = data[indexPath.row]
return cell
}
CustomCell.swift
class CustomCell: UITableViewCell {
var data = [CKRecord]()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
println(data)
}
}
在CustomCell.swift
,您只需要設置內容,例如: UILabel
, UIImage
等。
我認為您最好按照這里的指南進行操作,而不是在這里解釋全部內容: http : //www.ioscreator.com/tutorials/prototype-cells-tableview-tutorial-ios8-swift
如果您有更多時間,這是更深的時間: http : //www.raywenderlich.com/81879/storyboards-tutorial-swift-part-1
祝好運! ^ _ ^
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.