简体   繁体   中英

UITableView + NSMutableArray weird behavior

I'm currently trying to populate an UITableView from a plist downloaded from the internet. The plist gets loaded into a NSMutableArray and then saved as a file, which works fine.

When trying to populate my UITableView with it though, I run into trouble.

The UITableView displays only the first 8 entries from the *plist file (but there are 98) and then loops through them until its reached 98, so it's always the same 8 entries instead of 98 different ones.

Here's my log:

2010-07-20 15:50:19.064 myCustomers[15221:207] New Cell
2010-07-20 15:50:19.065 myCustomers[15221:207] Bob
2010-07-20 15:50:19.068 myCustomers[15221:207] New Cell
2010-07-20 15:50:19.069 myCustomers[15221:207] Jo
2010-07-20 15:50:19.071 myCustomers[15221:207] New Cell
2010-07-20 15:50:19.071 myCustomers[15221:207] Neil
2010-07-20 15:50:19.075 myCustomers[15221:207] New Cell
2010-07-20 15:50:19.075 myCustomers[15221:207] Robert
2010-07-20 15:50:19.077 myCustomers[15221:207] New Cell
2010-07-20 15:50:19.078 myCustomers[15221:207] Jack
2010-07-20 15:50:19.079 myCustomers[15221:207] New Cell
2010-07-20 15:50:19.080 myCustomers[15221:207] John
2010-07-20 15:50:19.081 myCustomers[15221:207] New Cell
2010-07-20 15:50:19.082 myCustomers[15221:207] Ralph
2010-07-20 15:50:19.083 myCustomers[15221:207] New Cell
2010-07-20 15:50:19.084 myCustomers[15221:207] Bart

It creates the new cells, but then stops at 8 and loops. :/

Here's how I create the cell and get the array's data:

if (tableView == myTable)
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

if (indexPath.section == 0) 
{
cell.textLabel.text = [self.dataForTable objectAtIndex:indexPath.row];
    NSLog(@"New Cell");
    NSLog(@"%@",[self.dataForTable objectAtIndex:indexPath.row]);
}
}
return cell;
}

The NSMutableArray "dataForTable" is created as such:

if ([[[NSMutableArray alloc] initWithContentsOfFile:fullFileName] autorelease] != nil)
    {
    self.dataForTable = [[[NSMutableArray alloc] initWithContentsOfFile:fullFileName] autorelease];
    }
    else 
    {
        self.dataForTable = [[NSMutableArray alloc] init]; //create a brand new array if there is no entries file.
    }

The array's data fetches fine, I've checked this in the log and all 98 entries are shown there, but the table view would only use 8.

I've been unable to find a solution for this, could anyone please help me out?

Thank you!

In your cellForRowAtIndexPath: method you setup your cell only when you create a new instance of it. While UITableView reuses cells for rows (ie cells for rows that become hidden are used for rows that become visible) and you need to setup cell for your row each time - so you should change your code to:

... 
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}

if (indexPath.section == 0) 
{
    cell.textLabel.text = [self.dataForTable objectAtIndex:indexPath.row];
    NSLog(@"New Cell");
    NSLog(@"%@",[self.dataForTable objectAtIndex:indexPath.row]);
}

return cell;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM