开发者

(iPad/iPhone) refresh a table cell while it is on screen

I have a table view with custom cells, each of which have images and some text which must be parsed from a webpage. I have and operation queue which gets the data from the page and calls the method (void)addLoadedImageAndExcerpt:(NSString *)imgExc in the tableviewcontroller after each page's data is loaded and stores the data in 2 arrays. I need each cell to refresh once the image and text that assoc开发者_运维知识库iated with it are loaded into these 2 arrays (named "articleExcerpts" and "imageDataObjects").

the method is as follows:

- (void)addLoadedImageAndExcerpt:(NSString *)imgExc {
NSArray *imgAndExcerpt = [imgExc componentsSeparatedByString:@"{|}"];
[articleExcerpts addObject:[imgAndExcerpt objectAtIndex:1]]; 

NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [imgAndExcerpt objectAtIndex:0]]];
[imageDataObjects addObject:imageData];
//count how many rows have been loaded so far.
loadedCount ++;

[self.table reloadData];//table is a UITableView

[imageData release];
}

the problem is, I can't get the cells to change while they are on screen. Once I scroll, they show the proper data, while they are on screen, I can't get them to change. I tried the methods outlined here and here, but they don't work. I tried calling tableView:cellForRowAtIndexPath: for the relevant row and modifying the variables, but that didn't solve anything because that method seems to create a new cell every time is is called, and doesn't get the existing ones (I'll post the code for that method further down).

Using [self.table reloadData] as I have it now doesn't seem do anything either, which really confuses me...

my tableView:cellForRowAtIndexPath: method (I bet the problem is here. I'm not convinced I creating my custom cells properly)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomizedCell";

CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];

    for (id currentObject in topLevelObjects){
        if ([currentObject isKindOfClass:[UITableViewCell class]]){
            cell =  (CustomCell *) currentObject;
            break;
        }
    }
}

// Configure the cell... 

//title
cell.titleString = [titles objectAtIndex:indexPath.row];
//date
cell.dateString = [dates objectAtIndex:indexPath.row];

//Photo. check if imageDataObjects array is complete up to the current row yet
if (loadedCount > indexPath.row) {
    if ([imageDataObjects objectAtIndex:indexPath.row] != @"NA") {
        cell.imageData = [imageDataObjects objectAtIndex:indexPath.row];
    } else {
        cell.imageData = NULL;
    }
}

//Excerpt. check if loadedCount array is complete up to the current row yet
if (loadedCount > indexPath.row) {
    cell.exerptString = [articleExcerpts objectAtIndex:indexPath.row];
}

return cell;
}

what am I missing?


I have had a similar problem before, and was able to get it working by including the lines

[table beginUpdates];
[table endUpdates];

at the end of the method where your data is received (so call them once you have the data to populate the cells).

Hope this works for you too!


Hmm, I think you're only supposed to interact with UI components in the main thread. NSOperationQueue stuff runs in another thread. Instead of calling

[self.table reloadData]

try

[self.table performSelectorOnMainThread:@selector(reloadData:) withObject:nil waitUntilFinished:NO]


As far as I understand, the image is being loaded, and then added to an array of images (imageDataObjects), and the row never updates.

First things first, are you sure that the method addLoadedImageAndExcrept is adding the images in order? Remember that NSArray objects are nil-terminated, and therefore, if you're adding an image for a row further, it won't appear if a previous image is nil. What happens if an image comes nil? The array will end abruptly. Use the "count" method on the array to check if this happens, add dummy objects, or swtich to a dictionary. This may not solve your current issue, but it's something to consider. (*)

Aside from that, if images are being loaded correctly, the only reason for your code to not work (in what I understand from the code), is that the table IBOutlet you added, is not connected.

*EDIT: I noticed that you're checking for @"NA" on the row (although I don't see where it's being set), so you probably already considered that

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜