向下滚动时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

知道为什么它会崩溃吗?

谢谢!

你不能释放persona对象,因为你只是从数组中得到它。此外,Person *persona = [[Person alloc] init];没有效果,因为您会立即用数组中的对象覆盖您创建的对象。固定代码应该看起来像:

Person *persona = [contactsArray objectAtIndex:indexPath.row];
[cell setCellNames:[persona name]];
[cell setCellStates:@"En Donosti"];
return cell;

最新更新