1

I want to display a label showing a number in each cell of the tableview but the label is only visible when I click on a row (when the cell is highlited)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UILabel *label;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        label = [[UILabel alloc] initWithFrame:CGRectMake(200,10, 15, 15)];
        label.tag = 1;
        [cell.contentView addSubview:label];
        [label release];
    }
    else {
        label = (UILabel *)[cell viewWithTag:1];   
    }
    if (indexPath.row == 0) {
        cell.textLabel.text = @"Photos";
        label.text = [NSString stringWithFormat:@"%d",1];
    }
    return cell;
}
4

4 回答 4

4

我有同样的问题,通过在将自定义标签添加为子视图之前设置 textlabel 的文本来解决。

...
cell.textLabel.text = @"X";
...
[cell.contentView addSubview:label]
于 2010-07-27T03:24:53.160 回答
1

当您更新textLabela 的属性时UITableViewCell,它会延迟创建 aUILabel并将其添加到单元格的子视图中。通常你不会使用textLabel和添加子视图的组合contentView,但如果你这样做,你需要确保textLabel视图没有放在contentView子视图的顶部。

于 2009-09-16T04:24:36.197 回答
0

首先,我假设这是针对 3.0。Apple 已经改变了 UITableViewCells 在 3.0 中的创建方式,你应该继续这样做。-initWithFrame:reuseIdentifier:已弃用。

也就是说,一个可能的问题是内置textLabel干扰了您添加的标签,可能是重叠的。您应该首先查看其中一种新的内置样式是否直接满足您的需求。如果不是,我建议要么只使用您自己的视图,要么只使用内置视图,可能重新排列它们。如果您想重新排列它们,Apple 建议将单元格子类化并重载-layoutSubviews. 我也相信这-tableView:willDisplayCell:forRowAtIndexPath:是在没有子类化的情况下进行最终单元布局的好地方。

于 2009-09-16T04:39:56.277 回答
0

Using a custom UITableViewCell gives you more control over the layout of a cell. Add custom views to the cell's contentView in the subclass and override the layoutSubviews to set the order of the subviews:

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.contentView bringSubviewToFront:self.yourCustomView];
}
于 2015-12-21T13:06:39.490 回答