我正在开发一个聊天应用程序。我不想在滚动时重新加载 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.check
是 NO
) 但问题是当我滚动
时,tableview
重新加载自身并显示整个消息(发送或接收)在左侧,因为 self.check
是 NO
。
我怎样才能阻止这种情况发生?
最佳答案
在您的 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/