滚动时禁用 table 视图重新加载

Disable the table view reloading while scrolling

我正在开发一个聊天应用程序。我不想在滚动时重新加载 tableview

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

        @try {

            static NSString *cellIdentifier=@"cell";
            MessageCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            if(cell==nil)
            {
                cell=[[MessageCell alloc]initMessagingCellWithReuseIdentifier:cellIdentifier];
            }
            if(self.check)
            {
                cell.sent=YES;
                cell.messageLabel.text=[self.sendArray objectAtIndex:indexPath.row];
            }
            else
            {
                cell.sent=NO;
                cell.messageLabel.text=[self.sendArray objectAtIndex:indexPath.row];
            }
            cell.timeLabel.text=@"27-04-2016";
            cell.backgroundColor=[UIColor clearColor];
            return cell;
        }
        @catch (NSException *exception) {
        NSLog(@"Error in func:%s, line:%d with reason:%@",__func__,__LINE__,exception.reason);

        }

}

当我发送消息时它显示在右边(self.checkYES),当我收到消息时它显示在左边(self.checkNO) 但问题是当我 scrolling 时,tableview 重新加载自身并在左侧显示整个消息(发送或接收),因为 self.checkNO.

我怎样才能阻止这种情况发生?

在您的 MessageCell class 中,您必须覆盖 prepareForReuse() 方法并重置所有标志。 当我们滚动 tableview 时,它会重用构造的单元格。因此,它也重用了标志。这是例子

override func prepareForReuse() 
{
    super.prepareForReuse();
    currencyCodeLbl.text = nil;
    currencyNameLbl.text = nil;
    isSelf = false;
    flagImage.hnk_cancelSetImage();
    flagImage.image = nil;
}

首先您应该创建一个实例 属性 来跟踪 table 视图状态。根据此 属性 的值,您可以重新加载或不重新加载您的 table 视图(无论您在代码中的哪个位置执行此操作)。

@property (assign, nonatomic) BOOL isTableViewScrolling;

要跟踪是否有人在 dragging/scrolling table 视图中,您可以使用 scrollView 委托方法:

scrollViewWillBeginDragging:

scrollViewDidEndDecelerating:

并相应地设置isTableViewScrolling

Self.check 应该是一个数组,并且它应该是每一行的特定值。所以你可以这样检查:

if(self.check[indexPath.row]) {
    // Do something!
}

很高兴这对您有所帮助。