一旦我开始滚动,前3个单元格是相同的但是转向正确?

时间:2013-11-07 23:50:52

标签: ios objective-c uitableview

所以,每当我启动我的应用程序时,它看起来像这样,前3个单元格都是一样的。但是一旦我开始向上和向下滚动它就会修复,而单元格2和3会显示正确的信息。

目前这是我的代码的样子:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"MyFeedCell";
    SampleTableCell *cell = (SampleTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"SampleTableCell" owner:self options:nil];
        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = (SampleTableCell *)currentObject;
                break;
            }
        }
    }
    // Configure the cell.
    NSUInteger row = [indexPath row];
    NSUInteger count = [_allEntries count];
    RSSEntry *entry = [_allEntries objectAtIndex:(count-row-1)];
    NSString *imageString = entry.image;
    imageString = [imageString stringByReplacingOccurrencesOfString:@"128x67" withString:@"768x432"];
    NSLog(@"IMAGE : %@", imageString);

    NSURL *imgURL = [[NSURL alloc] initWithString:imageString];
    [cell.profilePicture setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:nil]];
    NSString *date = [entry.articleDate substringToIndex:12];
    cell.datePosted.text = date;
    cell.name.text = entry.articleTitle;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;


    return cell;
}

这是一个关于它目前如何运作的GIF http://gyazo.com/2011099496257445c008a717beabd8fd

1 个答案:

答案 0 :(得分:0)

不再需要整个topLevelObjects gambit,用这个替换你的代码,看看你是否仍然遇到问题:

//this above @interface
static NSString *CellIdentifier = @"MyFeedCell";

//Put this in viewDidLoad
[self.table registerClass:[SampleTableCell class] forCellReuseIdentifier:CellIdentifier];
[self.table registerNib:[UINib nibWithNibName:@"SampleTableCell" bundle:nil] forCellReuseIdentifier:CellIdentifier]


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    SampleTableCell *cell = (SampleTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

       // Configure the cell.
    NSUInteger row = [indexPath row];
    NSUInteger count = [_allEntries count];
    RSSEntry *entry = [_allEntries objectAtIndex:(count-row-1)];

    NSString *imageString = entry.image;
    imageString = [imageString stringByReplacingOccurrencesOfString:@"128x67" withString:@"768x432"];
    NSLog(@"IMAGE : %@", imageString);

    NSURL *imgURL = [[NSURL alloc] initWithString:imageString];
    [cell.profilePicture setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:nil]];

    NSString *date = [entry.articleDate substringToIndex:12];
    cell.datePosted.text = date;
    cell.name.text = entry.articleTitle;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;


    return cell;
}