在UITableViewCell中调整UILabel的大小

时间:2011-11-30 23:01:22

标签: iphone objective-c uitableview uilabel

我正在使用此代码输出带有UILabels的tableView,以复制列的外观。

目前有2个标签,但是我添加了第三个标签,所以我需要让它们稍微宽一点,这样所有3个标签都适合。

我注释掉了添加第3个“列”的2行,这些列将显示用户名文本。

如何调整代码,以便在UITableView中可以容纳3列而不是2列。

enter image description here

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *MyIdentifier = [NSString stringWithFormat:@"AuditGridCell %i", indexPath.row];
        GridTableViewCell *cell = (GridTableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
        if (cell == nil) {
            cell = [[[GridTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
            [(GridTableViewCell *)cell clearColumns];
        }

        int is_row_bold = 0;
        NSMutableArray *values = [[NSMutableArray alloc] init];
        float sizes[10] = {CELL_CONTENT_WIDTH, 250.0, 100.0, 125.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};

        if ([self interfaceOrientation] == UIInterfaceOrientationPortrait || 
            [self interfaceOrientation] == UIInterfaceOrientationPortraitUpsideDown) {
            sizes[0] = CELL_CONTENT_WIDTH_PORTRAIT;
            sizes[1] = 200.0;
        }

        float sum_total = 0.0;
        if (1) {
            int spacer_width = 5;
            UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, spacer_width, tableView.rowHeight)] autorelease];
            [cell.contentView addSubview:label];
            sum_total += spacer_width;
        }   

        if (0 == indexPath.row) {
            is_row_bold = 1;
    //        [values addObject:@"Username"];
            [values addObject:@"Description"];
            [values addObject:@"Date"];
        } else {
            int index = indexPath.row - 1;
   //       [values addObject:[[self.auditRecords objectAtIndex:index] valueForKey:@"username"]];
            [values addObject:[[self.auditRecords objectAtIndex:index] valueForKey:@"description"]];
            [values addObject:[[self.auditRecords objectAtIndex:index] valueForKey:@"date"]];
        }

        for (int it=0; it < [values count]; ++it) {
            UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(sum_total, 0.0, sizes[it], tableView.rowHeight)] autorelease];
            sum_total += sizes[it];
            [(GridTableViewCell*)cell addColumn:sizes[it]];
            label.text = [values objectAtIndex:it];

            [cell.contentView addSubview:label];
        }
        [values release];

        return cell;
    }

1 个答案:

答案 0 :(得分:0)

使用cellForRowAtIndexPath时,您需要更改编码风格。尽管使用for循环实现它看起来很简单,但事实上,只需在没有迭代的情况下向cell.contentView添加值就更简单了。我建议您只需创建三个UILabel字段,例如label1label2label3。您可以向他们提供每个index path的文字,最后将其作为子视图添加到cell.contentView

这非常简单,每行可以看到3个标签。

注意:使用此方法,您需要完美地设置3 UILabels的帧大小,这样您就不会弄乱它们的外观。

更好的方法:

上面的实现是最简单的,但是cell reusability存在问题,如果您使用该设置向上和向下滚动,您可以看到所有UILabels被覆盖并弄乱整个视图。除非您的uitableview小于当前视图并且您有unscrollable视图,否则此问题必然会发生〜。

<强>解决方案:

UITableViewCell进行子类化并创建自己的CustomUITableViewCell并在其中嵌入3 UILabels,并将其用于使用reusability实现tableView。 您需要覆盖layoutSubviews方法。

- (void)layoutSubviews {

// In this example we will never be editing, but this illustrates the appropriate pattern
[self.contentView addSubview:self.label1];
[self.contentView addSubview:self.label2];
[self.contentView addSubview:self.label3];
[super layoutSubviews];

}

这个实现是没有错误的,而且它非常简单且逻辑上很棒(一旦你掌握了它,你就可以继承任何结构并创建自定义视图。)