ios - 滚动时禁用表格 View 重新加载

标签 ios objective-c uitableview

我正在开发一个聊天应用程序。我不想在滚动时重新加载 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.check is YES),当我收到消息时,它显示在左侧( self.checkNO) 但问题是当我滚动时,tableview 重新加载自身并显示整个消息(发送或接收)在左侧,因为 self.checkNO

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

最佳答案

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

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

关于ios - 滚动时禁用表格 View 重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36981941/

相关文章:

iphone - 将 UITableView 的相同默认背景设置为自定义 View Controller

ios - 在代码中使两个 AutoLayout 约束相等?

ios - cellForRow(在 : indexPath) returns nil Swift3

ios - 如何从本地通知打开特定的 View Controller ?

ios - 如何在没有操作的情况下呈现新的 UIViewController?

objective-c - 将毫秒转换为分钟

objective-c - 更改背景位置和菜单栏半透明度

ios - 是否有比 Objective-C 中的委托(delegate)更简洁的方法来处理异步 http 请求?

ios - XML 错误 : Unquoted attribute value

ios - UIScrollView 不滚动的自定义 UIView 小部件