Am trying to use 3 images as the background for a cell. a top image, a middle (repeating image) and a bottom image. If I use a dequeueReusableCellWithIdentifier then it works fine, however as soon as I start scrolling the table then the images don’t stay inside the cell.
See attached image.
http://theutherfish.co.uk/iosss.png
This is the troublesome code
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellidentifier"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellidentifier"];
}
This works fine, but I assume it is a memory hog.
UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];
What would be the best way to have the best of both worlds? i.e. it working and not being memory intensive.
UPDATE
complete cell for row at index path function
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellidentifier"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellidentifier"];
}
UIImageView *img1 = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, 0.0, 310.0, 8.0)];
img1.image = [UIImage imageNamed:@"top.png"];
UIImageView *img2 = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, 8.0, 310.0, [PullRefreshTableViewController heightForExpandingCell:text])];
img2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"middle.png"]];
UIImageView *img3 = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, [PullRefreshTableViewController heightForExpandingCell:text] + 8.0, 310.0, 8.0)];
img3.image = [UIImage imageNamed:@"tempbottom.png"];
[[cell contentView] addSubview:img1];
[[cell contentView] addSubview:img2];
[[cell contentView] addSubview:img3];
return cell;
I would advise using a single stretchable image as your cell background, and adding this in a single imageview (with the appropriate resizing masks) only when creating the cell (I.e. when cell== nil).
You then adjust the height in the heightForRowAtIndexPath method.
At the moment you are adding the image views every time a cell is reused, so you end up with dozens of them in each cell, and they are all different heights, so it looks a mess.
If the stretchable image doesn’t work for you, you should look into creating the image views once only as stated above, and simply adjusting the frames when the cell is re-used.
Alternatively, since your image looks like nothing more than some grey rounded corners, look into CALayer’s borderWidth, borderColor, borderRadius properties.