views:

11

answers:

1

Hi guys,

I have an app that has a lot of views including tableViews but one of them has a problem that I don't know how to solve. When the constructor of the cell is called:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

the parameter cellForRowAtIndexPath:(NSIndexPath *)indexPath get a random and strange number. I'm building this tableView in the same way as others. Somebody has any idea about what is happening?

Thanks, Claudio

A: 

The trick to indexPath is using the UITableView extention properties indexPath.section and indexPath.row.

In other words if you have a simple list of string:

self.data = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];

then in tableView:cellForRowAtIndexPath: you want to do:

cell.textLabel.text = [self.data objectAtIndex:indexPath.row];

If you have an Array within an Array:

self.data = [NSArray arrayWithObjects:
                [NSArray arrayWithObjects:@"A.1", @"A.2", nil],
                [NSArray arrayWithObjects:@"B.1", @"B.2", nil],
                nil];

Then you'll want to check the section and row property:

NSArray *strings = [self.data objectAtIndex:indexPath.section];
cell.textLabel.text = [strings objectAtIndex:indexPath.row];

Make sure you return the right numbers for numberOfSectionsInTableView: and tableView:numberOfRowsInSection:.

wm_eddie
I've checked both (indexPath.section and indexPath.row) and both are correct. But when it tries to print the first Cell, indexPath comes with a strange number (random number, but everytime something like 95622992). Where exactly is set indexPath before it be called?
Claudio
indexPath itself is a pointer to some random place in memory, So if you tried to print the indexPath it would be some large number. To use indexPath, you need to use it's properties or methods. If you want to print objects to the console use `%@` in a format string. Like `NSLog(@"indexPath = %@", indexPath);`
wm_eddie
Yeah, it was exactly what you said. Thanks a lot!
Claudio