【问题标题】:Resize UILabel in a UITableViewCell在 UITableViewCell 中调整 UILabel 的大小
【发布时间】:2011-11-30 23:01:22
【问题描述】:

我正在使用此代码输出一个带有 UILabel 的 tableView,它可以复制列的外观。

目前有 2 个标签,但我要添加第三个标签,因此我需要将它们缩小一点,以便所有 3 个标签都适合。

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

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

    - (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;
    }

【问题讨论】:

  • 究竟是什么问题?
  • 如何调整代码,以便可以容纳 3 列而不是 2 列。

标签: iphone objective-c uitableview uilabel


【解决方案1】:

当您使用cellForRowAtIndexPath 时,您需要更改您的编码风格。虽然您使用for 循环的实现看起来简单而美观,但实际上,只需向cell.contentView 添加值而不进行迭代要简单得多。我建议您可以只创建三个UILabel 字段,例如label1label2label3。您可以为每个index path 提供文本,最后将它们作为子视图添加到cell.contentView

这真的很简单,您将能够看到每行 3 个标签。

注意:使用此方法,您需要完美设置 3UILabels 的帧大小,以免弄乱它们的外观。

更好的方法:

上面的实现是最简单的,但是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];

}

此实现没有错误,而且非常简单且逻辑上很棒(一旦掌握了它,您就可以对任何结构进行子类化并创建自定义视图)。

【讨论】:

  • 感谢莱戈拉斯。你描述的方式是我自己编码的方式。但是问题中的代码是我正在修改的其他人的代码,只是为了添加第三列,所以我不想重写所有内容。所有 3 列现在都在工作,我只需要减小它们的水平尺寸以便我可以看到它们。
  • 我明白了。将UITableViewCell 子类化,添加具有适当字体大小和框架宽度的标签,我认为它们现在应该看起来不错。 :)
  • legolas 我不太确定是否要覆盖layoutSubviews。如果我在其中放置一个日志,则每次将一个单元格放在屏幕上时都会调用它,这对于在对象的整个生命周期中只需要发生一次的事情来说听起来不是很有效。你不应该在init 中添加子视图吗?当然,我可能是愚蠢的,做错事让layoutSubviews一直被调用。
  • 它们应该添加到-init。当您想根据视图的状态(即编辑)为子视图指定新框架时,您将覆盖 -layoutSubviews-layoutSubviews 中的任何更改都会自动生成动画。
  • @MarkAdams 哈哈。感谢您在我出现之前输入此内容:) @Paul.s 关于 init 方法的详细信息有点明显。这完全取决于您对uitableView 的要求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多