目标c-自定义UITableView不刷新单元格



我正在创建一个自定义UITableViewCells。我正在使用一个NIB文件作为单元格。它显示了一些来自REST API的数据。我遇到的问题是当我向下滚动&然后备份,单元格不会刷新。它显示了我向下滚动时的数据。这是我的代码-

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 *uPhoto = (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...

为什么我的手机内容不刷新?即使我输入了全新的数据(通过REST API的搜索),一些单元格仍然在表单元格中显示旧视图。

更新:如果我注释掉UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cId];,那么问题就解决了。即,我在滚动时看不到单元格内容的重复。

因为我在UITableViewCell中创建自定义ImageViews,所以在添加新内容之前,我是否需要做一些特殊的事情来清除单元格内容??

在我看来,你似乎忘记重置uPhoto UIImageView,以防照片URL为零。这会带来缓存的数据。

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

您正在使用

static NSString *cId  = @"Cell";

让我们为每个单元格尝试不同的标识符。我面临这个问题,并通过使用不同的小区标识符(如)来解决它

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

我认为它可以解决你的问题。

最新更新