user1150082
user1150082

Reputation: 67

My iphone application crashes when i use table view

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *theCell=[UITableViewCell alloc];
     theCell=[theCell initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

      NSLog(@"%i",[table count]);

         theCell.textLabel.text=[table objectAtIndex:[indexPath row]];  

    return theCell;
}

There are three values inserted in a file Values.plist and they populate the NSArray "table" in the function viewDidLoad() but my application crashes at

" theCell.textLabel.text=[table objectAtIndex:[indexPath row]];  "
But the line "NSLog(@"%i",[table count]);" works and shows me 3
please help .

Upvotes: 0

Views: 138

Answers (2)

Hubert Kunnemeyer
Hubert Kunnemeyer

Reputation: 2261

This is the standard implementation:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *cellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }


       cell.textLabel.text = [table objectAtIndex:indexPath.row];
        return cell;
    }

As a side note this is the ARC implementation. If your not using ARC just add an autoRelease:

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]autoRelease];

also make sure your dataSource numberOfSections is set right:

return 1;

and numberOfRows is set to the array:

return [table count];

Upvotes: 1

Parth Bhatt
Parth Bhatt

Reputation: 19469

Please take care of following issues:

1) Make sure that NSArray has values i.e it is populated.

2) Also try NSLog(@"%d",[table count]);

I think you should better go with %d and not %i.

Hope this helps you.

Upvotes: 0

Related Questions