简体   繁体   English

向下滚动时UITableView崩溃

[英]UITableView crashes when scroll down

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. 好吧,我已经发现了问题,那就是contactsArray是全局的。 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: 在viewDidLoad我做:

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]; 如果我评论persona=[contactsArray objectAtIndex:indexPath.row]; and [cell setCellNames:[persona name]]; [cell setCellNames:[persona name]]; So, I'm pretty sure that the problem is with contactsArray 所以,我很确定问题出在contactsArray上

Any idea why is it crashing? 知道为什么会崩溃吗?

Thanks! 谢谢!

You must not release persona object as you just get it from array. 您不能释放persona对象,因为您只是从数组中获取它。 Also the Person *persona = [[Person alloc] init]; 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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