0

请帮我设置UITableViewCell高度。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    UILabel *l0, *l1;
    if (cell == nil) {

        CGRect f = tableView.frame;
        float sc0 = pow(dt.PHI_1, 3.f);
        float sc1 = 1. - sc0;

        CGRect z =CGRectMake(0, 0, 0, 0);//f.size.height
        CGRect r =CGRectMake(0, 0, f.size.width, f.size.height / 26.f);
        CGRect r0 =CGRectMake(0, 0, f.size.width * sc0, f.size.height / 26.f);
        CGRect r1 =CGRectMake(f.size.width * sc0, 0, f.size.width * sc1, f.size.height / 26.f);

        cell = [[UITableViewCell alloc] initWithFrame:r]
        l0 = [[UILabel alloc] initWithFrame:r0];
        l0.tag = 100;
        [cell addSubview:l0];
        l1 = [[UILabel alloc] initWithFrame:r1];
        l1.tag = 101;
        [cell addSubview:l1];
        [cell.textLabel setFrame:z];
        [cell.imageView setFrame:z];
        [cell setFrame:r];
        [cell.backgroundView setFrame:r];
    } else {
        l0 = (UILabel *)[self.view viewWithTag:100];
        l1 = (UILabel *)[self.view viewWithTag:101];
    }
    cell.backgroundColor = [[UIColor alloc]initWithWhite:.0f alpha:0.f];

    cell.imageView.hidden = YES;
        cell.textLabel.hidden = YES;
        [l0 setText:@"B"];
        l0.backgroundColor = [UIColor yellowColor];

        [l1 setText:@"TEXT"];
        l1.backgroundColor = [UIColor redColor];

        return cell;
}

这段代码给出了这个:

在此处输入图像描述

这样的方法对我没有帮助:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGRect f = tableView.frame;

    return f.size.height / 26.f;
}

所以,如果我将高度设置得更小。也就是说,f.size.height / 26.f我有一个透明的酒吧。

UITableView定义:

table = [[UITableView alloc] initWithFrame:CGRectMake(x,y,w,h)];
table.dataSource = self;
[table setTag:1];
[self.view addSubview:table];

请为我提供一种设置单元高度的方法。如果我设置了非常大的高度,UITableViewCell它也不会被修改 - 只显示顶部。

4

3 回答 3

0

为单元格创建自定义类并使用此类而不是UITableViewCell. - (void)awakeFromNib {}并在自定义单元格类中设置您的单元格的属性。尝试这个。我认为这可能对你有用。:)

于 2016-04-16T07:46:43.323 回答
0

最好使用带有自动布局的自定义 UITableViewCell 来创建 UI 并将这些行添加到 viewDidLoad

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension 
于 2016-04-16T07:50:32.073 回答
0

谢谢大家。我找到了解决方案。

  1. 使用 storeboard 声明 UITableView
  2. 使用以下代码定义高度。

编码:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        CGRect f = tableView.frame;
        return f.size.height / 26.f;
    }

我不明白,但是当我以编程方式声明表时它不起作用。

于 2016-04-16T09:56:16.653 回答