0
votes

In my iPhone application, I have a UITableView and this contains a UIImageView, button and Label. I am updating my database as per the values from the server and updating the details in the tableview. If I run the app for second time, after some modification at the server, the imageview is not getting updated. Button and label are updating. When I checked the local path for the image from the database, it shows the new image in the documents folder but the table cell still shows the old one. To the see the updated image, I should reinstall the app. What should I do to fix this issue?

Here is the work flow of what I did:

  1. Loading new values from the database, and keeping all the values in an array
  2. Removing the tableview
  3. Creating the tableview again
  4. Reload tableview.

Edit //creating custom cell for the table view for displaying different objects

- (UIMenuItemCell *) getCellContentView:(NSString *)cellIdentifier {

    CGRect CellFrame = CGRectMake(0, 0, 150, 60);
    CGRect Label1Frame = CGRectMake(20, 23, 98, 30);
    CGRect imgFrame = CGRectMake(20, 48, 110, 123);
    CGRect btnFrame = CGRectMake(25, 136, 100, 30);

    UILabel *lblTemp;
    UIImageView *itemImg;
    UIButton *itemBtn;

    UIMenuItemCell *cell = [[UIMenuItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    cell.frame = CellFrame;

    //Initialize Label with tag 1.  
    lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
    lblTemp.tag = 1;
    lblTemp.textColor=[UIColor colorWithRed:139.0f/255.0f green:69.0f/255.0f blue:19.0f/255.0f alpha:1.0f];
    lblTemp.textAlignment = UITextAlignmentCenter;
    lblTemp.backgroundColor = [UIColor clearColor];
    lblTemp.font = [UIFont systemFontOfSize:13.0];
    [cell.contentView addSubview:lblTemp];
    [lblTemp release];

    //Initialize ImageView
    itemImg = [[UIImageView alloc]initWithFrame:imgFrame];
    itemImg.tag = 2;
    [cell.contentView addSubview:itemImg];
    [itemImg release];

    //Initialize Button
    itemBtn = [[UIButton alloc]initWithFrame:btnFrame];
    itemBtn.frame = btnFrame;
    itemBtn.tag = 3;
    itemBtn.titleLabel.textColor = [UIColor blueColor];
    itemBtn.titleLabel.font = [UIFont systemFontOfSize:9.0];
    [cell.contentView addSubview:itemBtn];
    [itemBtn release];


    return cell;
}

// Customize the appearance of table view cells.

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

        NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d", indexPath.row];

        UIMenuItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


        if(cell == nil){
            cell = [self getCellContentView:CellIdentifier];

            cell.transform = CGAffineTransformMakeRotation(M_PI_2);
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            cell.cellItemName = (UILabel *)[cell viewWithTag:1];
            cell.cellitemImage = (UIImageView *)[cell viewWithTag:2];
            cell.cellItemButton = (UIButton *)[cell viewWithTag:3];

            DataBaseClass *itemObj = [appDelegate.itemArray objectAtIndex:indexPath.row];

            NSString *imageLocalFilePath = nil;
            if ([[tempitemStatusArray objectAtIndex:indexPath.row] isEqualToString:@"NotAvailable"]) {
                cell.cellItemProgress.hidden = YES;
                cell.cellItemButton.hidden = NO;
                imageLocalFilePath = [NSString stringWithFormat:@"%@",[tempItemLocalNotAvailPath objectAtIndex:indexPath.row]];
                NSString *date = [self changeDateFormat:itemObj.itemReleaseDate];          
                [cell.cellItemButton setTitle:date forState:UIControlStateNormal]; 
                cell.cellItemButton.userInteractionEnabled = NO;
                cell.userInteractionEnabled = NO;
                [cell.cellItemButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
                [cell.cellItemButton setBackgroundImage:[UIImage imageNamed:@"not_available_bttn_bck_img"] forState:UIControlStateNormal];
            }else if ([[tempitemStatusArray objectAtIndex:indexPath.row] isEqualToString:@"Available"]){
                cell.cellItemButton.userInteractionEnabled = YES;
                cell.userInteractionEnabled = YES;
                cell.cellItemProgress.hidden = YES;
                [cell.cellItemButton setTitle:@"" forState:UIControlStateNormal];
                [cell.cellItemButton setBackgroundImage:[UIImage imageNamed:@"available_bttn_img_normal"] forState:UIControlStateNormal];
                [cell.cellItemButton setBackgroundImage:[UIImage imageNamed:@"available_bttn_img_pressed"] forState:UIControlStateHighlighted];
                [cell.cellItemButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
                [cell.cellItemButton addTarget:self action:@selector(confirmationAlert:) forControlEvents:UIControlEventTouchUpInside];
                imageLocalFilePath = [NSString stringWithFormat:@"%@",[tempItemLocalAvailPath objectAtIndex:indexPath.row]];
            }
            if ([imageLocalFilePath isEqualToString:@""]) {
                [cell.cellitemImage setImage:[UIImage imageNamed:@"item01.png"]];
            }else {
                [cell.cellitemImage setImageWithURL:[NSURL fileURLWithPath:imageLocalFilePath] placeholderImage:[UIImage imageNamed:@"item01.png"]];
            }        
            cell.cellItemName.text = [NSString stringWithFormat:@"%@",[tempItemNameArray objectAtIndex:indexPath.row]];
        }

        return cell;
    }

Please help.

4
are you reusing UITableViewCells and not updating the images correctly in cellForRowAtIndexPath? - muffe
yes i am, I am getting latest details from database and loading it into arrays. And those values will be loaded to table. The issue is only with the imageview, all other objects are fine. And when i checked, I am getting the latest image path from the database with new content, but still showing the old one. - Mithuzz
Can you show your code for tableView:cellForRowAtIndexPath: - beyerss
I have added code, please check. - Mithuzz
Where are you closing the if(cell == nil) ? Make sure to set all the data even if the cell exists (!=nil). - muffe

4 Answers

3
votes

I found the issue, in the cellForRowAtIndexPath method, for setting the image, I used the code

[cell.cellitemImage setImageWithURL:[NSURL fileURLWithPath:imageLocalFilePath] placeholderImage:[UIImage imageNamed:@"item01.png"]];

I used a external classes calls ImageLoading for loading the images and that classes included some cache methods, and that caused the issue. So I changed that line to

[cell.cellitemImage setImage:[UIImage imageWithContentsOfFile:imageLocalFilePath]];

that solved the issue.

Thanks for your support :)

0
votes

There should be a }

after the line

cell.cellItemButton = (UIButton *)[cell viewWithTag:3]; 
0
votes

i took jet a first look at your code, and i found out this "error":

   NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d", indexPath.row];

so... it won't give you any error, it works...

but that's against how table works: doing this you are creating and allocating a new cell for each item of your array/database, meaning that if you need to scroll to see 1.000 items, you create 1.000 cells!!!

it shouldn't be like this.

Normally a table just creates only the cells needed to be shown on the screen/display

meaning that if your table area is 300 pixel in height and each cell is 30 pixel height, then you may need only 11 cells, and you should use/allocate only 11 cells, not 1.000

the magic of cells is to reuse cells when user scrolls them out of screen (e.g. UP), to set it's data and images with the new item data and to put it again on screen (e.g. DOWN)

thats what CellIdentifier is normally used for, and it should be the same for all cells in a table (at least if all cells are similar, but the contained data, and you don't need different cells)

allocating too many cells could give you memory problems, if you manage too much items...

p.s.

i'm probably not answering to your question, there may be other problems in your code, as i said i just read it in hurry

-1
votes

you will have to keep some time delay between laoding your data from databse and tableview reload. you will then able to see the updated image.