67188

Swift Subclassing UITableViewDataSource EXC_BAD_ACCESS

Question:

I am trying To subclass My UITableViewDatasource, However My App Crashes with EXC_BAD_ACCESS. No explanations no error messages just crashes. A Sort Version of my DataSource Looks Like This.

import UIKit class DataSource :NSObject, UITableViewDataSource{ var tableView:UITableView let CellIdentifier = "Cell" init(tableView : UITableView) { println("Data Source") self.tableView = tableView super.init() self.tableView.dataSource = self } //:MARK UITableViewDataSource //-----------------------------------------------------------------------------------------// // Number Of Rows In Tableview //-----------------------------------------------------------------------------------------// func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int { return 10 } //-----------------------------------------------------------------------------------------// // Cell For Row At Index Path //-----------------------------------------------------------------------------------------// func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { let cell = self.tableView.dequeueReusableCellWithIdentifier(CellIdentifier, forIndexPath: indexPath) as UITableViewCell cell.textLabel.text = "Title of Row: #\(indexPath.row)" return cell } }

As You can see There is nothing special, this code works fine in Playground

Below is my call to DataSource

import UIKit class ListEntriesViewController :UITableViewController{ override func viewDidLoad() { println("View Did Load") var data = DataSource(tableView: self.tableView) self.tableView.dataSource = data } }

What Am I missing, Why this code works works in Playground but not in my App.

Thank You

Answer1:

The datasource is not retained by the table view. The object is destroyed immediately after viewDidLoad. You need to store it in a property.

Also note that <em>UITableViewDataSource</em> is a protocol, not a class. Hence it can't be subclassed.

Recommend

  • UITableView cell goes blank when scrolling
  • Creating a checkmark inside a Pickerview [duplicate]
  • How to use the File System Events API in Swift?
  • Column Nullability/Optionality: NULL vs NOT NULL
  • What's the benefit of the trailing apostrophe in character literals
  • CRASH: *** -[__NSArrayM objectAtIndex:]: index 4294967295 beyond bounds [0 .. 9]
  • Is is safe to use HSQLDB for production? (JBoss AS5.1)
  • Is there a way to disable a specific event in kendo ui scheduler?
  • EF 4.1 DBContext AutoDetectChangesEnabled
  • Center align outputs in ipython notebook
  • Reading a file into a multidimensional array
  • Creating Java object from class name with constructor, which contains parameters [duplicate]
  • how to avoid repetitive constructor in children
  • App restarts from wrong activity
  • how to save the state in userdefaults of accessory checkmark-iphone
  • custom UITableViewCell with image for highlighting
  • Word Open XML Mail Merge
  • How do I alternate colors in Flat List (React Native)
  • Jetty Server not starting: Unable to establish loopback connection
  • Is there a javascript serializer for JSON.Net?
  • Pass value from viewmodel to script in zk
  • Deleting and Updating values from a cusrsor adapter
  • Counter field in MS Access, how to generate?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • Sending data from AppleScript to FileMaker records
  • Where to put my custom functions in Wordpress?
  • sending/ receiving email in Java
  • vba code to select only visible cells in specific column except heading
  • Javascript + PHP Encryption with pidCrypt
  • Eraser for UIBezierPath
  • Websockets service method fails during R startup
  • Compare two NSDates in iPhone
  • Buffer size for converting unsigned long to string
  • Load html files in TinyMce
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • embed rChart in Markdown
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app