UITableViewCell不显示detailTextLabel.text-Swift
未显示详细信息(字幕)文本。但是,数据是可用的,因为当添加println()调用时,它会将可选(“数据”)打印到控制台,并显示预期的数据。在情节提要中,UITableViewController设置为适当的类,表视图单元格样式设置为“Subtitle”,重用标识符设置为“Cell”。如何获取要显示的字幕信息UITableViewCell不显示detailTextLabel.text-Swift,uitableview,swift,detailtextlabel,Uitableview,Swift,Detailtextlabel,未显示详细信息(字幕)文本。但是,数据是可用的,因为当添加println()调用时,它会将可选(“数据”)打印到控制台,并显示预期的数据。在情节提要中,UITableViewController设置为适当的类,表视图单元格样式设置为“Subtitle”,重用标识符设置为“Cell”。如何获取要显示的字幕信息 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
dispatch_async(dispatch_get_main_queue(), { () -> Void in
cell.textLabel.text = self.myArray[indexPath.row]["title"] as? String
cell.detailTextLabel?.text = self.myArray[indexPath.row]["subtitle"] as? String
println(self.myArray[indexPath.row]["subtitle"] as? String)
// The expected data appear in the console, but not in the iOS simulator's table view cell.
})
return cell
}
这里也有同样的问题(据我所知,可能是iOS 8中的一个bug?),我们就是这样解决的:
var cell=tableView.dequeueReusableCellWithIdentifier(“cell”,forIndexPath:indexPath)作为UITableViewCell
let cell = UITableViewCell(style: UITableViewCell.CellStyle.value2, reuseIdentifier: "cellId")
如果仍要使用故事板中的原型单元,请选择TableViewcell样式作为副标题。它将起作用。如果在尝试将文本设置为非零值时,在某个位置将文本设置为零,则包含文本的实际视图将丢失。这是在iOS8中引入的。请尝试将设置为空空格
@“
字符
请参见:您的代码看起来不错。只需转到情节提要并选择tableview的单元格->现在转到属性检查器并选择要添加字幕的样式 按照下面的屏幕截图进行操作
希望对您有所帮助。如果在界面生成器中没有单元格的情况下以编程方式执行此操作,则此代码在Swift 2.0中非常有用+
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
yourTableView.delegate = self
yourTableView.dataSource = self
yourTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "subtitleCell")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return yourTableViewArray.count
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell: UITableViewCell = yourTableView.dequeueReusableCellWithIdentifier("subtitleCell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = "the text you want on main title"
cell.detailTextLabel?.text = "the text you want on subtitle"
return cell
}
值得一提的是:我的问题是细节没有出现。这是因为我已经注册了tableView单元,我不应该注册,因为单元原型是直接在故事板中定义的。 试试这个,它对我有用(swift 5) 目标c:
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellId"];
下面是swift 5的工作原理,使用detailtextlabel,在视图控制器中使用UITableView对象来获取字幕,如果您缺少其中任何一个,它将无法工作,并且可能会崩溃
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
在viewDidLoad中:
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "subtitleCell")
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if cell == nil {
cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell")
}
cell?.textLabel?.text = "title"
cell?.textLabel?.numberOfLines = 0
cell?.detailTextLabel?.text = "Lorem ipsum"
cell?.detailTextLabel?.numberOfLines = 0
return cell ?? UITableViewCell()
委托职能:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Fetch a cell of the appropriate type.
let cell = UITableViewCell(style: .subtitle , reuseIdentifier: "subtitleCell")
// Configure the cell’s contents.
cell.textLabel!.text = "Main Cell Text"
cell.detailTextLabel?.text = "Detail Cell Text"
return cell
}
xcode 11
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
let cell = UITableViewCell(style: UITableViewCell.CellStyle.value1, reuseIdentifier: "reuseIdentifier")
cell.detailTextLabel?.text = "Detail text"
cell.textLabel?.text = "Label text"
// Configure the cell...
return cell
}
在Xcode11和Swift5中,我们必须执行以下操作。 如果我们通过检查条件单元格==nil,然后使用cellStyle创建UITableViewCell来执行此操作,那么它将不起作用。下面的解决方案对我有效
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: cellIdentifier)
cell?.textLabel?.text = "Title"
cell?.detailTextLabel?.text = "Sub-Title"
return cell!
}
上面的一些解决方案并不完全正确。因为单元格应该重用,而不是重新创建。您可以更改init方法
final class CustomViewCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: .value1, reuseIdentifier: reuseIdentifier)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
带字幕文本的Swift 5,无需在viewDidLoad中注册您的单元格:
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "subtitleCell")
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if cell == nil {
cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell")
}
cell?.textLabel?.text = "title"
cell?.textLabel?.numberOfLines = 0
cell?.detailTextLabel?.text = "Lorem ipsum"
cell?.detailTextLabel?.numberOfLines = 0
return cell ?? UITableViewCell()
谢谢在重新编写代码之后,“Subtitle”选项起作用了。当它是常规viewcontroller中的tableview时,这对我不起作用,但在TableViewController中起作用。我猜是奇怪的ios8/swift错误。@CodyMace:不,它适用于您使用的所有表视图。您可能遗漏了其他内容。可能受到iOS8中引入的“优化”的影响?使用Xcode:Version6.4(6E35b)在TableView中对我有效这是我的问题,但当您将其设置为“”和nil时,就会发生这种情况。所以你必须把它设为“”。因此,基本上isEmpty也会导致这种行为。@Naishta真的很高兴它能为您工作……顺便说一句,我在使用Swift 2.0时发布了这篇文章,定义新的单元格将占用大量内存?。我认为
dequeueReusableCellWithIdentifier
将通过重用cellRight帮助保留内存。更新到匹配这仍然是一个问题!在您的答案周围添加一些详细信息。此代码不会使tableView的单元格出列。每次调用cellForRowAt时,您只需创建单元格。这应该是正确的解决方案,因为单元格应该被重用。
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if cell == nil {
cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell")
}
cell?.textLabel?.text = "title"
cell?.textLabel?.numberOfLines = 0
cell?.detailTextLabel?.text = "Lorem ipsum"
cell?.detailTextLabel?.numberOfLines = 0
return cell ?? UITableViewCell()