重新加载/滚动表格时标签消失



我有一个UITableview,有两个UILabels,就像在聊天中一样。当可见UITableview被填充并且当我添加额外的单元格时,我的UILabel消失了。

可能还有其他可见UILabels。只有那 2 个聊天UILabels被隐藏。谁能给我建议可能的解决方案?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
ChatScreenViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChatCell" forIndexPath:indexPath];
index = indexPath.row + 1;
indexx = indexPath;

cell.ChatLabel1.layer.cornerRadius = 8;
cell.ChatLabel1.layer.masksToBounds = YES;
cell.ChatLabel2.layer.cornerRadius = 8;
cell.ChatLabel2.layer.masksToBounds = YES;
cell.ImageviewLabel1.layer.cornerRadius = 20;
cell.ImageviewLabel1.layer.masksToBounds = YES;
cell.ImageviewLabel2.layer.cornerRadius = 20;
cell.ImageviewLabel2.layer.masksToBounds = YES;
cell.ImageviewLabel2.backgroundColor = [UIColor greenColor];

if([Replychat[indexPath.row] valueForKey:@"bot"] != NULL)
{
cell.ChatLabel1.text = [Replychat[indexPath.row] valueForKey:@"bot"];
cell.ChatLabel2.hidden = YES;
cell.ImageviewLabel2.hidden = YES;
cell.TimeLabel1.text = TimeArray[indexPath.row];
}
else
{
cell.ChatLabel2.text = [Replychat[indexPath.row] valueForKey:@"User"];
cell.ChatLabel1.hidden = YES;
cell.ImageviewLabel1.hidden = YES;
cell.TimeLabel2.text = TimeArray[indexPath.row];
}
return cell;

}

自定义表格视图单元格中的标签在滚动后消失

我的代码有一个 if else 语句,其中一个分支可以设置单元格。聊天标签2.隐藏 = 是;但另一个分支不设置单元格。ChatLabel2.hidden = NO;.因此,一旦标签被隐藏,它就永远不会被取消隐藏。当具有隐藏标签的单元格被重复使用时,标签将保持隐藏状态。

添加单元格。聊天标签2.隐藏 = 否;(以及所需的任何其他"反向"配置)到 if 语句。

尝试:

//....
cell.ImageviewLabel2.backgroundColor = [UIColor greenColor];
// because tableview reuse uitableviewcell, so you must reset hidden status for lable
cell.ChatLabel2.hidden = NO;
cell.ChatLabel1.hidden = NO;
if([Replychat[indexPath.row] valueForKey:@"bot"] != NULL)
//....

试试这种方式

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; {

ChatScreenViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChatCell" forIndexPath:indexPath];

如果(单元格 == 无){

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"ChatCell"];

}

index= indexPath.row + 1;

索引 x = 索引路径;

细胞。聊天标签1.图层.角落半径 = 8;

细胞。ChatLabel1.layer.masksToBounds = YES;

细胞。聊天标签2.层角半径 = 8;

细胞。ChatLabel2.layer.masksToBounds = YES;

细胞。图像视图标签1.图层.角半径 = 20;

细胞。ImageviewLabel1.layer.masksToBounds = YES;

细胞。图像视图标签2.图层.角半径 = 20;

细胞。ImageviewLabel2.layer.masksToBounds = YES;

细胞。ImageviewLabel2.backgroundColor = [UIColor greenColor];

if([Replychat[indexPath.row] valueForKey:@"bot"] != NULL) {

细胞。聊天标签1.隐藏 = 否;

细胞。ChatLabel1.text = [Replychat[indexPath.row] valueForKey:@"bot"];

细胞。聊天标签2.隐藏 = 是;

细胞。图像视图标签2.隐藏 = 是;

细胞。TimeLabel1.text = TimeArray[indexPath.row];

}

否则{

细胞。聊天标签2.隐藏 = 否;

细胞。ChatLabel2.text = [Replychat[indexPath.row] valueForKey:@"User"];

细胞。聊天标签1.隐藏 = 是;

细胞。图像视图标签1.隐藏 = 是;

细胞。TimeLabel2.text = TimeArray[indexPath.row];

}

返回细胞;

}

最新更新