0

カスタムセルがあります。その中には 2 つのラベルがあります。nameLabel の高さと呼ばれるものの 1 つは、動的に変更する必要があります。場合によっては 1、2、または 3 行になることもあります。しかし、列は互いに重なっており、それぞれの列の線を横切っています。どうすればこの問題を解決できますか?

ラベルとカスタム セル オブジェクトの[自動レイアウトを使用] オプションが無効になっています。ラベルの高さは動的に変更する必要があり、次に CustomCell のものです。そして、tableRow。頭が混乱しています。また、CustomCell の背景色が変化していることがわからないのはなぜですか?

ありがとう

これが私のコードです:

- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UILabel *label = [[UILabel alloc] init];
    label.numberOfLines = 0; // allows label to have as many lines as needed
    label.text = [[self.dataList objectAtIndex:indexPath.row] objectForKey:@"NAME"];
    CGSize labelSize = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(320, 63) lineBreakMode:NSLineBreakByWordWrapping];
    CGFloat h = labelSize.height;
    NSInteger x=0.0;
    if (h==63.0) x=30;
    if (h==42.0) x=20;
    if (h==21.0) x=10;

    return h+30;
}

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

    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:cellID];    
    if (cell == nil)
    {
        cell = (CustomCell*)[[[NSBundle mainBundle]
                              loadNibNamed:@"CustomCell" owner:nil options:nil]
                             lastObject];
    }    
    // customization
    NSDictionary *d = [self.dataList objectAtIndex:indexPath.row];

    cell.nameLabel.text = [d objectForKey:@"NAME"];
    cell.cityLabel.text = [d objectForKey:@"CODE"];
    cell.indexPath = indexPath;    

    return cell;
}

このリンクには、問題を簡単に理解するためのカスタム セルのシミュレーターと xib の画像があり ます。動的/

4

1 に答える 1