我已经在 Stack Overflow 和其他网站上梳理了数十个关于此的问题/答案,但我似乎无法让它发挥作用。这看起来是一个相当普遍的问题,但我发现的其他问题的答案都没有应用。基本上,我试图将一个简单的 UITextView 添加到表格单元格,但它没有显示出来。这是我得到的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
UITextView *textviewFactoid = nil;
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"cell"];
textviewFactoid = [[UITextView alloc]initWithFrame:CGRectMake(0, -3, 300, 25)];
[textviewFactoid setFont:[UIFont systemFontOfSize:12.0f]];
[textviewFactoid setBackgroundColor:[UIColor blueColor]];
[textviewFactoid setTextColor:[UIColor grayColor]];
[textviewFactoid setScrollEnabled:NO];
[textviewFactoid setEditable:NO];
[textviewFactoid setTag:98];
[[cell contentView] addSubview:textviewFactoid];
} else {
textviewFactoid = (UITextView*)[cell.contentView viewWithTag:98];
}
NSString *textviewFactoidText = @"Here is the factoid";
[textviewFactoid setText:textviewFactoidText];
return cell;
}
有谁知道为什么单元格会是空白的?我将 backgroundColor 设置为蓝色,这样我就可以查看 textview 是否存在,但它似乎不存在。我也没有收到任何错误或警告来帮助我查明问题所在。
我几乎从我编写的另一个应用程序中逐字复制了这段代码,并且在那里工作正常......我确信这是我遗漏或更改的简单内容,但我有点难过.
谁的眼光比我更敏锐?
最佳答案
如果您正在为 iOS 6 构建并且已经为您的单元格标识符注册了一个 nib 或类 dequeueReusableCellWithIdentifier
将始终返回一个单元格。您的 if (cell == nil) {
永远不会评估为 true 并在该 block 内运行代码。
关于ios - UITableViewCell 不显示 subview UITextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17223465/