将细胞与UITableViewCell对齐的问题

时间:2016-11-24 00:26:51

标签: ios objective-c uitableview

问题在于,当加载表格的滚动时,文本位于左侧,当完成加载时,滚动向右移动,您可以看到位置的变化,这看起来很糟糕,我查看其他答案,但没有为我工作。

Problems aligning text UITableViewCell

Aligning text in UITableView

Centro Alinear texto en el problema UITableViewCell

我在tableView中有一个普通的单元格,当我想将文本对齐时,我使用:

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

  

加载滚动

loaded scroll

0 个答案:

没有答案