Iphone 在UITableView中,cell.detailTextLabel.text不是';t工作…;为什么?
在Iphone 在UITableView中,cell.detailTextLabel.text不是';t工作…;为什么?,iphone,uitableview,Iphone,Uitableview,在tableView:cellforrowatinexpath:中,我有以下内容: cell.textLabel.text = @"label"; cell.detailTextLabel.text = @"detail"; textLabel按预期显示,但是detailTextLabel根本不显示,尽管没有诊断。我所期望的是,“细节”文本将出现在单元格的第二行,低于“正常”文本,可能字体较小 这里的另一篇帖子也提出了同样的问题,用户“jbrennan”回答说tableview单元格样式必须不
tableView:cellforrowatinexpath:
中,我有以下内容:
cell.textLabel.text = @"label";
cell.detailTextLabel.text = @"detail";
textLabel
按预期显示,但是detailTextLabel
根本不显示,尽管没有诊断。我所期望的是,“细节”文本将出现在单元格的第二行,低于“正常”文本,可能字体较小
这里的另一篇帖子也提出了同样的问题,用户“jbrennan”回答说tableview单元格样式必须不是UITableViewCellStylePlain
。但是,似乎只有两种可能的样式,UITableViewCellStylePlain
和uitableviewcellstylegroup
。我用这两种方法都得到了相同的结果(细节标签不显示)
是否有我在文档中没有看到的其他单元格样式?上次更新中是否有UITableView
更改,并且detailtextlab
不再可用?我需要做些额外的事情才能让它出现吗?有什么建议吗
我正在使用xcode 3.2.5并构建iPhone 4.2模拟器。您的初始化需要更改为:
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle
重用标识符:CellIdentifier]autorelease]代码>
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
我已经强调并加粗了你需要改变的部分 分配单元格类型时,需要将其设置为Subtitle
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:BasicCellIdentifier];
}
使用Xcode 4.2时,将“表格视图”单元格样式设置为“故事板中的字幕”dequeueReusableCellWithIdentifier将返回一个实例化的单元格。在情节提要中将表格视图单元格样式设置为Subtitle
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
并编写此代码来配置您的单元
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:BasicCellIdentifier] autorelease];
}
Swift 3:
如果要使单元格覆盖“cellForRowAt indexPath”函数:
let tableContent = ["1", "2", "3", "4", "5"]
let tableDetailContent = ["a", "b", "c", "d", ""]
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "yourCellIdentifier")
cell.textLabel?.text = tableContent[indexPath.row]
cell.detailTextLabel?.text = tableDetailContent[indexPath.row]
return cell
}
上面的代码部分允许您设置detailTextLabel。。您可以在序列图像板上为表格视图单元格样式(自定义、基本、右细节、左细节、副标题)设置所需的任何内容,此行将覆盖它:
style: UITableViewCellStyle.subtitle
对于Swift 4,尽管技术与任何版本(或Obj-C)相同-
我知道派对有点晚了,但很多答案让事情变得比实际情况复杂得多
假设您使用的是故事板,您只需将故事板中的表格视图单元格样式设置为“Subtitle”,然后-
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myIdentifier", for: indexPath)
cell.textLabel?.text = "Title"
cell.detailTextLabel?.text = "Subtitle..."
return cell
}
完全不需要使用代码来实例化单元格,只需照常重用即可。确保设置默认文本内容,并且宽度足够宽,可以实际显示文本 对!!非常感谢。我曾尝试将样式更改为字幕,但我更改了UITableViewStyle而不是UITableViewCellStyle,并且没有编译,因此我认为字幕样式不再可用。哈哈!我们在编码时都会犯愚蠢的错误。我认为,如果苹果禁用字幕样式,将会引发骚乱。这似乎每次都会初始化一个新的UITableViewCell,而不是从堆栈中退出以保留内存。似乎重用UITableViewCellStyleSubtitle样式单元格的唯一方法是子类化、重写初始值设定项并调用通常的[tableView dequeueReusableCellWithIdentifier:@“ReuseIdentifier”]代码>有人能确认或否认这一点吗?请参阅关于initWithStyle:reuseIdentifier:
:以防有人想要@AurumAquila答案的Swift 4版本:let cell=UITableViewCell(style:UITableViewCell.CellStyle.subtitle,reuseIdentifier:“cell”)
谢谢@agrippa。我们还需要这行代码吗:let cell=tableView.dequeueReusableCell(带有标识符:“cell”,for:indexath)
?对!非常感谢。请参阅我对先前答案的评论。我希望我能给你+100多简单的答案。围绕这个问题有很多复杂的答案。但这个答案显然是swift4的出路!非常感谢。您如何在不使用故事板的情况下以编程方式注册subtitle类型的单元格。只需将UITableViewCell子类化,并在init声明中调用super,强制样式为subtitle。像这样:重写init(style:UITableViewCell.CellStyle,reuseIdentifier:String?{super.init(style:.subtitle,reuseIdentifier:reuseIdentifier)}然后注册您的自定义单元格
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier: CellIdentifier];