The problem is that when loading the scroll of the table the text is on the left and when finished loading the scroll goes to the right and you can see the change in position, this looks bad and I looked into other answer but nothing worked for me.
Problems aligning text UITableViewCell
Centro Alinear texto en el problema UITableViewCell
I have a normal cell in tableView, when I want to align it text to right I use:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *mensaje = [self.mensajes objectAtIndex:indexPath.row];
NSString *tipoFR = [mensaje objectForKey:@"emisor"];
NSString *idUsuario = @"10";
if ([tipoFR isEqualToString:idUsuario]) {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSString *chat = [NSString stringWithFormat:@" %@ ", [mensaje objectForKey:@"mensaje"]];
//Move text to the right
[self performSelector:@selector(alignText:) withObject:cell afterDelay:0.0];
cell.textLabel.textAlignment = NSTextAlignmentRight;
cell.detailTextLabel.textAlignment = NSTextAlignmentRight;
cell.textLabel.lineBreakMode = NSLineBreakByTruncatingTail;
cell.detailTextLabel.text = @"";
cell.backgroundColor = [UIColor colorWithRed:(131 / 255.0 ) green:( 224 / 255.0 ) blue:( 140 / 255.0 ) alpha:1];
cell.textLabel.layer.cornerRadius = 10.0f;
[cell.textLabel setClipsToBounds:YES];
cell.textLabel.text = chat;
cell.textLabel.numberOfLines = 0;
return cell;
}else{
static NSString *CellIdentifier = @"Cellda";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textLabel.textAlignment = NSTextAlignmentLeft;
cell.detailTextLabel.textAlignment = NSTextAlignmentLeft;
cell.textLabel.lineBreakMode = NSLineBreakByTruncatingTail;
cell.detailTextLabel.text = @"";
cell.textLabel.text = [mensaje objectForKey:@"mensaje"];
cell.textLabel.numberOfLines = 0;
return cell;
}
}
- (void)alignText:(UITableViewCell*)cell{
CGRect frame = cell.textLabel.frame;
frame.origin.x = cell.frame.size.width - (frame.size.width + 10.0);
cell.textLabel.frame = frame;
frame = cell.detailTextLabel.frame;
frame.origin.x = cell.frame.size.width - (frame.size.width + 10.0);
cell.detailTextLabel.frame = frame;
[cell setNeedsDisplay];
}
loading scroll issue image
loading scroll