开发者

UITableView crashes when scroll down

开发者 https://www.devze.com 2023-04-10 10:48 出处:网络
I know there\'s a lot of questions about this topic but I have not be able to solve my problem... Well, I have detected the problem, it\'s the contactsArray that\'s global. If I comment that lines, t

I know there's a lot of questions about this topic but I have not be able to solve my problem...

Well, I have detected the problem, it's the contactsArray that's global. If I comment that lines, the table works fine.

The code is this:

@interface ContactsView : UIViewController <UITableViewDelegate, UITableViewDataSource>{
    IBOutlet UITableView *table;
    NSMutableArray * contactsArray;
}
@property (nonatomic, retain) NSMutableArray *contactsArray;
@property (nonatomic, retain) IBOutlet UITableView *table;

In viewDidLoad I do:

contactsArray = [[NSMutableArray alloc] init];

And here the implementation of each cell:

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

    ContactsCell *cell = (ContactsCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell==nil){
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ContactsCell" owner:self options:nil];
        for(id currentObject in topLevelObjects){
            if([currentObject isKindOfClass:[UITableViewCell class]]){
                cell = (ContactsCell *) currentObject;
                break;
            }
        }
    }


    // Configure the cell...
    Person *persona = [[Person alloc] init];
    persona=[contactsArray objectAtIndex:indexPath.row];

    [cell setCellNames:[persona name]];
    [cell setCellStates:@"En Donosti"];

    [persona release];
    return cell;
}

If I comment the persona=[contactsArray objectAtIndex:indexPath.row]; and [cell setCellNames:[persona name]]; So, I'm pretty sure th开发者_StackOverflow中文版at the problem is with contactsArray

Any idea why is it crashing?

Thanks!


You must not release persona object as you just get it from array. Also the Person *persona = [[Person alloc] init]; has no effect as you immediately overwrite object you create with object from array. Fixed code should look like:

Person *persona = [contactsArray objectAtIndex:indexPath.row];

[cell setCellNames:[persona name]];
[cell setCellStates:@"En Donosti"];
return cell;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号