UITableViewCell initWithStyle:UITableViewCellStyleSubtitle无效 [英] UITableViewCell initWithStyle:UITableViewCellStyleSubtitle is not working
问题描述
我在尝试在单元格中显示信息时遇到问题,一个在左侧,另一个在右侧。我知道在 UITableViewCellStyleSubtitle
中使用 initWithStyle
。我使用它但它似乎不起作用。
I'm having an issue in trying to display info in a cell, one on the left and one on the right. I'm aware using initWithStyle
with UITableViewCellStyleSubtitle
. I use this but it doesn't seem to work.
以下是一些示例代码:
- (UITableViewCell *)tableView:(UITableView *)ltableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Account Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Cellidentifier];
}
Accounts *account = [self.fetchedResultsController objectAtIndexPath];
cell.textLabel.text = account.name;
cell.detailTextLabel.text = @"Price";
return cell;
}
我可以显示cell.textLabel.text就好了,但我无法得到简单的价格显示。我尝试了不同的东西,比如设置 cell.detailTextLabel
的字体大小。
I can display cell.textLabel.text just fine, however I cannot get the simple "Price" to be displayed. I've tried different things, such as setting the font size of cell.detailTextLabel
.
我也试过 UITableViewCellStyleValue1
,正如有些人在旧帖子中所建议的那样。
设置为Price后抛出NSLog,将cell.detailTextLabel显示为null。
I've also tried UITableViewCellStyleValue1
as some had suggested in older posts.
Threw NSLog after setting to "Price", shows cell.detailTextLabel as null.
不确定我做错了什么。
编辑:我发现: cell.detailTextLabel.text为NULL
如果我删除 if(cell == nil)
它可以工作...
检查应该到位,那么在使用不同的样式时如何使其工作?
If I remove if (cell == nil)
it works...
That check should be in place, so how do you make it work when using the different styles?
推荐答案
使用故事板时原型单元格,一个单元格始终从dequeue方法返回(假设存在具有该标识符的原型)。这意味着你永远不会进入(cell == nil)
块。
When using storyboards and prototype cells, a cell is always returned from the dequeue method (assuming a prototype with that identifier exists). This means you never get into the (cell == nil)
block.
在您的情况下,原型单元格未在具有字幕样式的故事板中定义,因此从不使用字幕单元格,并且细节文本标签不存在。将故事板中的原型更改为具有字幕样式。
In your case the prototype cell is not defined in the storyboard with the subtitle style, so a subtitled cell is never used, and the detail text label does not exist. Change the prototype in the storyboard to have the subtitle style.
这篇关于UITableViewCell initWithStyle:UITableViewCellStyleSubtitle无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!