0

in my cellForRowAtIndexPath delegate i am trying to load different images into a custom cell depending on which array (detailViewListLoadMore) is being loaded into the table. The array contains annotation objects.

however the images dont seem to refresh, they seem to be the same as the last array unless i scroll down and then they are correct, its like they are being pre loaded from the last array.

here is my method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

//set up custom table cell
static NSString *CustomCellIdentifier = @"CustomCellIdentifier";

//reuse the custom cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier]; 

//setup custom cell
if (cell == nil) {

    //load custom cell from nib
    NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"CustomDetailViewCell"
                                                      owner:self
                                                    options:nil];

    if ([nibArray count] > 0)
        cell = self.detailCell;
    else
        NSLog(@"failed to load CustomDetailViewCell nib file");

} 

//configure the cell row
NSUInteger row = [indexPath row];

//create temp cell lable strings
NSString *headingString, *detailString, *subDetailString, *imageString;
UIImage *cellDistanceImage;

//create temp annotation object
Annotation *tempAnnotationObj = [detailViewListLoadMore objectAtIndex:row];            

if (tempAnnotationObj.type == @"one")
    cellDistanceImage = [UIImage imageNamed:@"AnnotationOne.png"];
else if (tempAnnotationObj.type == @"two")
    cellDistanceImage = [UIImage imageNamed:@"AnnotationTwo.png"];
else if (tempAnnotationObj.type == @"three")
    cellDistanceImage = [UIImage imageNamed:@"AnnotationThree.png"];

}

//set table cell strings
headingString = tempAnnotationObj.streetName;
detailString = [NSString stringWithFormat:@"No of Bays: %@",tempAnnotationObj.bays];
        subDetailString = [NSString stringWithFormat:@"%@m",tempAnnotationObj.distance];           

//update heading label
UILabel *headingLabel = (UILabel *)[cell viewWithTag:1];
headingLabel.text = headingString;

//update detail label
UILabel *detailLabel = (UILabel *)[cell viewWithTag:2];
detailLabel.text = detailString;

//update sub detail label
UILabel *subDetailLabel = (UILabel *)[cell viewWithTag:3];
subDetailLabel.text = subDetailString;    

//set appropriate distance image
[self.detailCellImage setImage:cellDistanceImage];

return cell;
}

Any help would be appreciated.

Cheers,

Chris

3
  • first of all please use [tempAnnotationObj.type isEqualToString:@"three"] instead of ==, it is a wrong method to compare NSStrings. then you can check out which one of the condition is getting selected in your if/elseif case. Sep 27, 2011 at 11:18
  • there is one extra closing brace '}'. is there a few lines missing in sample code? Sep 27, 2011 at 11:21
  • yeah, i didnt post it all. just the relevant bits
    – ChrisM
    Sep 27, 2011 at 11:29

3 Answers 3

0

Have you tried putting [tableView reloadData]; into the method that changes tableView's data source?

0
0

why you have set

//set appropriate distance image
[self.detailCellImage setImage:cellDistanceImage];

with self if you want to set image on cell? i think it should be

cell.detailCellImage

5
  • because for the cell image i created a UIImageView and linked it up in the IB. if i change it to cell i get the error "property 'detailCellImage' not found on object of type "UITableViewCell *"
    – ChrisM
    Sep 27, 2011 at 11:41
  • oh.. so you are keeping an imageview in your class and the single imageview is showing up on all cells? Sep 27, 2011 at 11:47
  • i dont know why i set it up like that, but i dont want it keeping in my class. i have changed it to... UIImageView *anImageView = (UIImageView *)[cell viewWithTag:4]; [anImageView setImage:cellDistanceImage];. seems to work fine, dont know why i didnt do it like this before
    – ChrisM
    Sep 27, 2011 at 11:53
  • 90% chances of the problem are related to not calling [tableView reloadData] after updating the dataSource. please check it atleast 4 times that reloadData is being called from somewhere when you update the data source. if still the problem cannot be corrected. there is problem at some other part of your code which is less likely. Sep 27, 2011 at 12:02
  • cheers, i will check. I wonder if this issue also applies to an activity indicator i have on the last cell of the table to load more rows. however it works when pressed once then disappears?
    – ChrisM
    Sep 27, 2011 at 12:07
0

seemed to work it out using this....

UIImageView *anImageView = (UIImageView *)[cell viewWithTag:4];
[anImageView setImage:cellDistanceImage]; 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.