开发者

Custom UITableView not refreshing cells

I am creating a custom UITableViewCells. I am using a NIB file as the cell. Its displaying some data from REST API's. The problem I am having is when I scroll down & then back up, the cells are not refreshed. It shows the data from when I scrolled down. Here's my code -

MyViewController.h

@interface FLOViewController : UIViewController <UISearchBarDelegate, 
UITableViewDelegate, 
UITableViewDataSource>
{
    UISearchBar *sBar;
    UITableView *searchResTable;
    NSArray *searchRes;
    UITableViewCell *resultsOne;
}

@property (nonatomic, retain) IBOutlet UILabel *photos;
@property(nonatomic, retain) IBOutlet UITableView *searchResTable;
@property (nonatomic, retain) NSArray *searchRes;

/* Search Results Templates */
@property (nonatomic, assign) IBOutlet UITableViewCell *resultsOne;

MyViewController.m

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.searchRes == nil)
        return nil;

    static NSString *cId  = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cId];
    if(cell == nil)
    {
        NSLog(@"CREATING NEW CELL");
        [[NSBundle mainBundle] loadNibNamed:@"ResultsOne" owner:self options:nil];
        cell            = self.resultsOne;
        self.resultsOne = nil;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    //from here on displaying images etc from REST API.
    UIImageView *uPhot开发者_如何学运维o = (UIImageView *)[cell viewWithTag:2];
NSString *photoURL  = [[self.searchRes objectAtIndex:[indexPath row]] objectForKey:@"originator_photo_url"];
if(photoURL)
{
    [UIView beginAnimations:@"fadeIn" context:NULL];
    [UIView setAnimationDuration:0.5];

    NSString *urlString = [NSString stringWithString:photoURL];
    [uPhoto setImageWithURL:[NSURL URLWithString:urlString] 
           placeholderImage:[UIImage imageNamed:[NSString stringWithFormat:@"ph%d.png",[indexPath row]]]];

    [UIView commitAnimations];
}

    //similarly display other sections in the cell...

Why are the contents of my cell not refreshing? Even when I put in completely new data (through search from REST API's) some of the cells still show old views in the tablecells.

UPDATE: If I comment out UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cId]; then the problem is solved. i.e. I dont see repetition of cell content on scrolling.

Because I am creating custom ImageViews in UITableViewCell do i need to do something special to clear out the cell content before new content can be added??


To me it looks like you forgot to reset the uPhoto-UIImageView, in case the photoURL is nil. This brings in cached data.

if (photoURL) {
   ...
}
else {
  uPhoto.image = nil;
}


You are using

static NSString *cId  = @"Cell";

Let's try different identifier for each cell. I face this problem and solve it by using different cell identifier like

static NSString *cId  =[NSString StringWithFormat : @"Cell_%d_%d",[indexPath section], [indexPath row] ];

I think it may solve your problem.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜