![](/img/trans.png)
[英]Scroll down crashes application on a UITableView with a custom UITableViewCell because of zombie object
[英]UITableView crashes when scroll down
我知道關於這個話題有很多問題,但我無法解決我的問題...
好吧,我已經發現了問題,那就是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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.