Reputation: 3301
I have used UItableViewCell
default title and detail label before, it worked fine without any warning, Now I have customized the table view cell, I have used label and added as subview, and I am getting image from file also for contacts... Now I am receiving frequently memory warnings and it is crashing, I am reusing identifier like below still warning showing up.. What could be the problem, Am I not freeing the label's memory properly or fetching image from file frequently, causing this? Any idea...
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;
When should I free the memory for above labels... ?
Upvotes: 0
Views: 216
Reputation: 17655
your cell and cell_italic are two different type of cells, because cell is of default UITableViewCell and cell_italic is your custom cell, you have to cast UITableViewCell to cell_italic type cell.also one thing- you have allocated cell & returning cell_italic.
you can cast like this:
CustomResultCell *cell;
cell = (CustomResultCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
update 1: change
`UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];`
to
UITableViewCell *cell =(UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Upvotes: 1