【发布时间】:2011-11-09 12:33:17
【问题描述】:
我有一个 UITableView,当一个单元格具有与其他单元格相同的内容时,此内容仅出现在绘制的 las 单元格中。我的自定义单元格添加了一个 UIView 属性来添加来自其他类的动态子视图。
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"cell";
CollectionCell *cell = (CollectionCell *)[tableView
dequeueReusableCellWithIdentifier:MyIdentifier];
if (!cell) {
cell = [[[CollectionCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:MyIdentifier]
autorelease];
}
[cell setCollectionView:/* Generated view in other class */];
return cell;
}
具体问题是:
我的动态视图由例如 2 个 UILabel 组成:
- 如果标签 1 是标题,则每一行的标题都是唯一的 -> 没问题,渲染良好。
- 如果标签 2 是一个类别,则从 0 到 5 的索引具有相同的类别 -> 只有索引 5 处的行显示类别标签。
我无法在单元格实例化中创建此标签并添加为子视图,因为单元格内容都是动态的。
感谢您的时间和帮助。
更新:
我无法在单元格实例化中创建此标签并添加为子视图,因为单元格内容都是动态的。
我来详细解释一下:
添加到 collectionView 属性的内容和 UI 控件在每次执行时可能会有所不同。在一次执行中,collectionView 可能有一个 UIImageView 和一个 UILabel,下一次执行它有 2 个 UILabel(例如)。这就是为什么我不能创建这样的东西
if (!cell) {
cell = [[[CollectionCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:MyIdentifier]
autorelease];
UILabel *foo = [[UILabel alloc] initWithFrame:SomeFrame];
[foo setTag:101];
[cell.collectionView addSubview:foo];
}
UILabel *foo = [cell.collectionView subviewWithTag:101];
[foo setTitle:@"This content is dynamic"];
谢谢!
更新 2:
似乎是自定义 UILabel 子类的问题。如果我使用原始 UILabel 显示字符串效果很好。
【问题讨论】:
-
“我无法在单元格实例化中创建此标签并添加为子视图,因为单元格内容都是动态的。”请对此进行扩展,并显示您正在设置的集合视图。 cellForRowAtIndex path 是设置动态内容的地方。
标签: objective-c ios uitableview