IPhone Development - Problems on building cells
I have an app that has a lot of views including tableViews but one of them has a 开发者_Python百科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
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:
.
精彩评论