ios - How can I programmatically set dataSource of UITableView? -


I have a strange problem. I am trying to allocate a data source on a program basis.

I have created a UITableView and an IBOuttlet in my ViewController using an interface builder for this. I have created a class that implements UITableViewDataSource . I set my table's dataSource to be an example of the data table

error thread 1: EXC_BAD_ACCESS (code = EXC_i386_GPFLT) and Class is executed in runtime. AppDelegate Define line is highlighted.

  View category Controller: UIViewController {@IBOutlet weak var table: UITableView! Override func viewDidLoad () {ds = MyData () table.dataSource = ds // & lt; ---- Runtime error table .reloadData () super.viewDidLoad ()} // // Other methods} class MyData: NSObject, UITableViewDataSource {func tableView (table view: UITableView!, NumberOfRowsInSection section: int) - & gt; Int {return 5} func tableView (Tableview: UITableView!, CellForRowAtIndexPath indexPath: NSIndexPath!) - & gt; UITableViewCell! {Cell = UITableViewCell () cell.textLabel.text = "a row" return cell}} Any idea why I am getting this runtime error? I am using XCode 6 Beta 4 with Swift.   

Change your code to:

  View class controller: UIViewController {@Iboutlet Weak var table: UITableView! Var Data Source: MyData? Override func viewDidLoad () {super.viewDidLoad () data source = MyData () table.dataSource = DataSource! }}  

Your app breaks because the DS is returned to the viewDidLoad return as soon as possible. You have to keep the context of your data source.


Comments

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -