【问题标题】:Set Multiple UILabel in a row of UITable在一行UITable中设置多个UILabel
【发布时间】:2012-08-27 05:42:51
【问题描述】:

我想在表格的行中设置UILabel,但问题是应用重启时每次标签的数量不同。 清除我要创建具有动态行和列的表的更多详细信息。行是自己设置的,但是在列中我设置的标签也是动态的,那么每次新的 Web 服务调用时如何设置该标签?

例如

行/列

A1 b11 b12 .......b1n

A2 b21 b22 .......b2n

A3 b31 b32 .......b3n . .

bm1 bm2 .......bmn

其中 A 是行或 b 是列

【问题讨论】:

  • 我..只是不明白这个问题...

标签: objective-c ios uitableview uilabel


【解决方案1】:

例如,您可以有一个名为 rowData 的可变数组。在您的 init/initWithCoder/etc 方法中,您可以创建另一个名为 columnData 的可变数组,例如它可以包含每个标签的数据。将每一行的 columnData 对象添加到 rowData。

对于您的 cellForRowAtIndexPath 方法,您可以执行以下操作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

if (!cell) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] autorelease];

    int row = [indexPath row];
    int i = 0;
    int nLabels = [[rowData objectAtIndex:row] count];
    UILabel *label;

    NSLog([NSString stringWithFormat:@"%d labels", nLabels]);

    for (i = 0; i < nLabels; i++) {
        NSLog([NSString stringWithFormat:@"%d", i]);
        label = [[[UILabel alloc] initWithFrame:CGRectMake(cell.frame.origin.x + (i * 40), cell.frame.origin.y, 40, cell.frame.size.height)] autorelease]; //replace 40 with desired label width
        label.textColor = [UIColor grayColor];
        label.backgroundColor = [UIColor clearColor];
        // set the label text here

        [cell.contentView addSubview:label];

        NSLog([NSString stringWithFormat:@"%d subviews", [[cell.contentView subviews] count]]);
    }
}

return cell;

}

【讨论】:

  • 当一行被选中时,如何获取这些标签的字符串值?
【解决方案2】:

更好的选择是在 UITableViewCell 中创建一个 UITableView 并且你的每个标签都是单元格,你可以禁用内部 tableview 滚动。但是你需要确保你已经正确计算了内部 tableview 的高度。

添加 n 个 UIlabel 等其他解决方案不会让您平滑滚动。

【讨论】:

    【解决方案3】:

    创建具有您可能需要的最大标签数的单元格。使他们的标签为 1,2,3,4... - 增加数字。每次要求您在表格旁提供一个单元格时,您要么取一个旧单元格,要么取一个新单元格,决定需要多少标签,使该范围的标签可见(即 label.hidden = NO;),然后隐藏另一个标签。您显然需要更改可见标签的文本。

    【讨论】:

    • 前段时间我使用过类似的方法,只是我动态添加了标签。标签的添加就像带有自动换行的文本一样。
    【解决方案4】:

    你使用tag方法和indexpath.row。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
                if (!cell) {
                          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
                          for (i = 0; i < n; i++) {
                                  label = [[UILabel alloc] initWithFrame:CGRectMake (i * yourWidth, 0, yourWidth + 5, yourHight)]; 
                                  label.textColor = [UIColor blueColor];
                                  label.tag = (indexPath.row)*100 + i  //you can create n labels in a row.
                                  label.text = [NSString stringWithString:@"%d",label.tag]; 
                                  [cell.contentView addSubview:label];
                            }
                }
    
            return cell;
    
    } 
    

    我认为这对你会有帮助。

    【讨论】:

      【解决方案5】:

      您应该考虑使用不同的视图,例如DTGridView

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-04-22
        • 2018-05-29
        • 1970-01-01
        • 2015-07-26
        • 1970-01-01
        • 2017-03-28
        • 1970-01-01
        相关资源
        最近更新 更多