I have a table view with custom cell loaded from xib
the cell has a button change it’s image when touched down
My problem is when I scroll the table up and down out side the view the button reload it’s default image
this is my code
for the table:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifer=@"LabelCell";
TestTableCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifer];
if (cell==nil)
{
cell = [[TestTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifer];
NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"TestTableCell" owner:nil options:nil];
for (UIView *view in views)
{
if([view isKindOfClass:[TestTableCell class]])
{
cell = (TestTableCell*)view;
}
}
cell.selectionStyle=UITableViewCellSeparatorStyleNone;
UIImage *cellbackground=[[UIImage imageNamed:@"cell-background.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
cell.backgroundView=[[UIImageView alloc]initWithImage:cellbackground];
}
return cell;
}
You need to store whether the image has been pressed in your data model. UITableView will reuse your cell for other rows while you scroll and everything inside the cell will be forgotten.