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

标签 objective-c ios uitableview ios4

我正在创建自定义 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 的搜索),一些单元格仍会在表格单元格中显示旧 View 。

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

因为我正在 UITableViewCell 中创建自定义 ImageViews,所以我需要做一些特殊的事情来清除单元格内容,然后才能添加新内容吗?

最佳答案

在我看来,您似乎忘记了重置 uPhoto-UIImageView,以防 photoURL 为 nil。这会引入缓存数据。

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

关于objective-c - 自定义 UITableView 不刷新单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6343252/

相关文章:

ios - 在 iPhone Core Data 或 SQLite 中存储长期数据的最佳方式是什么?

iphone - 如何从 iPhone 的资源文件夹中获取文件夹和文件列表?

ios - 点击时获取单个 tableviewcell 的高度?

ios - 如何从另一个 View Controller 将数据插入到 tableView 中?

ios - 从服务器下载@2x 和@3x 图像到 Tableview

iphone - 如何在不加载整个 View 的情况下重置 UIImageView 中的图像?

ios - 原型(prototype)单元 - 元素的可访问性顺序

ios - 如何在 UIScrollView 上添加 UIViewController View

objective-c - 预绑定(bind)消息以避免方法查找

iphone - 为 UIView 添加时 UITapGestureRecognizer 无法工作无法检测到问题