大家,我需要在tableView单元格中插入一些文本。但是我不知道如何更改行距。所以看起来很丑。因为每一行都太近了。请帮我。非常非常感谢!

//texts
UITextView *hisText = [[UITextView alloc] init];
hisText.frame = CGRectMake(100, 110, 600, 300);
[hisText setFont:[UIFont fontWithName:@"HelveticaNeue" size:14]];
hisText.textColor = [UIColor blackColor];
hisText.editable = NO;

switch (indexPath.row) {
    case 0:
        hisText.text = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.";
        hisImg.image = [UIImage imageNamed:@"1928-1.jpg"];
        [cell.contentView addSubview:hisText];
        //[cell.contentView addSubview:hisImg];
        break;
    case 1:
        hisText.text = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.";
        [cell.contentView addSubview:hisText];
        break;
    default:
        break;
}

最佳答案

如果所有文本的长度都适合您创建的UITextView框架,则只需在heightForRowAtIndexPath中执行以下操作:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
      return 110; //you can set to whatever height you wish if you want some 'padding'
 }


但是,执行此操作以使其适合任何长度的文本的一种正确方法是在heightForRow中创建UITableView并适当调整其大小以适合文本(可能使用sizeToFit),然后将其取为tableView.frame.size.height和返回tableView行高的那个值。

就像是:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
   UITextView *hisText = [[UITextView alloc] init];
   hisText.frame = CGRectMake(100, 110, 600, 300);
   [hisText setFont:[UIFont fontWithName:@"HelveticaNeue" size:14]];

   switch (indexPath.row) {
    case 0:
        hisText.text = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium    adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.";
         [hisText sizeToFit];
         break;
    case 1:
        hisText.text = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.";
        [hisText sizeToFit];
        break;
    default:
        break;
    }

    float theHeight = hisText.frame.size.height;

    return theHeight;

}

关于ios - 如何在TableViewCell中更改行空间,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20995817/

10-12 06:21