2011-10-03 95 views
0

我知道有很多关于这个主题的问题,但我还没有能够解决我的问题......的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]];所以,我很确定问题是机智的h contactsArray

任何想法为什么它崩溃?

谢谢!

回答

2

只要从数组中获取对象,就不能释放persona对象。当你用数组中的对象立即覆盖你创建的对象时,Person *persona = [[Person alloc] init];也不起作用。固定代码应如下所示:

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

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