简体   繁体   中英

iOS Swift: Pass data from ViewController to UITableViewCell

I'm trying to pass data from a ViewController to a custom UITableViewCell but it's not working. When I print data from ViewController.swift everything is in tact but when I print data from CustomCell.swift the array is empty. Here is my code:

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

in CustomCell.swift , you just have to set the content such as: UILabel , UIImage , etc.

Rather than explaining here the whole things, I think you better follow the tutorial here: http://www.ioscreator.com/tutorials/prototype-cells-tableview-tutorial-ios8-swift

if you have more time, this is the deeper one : http://www.raywenderlich.com/81879/storyboards-tutorial-swift-part-1

good luck! ^_^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM