Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Swift + Parse.com : Passing data into a UITableView cell

Hello All,

I'm working on a support ticket application. I'm trying to pass my queried data into my UITableview cell.

Unfortunately I receive the following error: "Thread 1:EXC_BAD_ACCESS(code=1, address = 0x6f6c6f69)" at the following line:

let itemDic:PFObject = self.items.objectAtIndex(indexPath.row) as PFObject

Here is how I'm loading my data:

func addItems() {

    items.removeAllObjects()

    var ticketQuery = PFQuery(className: "Ticket")

    //run query
    ticketQuery.findObjectsInBackgroundWithBlock({
        (success:[AnyObject]!, error: NSError!) -> Void in

        if (success != nil) {

            for object:PFObject! in success as [PFObject]{
                self.items.addObject(object)
            }

            println(self.items)
            self.tblTicket.reloadData()

        }})
}

Here how I'm passing into table cell:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell:TicketTableViewCell = tblTicket.dequeueReusableCellWithIdentifier("TicketCell", forIndexPath: indexPath) as TicketTableViewCell
    let itemDic:PFObject = self.items.objectAtIndex(indexPath.row) as PFObject

    cell.lblTitle.text = itemDic.objectForKey("ticketTitle") as String!
    var statusImg = itemDic.objectForKey("ticketStatus") as NSString!
    if (!statusImg.isEqualToString("None")) {
      cell.imgStatus.image = UIImage(named: statusImg)
    }

    return cell;
}

4 Answers

try using bracket notation to index items and to access the dictionary keys instead:

let itemDic:PFObject = self.items[indexPath.row] as PFObject

        cell.lblTitle.text = itemDic["ticketTitle"] as String!
        var statusImg = itemDic["ticketStatus"] as NSString!
        if (!statusImg.isEqualToString("None")) {
          cell.imgStatus.image = UIImage(named: statusImg)
        }

        return cell;

No error is returned, but data isn't displayed even when if title is forced.

cell.lblTitle.text = "Test"

and the delegate method is getting called correct? print something out inside your delegate to make sure. if you set the title label text I would think it would show up.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell  {

println("cellForRow got called")

Your correct. The delegate method wasn't getting called. I correct that issue though code still breaks at the time line with same error.

alright. commentyour other code out except the code that creates the cell. then try running.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell  {


        let cell:TicketTableViewCell = tblTicket.dequeueReusableCellWithIdentifier("TicketCell", forIndexPath: indexPath) as TicketTableViewCell

    }

if that runs fine then add a line in there that prints out the array of items and the item at the index path:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell  {


        let cell:TicketTableViewCell = tblTicket.dequeueReusableCellWithIdentifier("TicketCell", forIndexPath: indexPath) as TicketTableViewCell

        println(self.items)
        println(self.items[indexPath.row)
    }

let me know what you get