Search code examples
iosuitableviewswiftswift-playground

Swift UITableView : Datasource assigning Issue


This is a simple UITableView code that is working correctly in playground:

import UIKit

class MyDataSuorce : NSObject, UITableViewDataSource {
    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
    {
        let cell = UITableViewCell(style: .Value2, reuseIdentifier: nil)

        cell.textLabel.text = "Row #\(indexPath.row)"

        return cell;
    }

    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
    {
        return 4
    }

}


let list = UITableView(frame:CGRectMake(0,0,300,300), style: .Plain)
let d = MyDataSuorce()
list.dataSource = d
list.reloadData()

But when I first tried this code, it doesn't work and just show an empty UITableView:

let list = UITableView(frame:CGRectMake(0,0,300,300), style: .Plain)
list.dataSource = MyDataSuorce()
list.reloadData()

What's wrong with second code ?

EDIT:

This is the console Output for list.dataSource = MyDataSuorce() :

Playground execution failed: error: error: Couldn't lookup symbols: _memmove


Solution

  • Maybe I resolve the "Why?"...

    I think, it's about swift variable lifetime. (correct?)

    In second code, I just forgot that returned object (by MyDataSource()) will be exist during that line of code.