向下滚动时UITableView崩溃

时间:2011-10-03 10:29:45

标签: objective-c ios uitableview

我知道有很多关于这个话题的问题,但我无法解决我的问题......

嗯,我已经发现了问题,那就是contactsArray是全局的。如果我评论这些行,表格工作正常。

代码是这样的:

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

在viewDidLoad中我做:

contactsArray = [[NSMutableArray alloc] init];

这里是每个单元格的实现:

- (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;
}

如果我评论persona=[contactsArray objectAtIndex:indexPath.row];[cell setCellNames:[persona name]]; 所以,我很确定问题出在contactsArray

知道为什么会崩溃吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

您不能释放persona对象,因为您只是从数组中获取它。此外,Person *persona = [[Person alloc] init];无效,因为您立即覆盖使用数组中的对象创建的对象。固定代码应如下所示:

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

[cell setCellNames:[persona name]];
[cell setCellStates:@"En Donosti"];
return cell;
相关问题