Reputation: 19303
I'm trying to change a picture inside my tableview. I'm using [myTableView reloadData];
in another method (that is firing up) to refresh my TableView
and I can see that other elements in the tableview are refreshing but the image stays the same. What Am I missing?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *lblName;
UIImageView *checkMark = [[UIImageView alloc] initWithFrame:CGRectMake(230, 0, 30, 50)];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(10, 1, 260, 70)];
backgroundView.backgroundColor = UIColorFromRGB(0x000000, 0.3);
backgroundView.userInteractionEnabled = YES;
checkMark.image = [UIImage imageNamed:@"V.png"];
checkMark.contentMode = UIViewContentModeCenter;
checkMark.backgroundColor = [UIColor clearColor];
[backgroundView addSubview:checkMark];
[checkMark release];
[cell.contentView addSubview:backgroundView];
[backgroundView release];
}
if (something)
{
checkMark.image = [UIImage imageNamed:@"halfleft.png"];
NSLog(@"something was done");
}
return cell;
}
Upvotes: 0
Views: 262
Reputation: 2901
reloadData correctly calls this method.
Your problem is simple, the pointer on "checkMark" isn't good in the case the cell is "reused". Do something like this :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIView *backgroundView;
UIImageView *checkMark;
if (cell == nil) {
checkMark = [[UIImageView alloc] initWithFrame:CGRectMake(230, 0, 30, 50)];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
backgroundView = [[UIView alloc] initWithFrame:CGRectMake(10, 1, 260, 70)];
backgroundView.backgroundColor = UIColorFromRGB(0x000000, 0.3);
backgroundView.userInteractionEnabled = YES;
backgroundView.tag = 555;
checkMark.image = [UIImage imageNamed:@"V.png"];
checkMark.contentMode = UIViewContentModeCenter;
checkMark.backgroundColor = [UIColor clearColor];
checkMark.tag = 666;
[backgroundView addSubview:checkMark];
[checkMark release];
[cell.contentView addSubview:backgroundView];
[backgroundView release];
} else {
backgroundView = (UIImageView *)[cell.contentView viewWithTag:555];
checkMark = (UIImageView *)[backgroundView viewWithTag:666];
}
if (something)
{
checkMark.image = [UIImage imageNamed:@"halfleft.png"];
NSLog(@"something was done");
}
return cell;
}
But, you should do something more simple (less subviews if possible. OR subclass UITableViewCell to have a pointer on the checkmark ;-)
Upvotes: 1