我以前使用过UItableViewCell
默认标题和详细信息标签,它在没有任何警告的情况下可以正常工作,现在,我已经自定义了表格视图单元格,我已经使用了标签并将其添加为子视图,并且还从文件中获取了联系人的图像。 。现在,我经常收到内存警告,并且崩溃了,我在重复使用如下所示的标识符,仍然显示警告。.可能是什么问题,我是否没有正确释放标签的内存或经常从文件中获取图像,导致此问题?任何想法...
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// Get the path of the file.
get__path(file_path);
NSString *fle_path = [NSString stringWithUTF8String:file_path];
fle_path = [fle_path stringByAppendingFormat:@"%c%ld%s",'/',obj->user_id,".jpg" ];
NSString *combined_name = [NSString stringWithCString:obj->combined_name encoding:NSASCIIStringEncoding];
NSString *email = [NSString stringWithCString:obj->email encoding:NSASCIIStringEncoding];
CGRect rect = [cell frame];
UIImageView *img_view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 55, 55)];
UILabel *name_label = [[UILabel alloc] initWithFrame:CGRectMake(60, 7, rect.size.width-70, 20)];
UILabel *detail_label = [[UILabel alloc] initWithFrame:CGRectMake(60, 27, rect.size.width-70, 20)];
// Set Image
UIImage *cellImage = [UIImage imageWithContentsOfFile:fle_path];
if (cellImage != nil) {
[img_view setImage:cellImage];
}
// Set name
[detail_label setFrame:CGRectMake(60, 18,rect.size.width-70, 20)];
name_label.text = combined_name;
name_label.font = [UIFont fontWithName:@"Georgia-Italic" size:18];
detail_label.text = email;
[cell_italic.contentView addSubview:img_view];
[cell_italic.contentView addSubview:detail_label];
cell_italic.selectionStyle = UITableViewCellSelectionStyleNone;
rect.size.height = 55;
[cell_italic setFrame:rect];
return cell_italic;
什么时候应该释放上述标签的内存...?
最佳答案
您的单元格和cell_italic是两种不同类型的单元格,因为单元格是默认的UITableViewCell,而cell_italic是您的自定义单元格,则必须将UITableViewCell转换为cell_italic类型的cell。还有一件事-您已分配了cell并返回cell_italic。
您可以像这样进行投射:
CustomResultCell *cell;
cell = (CustomResultCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
更新1:更改
`UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];`
至
UITableViewCell *cell =(UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
关于iphone - 在iOS中收到UITableView的内存警告,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14700722/