UITableView placeholder for dequeued cells
I'm using a UITableView with custom UITableViewCell's to display some news item from an RSS feed. this is working great ,
the only problem is - when is scroll down, i see the "old" cells, and only when my scroll stops, it loads the new content.
So my question is - can i somehow put a placeholder so it would at least show "loading" when scrolling or some other kind of indication?
Thanks in advance :)
ShaiWhen do you fill you cells with content, normally you do this in the UITableViewDataSource
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
This will get called every time a cell is needed before displaying it. If you set the data form that cell here is will be update before it gets displayed.
If you are grabbing something from the web, which could take some time, this is the place to set any content that you grab from the web to loading or placeholder images.
Then in the UITableViewDelegate
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
Start the async fetching of data and update the cell when the data is received.
To avoid duplication of previous data in new cell, use an unique CellIdentifier.
NSString *CellIdentifier = [NSString stringWithFormat:@"%i", indexPath.row];
NewsCell *cell = (NewsCell *) [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil)
{
cell = [[ActivitiesCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier];
}
In NewsCell.m file, use following code and load the new cell by calling initWithStyle method, instead of using loadNibNamed.
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
// Initialization code.
}
return self;
}
精彩评论