UITableView在快速向上滚动时不渲染单元格

时间:2015-11-19 15:39:42

标签: ios objective-c uitableview

当我在tableview中快速向上滚动时,它的一些单元格开始不显示。 从下图中可以看出,我的tableview没有显示它的一些单元格。

enter image description here

我检查了我的cellforrowatindexpath,它似​​乎通常被称为所有uitableviewcells。你知道从哪里开始调试是个问题吗?

这是我的故事板配置:

enter image description here

这里是我编写的用于调试问题的一小部分代码,但只有第一个NSLog被打印出来并且一切似乎都没问题:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CoreDataTVCell *cell = (CoreDataTVCell *)[tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
NSLog(@"Cell row: %ld section: %ld", (long)indexPath.row, (long)indexPath.section);

@try {
    cell.managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
    [cell updateCell];
}
@catch (NSException *exception) {
    NSLog(@"Object not found: %@", exception.callStackSymbols);
}

if(!cell)
{
    NSLog(@"Cell not found");
}

return cell;
}

同样的问题也发生在具有2个单元标识符的简化TVC(一个用于标题,一个用于其他行):

enter image description here

这是我通过打印可见细胞(不包括第一个)获得的:

enter image description here

2015-11-19 17:34:28.430 XXX[7483:3224955] ADA APENNINE - <CustomerTVCell: 0x15601e000; baseClass = UITableViewCell; frame = (0 240; 768 120); clipsToBounds = YES; hidden = YES; autoresize = W; layer = <CALayer: 0x1557bdf50>>
2015-11-19 17:34:28.431 XXX[7483:3224955] ADA APENNINE - <CustomerTVCell: 0x15601e000; baseClass = UITableViewCell; frame = (0 240; 768 120); clipsToBounds = YES; hidden = YES; autoresize = W; layer = <CALayer: 0x1557bdf50>>
2015-11-19 17:34:28.431 XXX[7483:3224955] AMED - <CustomerTVCell: 0x156036a00; baseClass = UITableViewCell; frame = (0 480; 768 120); clipsToBounds = YES; autoresize = W; layer = <CALayer: 0x1557b68e0>>
2015-11-19 17:34:28.432 XXX[7483:3224955] AMED - <CustomerTVCell: 0x156036a00; baseClass = UITableViewCell; frame = (0 480; 768 120); clipsToBounds = YES; autoresize = W; layer = <CALayer: 0x1557b68e0>>
2015-11-19 17:34:28.432 XXX[7483:3224955] AMPARO - <CustomerTVCell: 0x1560dc400; baseClass = UITableViewCell; frame = (0 720; 768 120); clipsToBounds = YES; autoresize = W; layer = <CALayer: 0x1556e7980>>
2015-11-19 17:34:28.433 XXX[7483:3224955] AMPARO - <CustomerTVCell: 0x1560dc400; baseClass = UITableViewCell; frame = (0 720; 768 120); clipsToBounds = YES; autoresize = W; layer = <CALayer: 0x1556e7980>>
2015-11-19 17:34:28.433 XXX[7483:3224955] ADA APENNINE - <CustomerTVCell: 0x15601e000; baseClass = UITableViewCell; frame = (0 240; 768 120); clipsToBounds = YES; hidden = YES; autoresize = W; layer = <CALayer: 0x1557bdf50>>

在这种情况下可以看到7行,但似乎有些行被重复,而其他行则不存在。

这是应该显示的内容:

2015-11-19 17:45:50.314 X[7483:3224955] Cell row: 0 section: 0
2015-11-19 17:45:50.318 X[7483:3224955] Customer: A LA MODE

2015-11-19 17:45:50.321 X[7483:3224955] Cell row: 1 section: 0
2015-11-19 17:45:50.322 X[7483:3224955] Customer: ADA APENNINE

2015-11-19 17:45:50.325 X[7483:3224955] Cell row: 2 section: 0
2015-11-19 17:45:50.326 X[7483:3224955] Customer: ALTON

2015-11-19 17:45:50.329 X[7483:3224955] Cell row: 3 section: 0
2015-11-19 17:45:50.330 X[7483:3224955] Customer: AMED

2015-11-19 17:45:50.337 X[7483:3224955] Cell row: 4 section: 0
2015-11-19 17:45:50.338 X[7483:3224955] Customer: AMIRA

2015-11-19 17:45:50.360 X[7483:3224955] Cell row: 5 section: 0
2015-11-19 17:45:50.361 X[7483:3224955] Customer: AMPARO

2015-11-19 17:45:50.370 X[7483:3224955] Cell row: 6 section: 0
2015-11-19 17:45:50.371 X[7483:3224955] Customer: AMPARO

1 个答案:

答案 0 :(得分:0)

警告:这不是解决方案,只是一种解决方法!

在用户快速向上滚动时重新加载数据会有所帮助。

属性:

@property (nonatomic) CGPoint lastOffset;
@property (nonatomic) NSTimeInterval lastOffsetCapture;
@property (nonatomic) BOOL isScrollingFast;

方法:

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint currentOffset = scrollView.contentOffset;
    NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate];

    NSTimeInterval timeDiff = currentTime - self.lastOffsetCapture;
    if(timeDiff > 0.1) {
        CGFloat distance = currentOffset.y - self.lastOffset.y;

        CGFloat scrollSpeedNotAbs = (distance * 10) / 1000; //in pixels per millisecond

        //quickly scroll up
        if (scrollSpeedNotAbs < - 3.5) {
            self.isScrollingFast = YES;
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
            NSLog(@"Fast!");
        }

        self.lastOffset = currentOffset;
        self.lastOffsetCapture = currentTime;
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (!decelerate) {
        [self scrollingFinish];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollingFinish];
}

- (void)scrollingFinish {
    //enter code here
    if(self.isScrollingFast){
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        self.isScrollingFast = NO;
    }
}

请记住声明&lt; UIScrollViewDelegate&gt;在你的TVC。