如何更改TableView单元格中的行间距

时间:2014-01-08 12:34:58

标签: ios line tableview cell space

每个人,我都需要在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;
}

3 个答案:

答案 0 :(得分:1)

如果所有文本的长度都适合您创建的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;

}

答案 1 :(得分:0)

1)取代UITextView取UILabel,根据文本大小调整UILabel的框架。

现在,如果您希望行空间根据您的要求增加UILabel的框架,它将为您提供行之间的空间。

2)或者使用UIWebeview而不是UITextView,并且您可以根据HTML正文提供行间距。

答案 2 :(得分:0)

U可以增加行的高度。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [indexPath row]+110;
}  

这里,[indexPath row] - 具有rowheight的默认值