[英]Scroll down crashes application on a UITableView with a custom UITableViewCell because of zombie object
我有以下代碼:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"BSTGListCell";
BSTGListCell *cell = (BSTGListCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[[NSBundle mainBundle] loadNibNamed:@"BSTGListCell" owner:self options:nil] objectAtIndex:0];
}
PFObject* currentEl = [self.tableData objectAtIndex:indexPath.row];
cell.title.text = [currentEl objectForKey:@"Name"];
cell.description.text = [currentEl objectForKey:@"Address"];
return cell;
}
向下滾動添加為子視圖的表視圖時,我收到“發送到已釋放實例的消息”。 僵屍檢查員說,被訪問的對象保留在此處:
cell = [[[NSBundle mainBundle] loadNibNamed:@"BSTGListCell" owner:self options:nil] objectAtIndex:0];
可能是由ARC發布的。 為什么會發生這種情況以及如何預防呢?
你真的不應該這樣。 使用筆尖中的單元格的方法是可能在viewDidLoad中注冊筆尖,如下所示:
UINib *nib = [UINib nibWithNibName:@"BSTGListCell" bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:@"BSTGListCell"];
然后在您的cellForRowAtIndexPath中,使用dequeueReusableCellWithIdentifier:forIndexPath:,而不要使用if(cell == nil)子句。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BSTGListCell *cell = (BSTGListCell *)[tableView dequeueReusableCellWithIdentifier:@"BSTGListCell" forIndexPath:indexPath];
PFObject* currentEl = [self.tableData objectAtIndex:indexPath.row];
cell.title.text = [currentEl objectForKey:@"Name"];
cell.description.text = [currentEl objectForKey:@"Address"];
return cell;
}
代碼的實際問題是loadNibNamed:owner:options,返回一個數組,在將其分配給單元之前,必須從該數組中取出一個對象。 但是,無論如何,我展示的方式是一種更有效的方法。
我的問題出在dequeueReusableCell
系列產品上,我像這個鏈接一樣將其取出來,現在我的作品開始了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.