meowsush
meowsush

Reputation: 197

Label disappear when table is reloaded / scrolled

I have a UITableview with two UILabels like in the chat. When the visible UITableview is filled and when I add extra cells, my UILabels get disappeared.

There are may other UILabels which is visible. Only those 2 chat UILabels is hidden. Can anyone suggest me the possible solution?

- (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;

}

Upvotes: 0

Views: 580

Answers (3)

Rajesh Dharani
Rajesh Dharani

Reputation: 285

Try this way

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

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

if (cell == nil) {

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

}

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.hidden = NO;

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.hidden = NO;

cell.ChatLabel2.text = [Replychat[indexPath.row] valueForKey:@"User"];

cell.ChatLabel1.hidden = YES;

cell.ImageviewLabel1.hidden = YES;

cell.TimeLabel2.text = TimeArray[indexPath.row];

}

return cell;

}

Upvotes: 0

Nguyen Hoan
Nguyen Hoan

Reputation: 1693

try:

   //....
    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)
   //....

Upvotes: 0

meowsush
meowsush

Reputation: 197

Labels in custom TableView cells disappearing after scrolling

My code has an if else statement where one branch can set cell.ChatLabel2.hidden = YES; but the other branch does not set cell.ChatLabel2.hidden = NO;. So, once the label is hidden it will never be un-hidden. When the cell with the hidden label is reused the label remains hidden.

Add cell.ChatLabel2.hidden = NO; (and any other 'inverse' configuration required) to your if statement.

Upvotes: 1

Related Questions