- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *TelephoneCellId = @"TelephoneCellId";
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:TelephoneCellId];
UIImageView *imgView;
UILabel *lblName;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TelephoneCellId] autorelease];
UIView *uiview = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 90.0)];
imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(10.0, 10.0, 60.0, 60.0)] autorelease];
imgView.tag = IMAGE_TAG;
[uiview addSubview:imgView];
lblName = [[[UILabel alloc] initWithFrame:CGRectMake(80.0, 10.0, 200.0, 20.0)] autorelease];
lblName.tag = NAME_TAG;
[uiview addSubview:lblName];
[cell.contentView addSubview:uiview];
[uiview release];
} else {
imgView = (UIImageView *)[cell.contentView viewWithTag:IMAGE_TAG];
lblName = (UILabel *)[cell.contentView viewWithTag:NAME_TAG];
}
NSDictionary *dict = [[[self.telephoneList objectForKey:[self.keys objectAtIndex:[indexPath section]]] objectAtIndex:[indexPath row]] retain];
imgView.image = [UIImage imageNamed:[dict objectForKey:@"image"]];
lblName.text = [dict objectForKey:@"name"];
[dict release];
return cell;
}
在上面的代碼中,我得到了有關保留和自動釋放的問題。如果從que返回的UITableViewCell爲零,那麼我必須創建一個新的。當我這樣做時,我會自動釋放標籤和圖像。然後我設置文字/圖像。如果UITableViewCell不爲零,那麼我可以通過標記查找視圖,但是這會返回一個自動釋放對象。我應該保留它嗎?然後在完成設置值後釋放?或者是這個okey,因爲視圖保留它,所以它至少是1,因此它是安全的不釋放?內存管理問題
有關此類場景的幫助將非常感謝,因此總結一下,何時纔可以在自動釋放對象上進行設置/獲取值?
謝謝你的時間:)