gpt4 book ai didi

swift - detailTextLabel 不会在 ios 8 swift 中更新

转载 作者:行者123 更新时间:2023-11-30 14:16:34 24 4
gpt4 key购买 nike

我有一个简单的表格 View ,它使用 UITableViewController 和 Subtitle 样式。我无法成功更新detailTextView 字段。我在 Stack Overflow 上读到,ios 8 中存在潜在的错误,但所有建议的解决方法都不适合我。当我打印该字段的值时,我得到零。任何帮助,将不胜感激。下面是下面的代码,后面是 println 语句的结果。我看到另一个 SO 条目列出了 ios 8 中的一个错误,但建议的解决方法都不适合我。我也在苹果论坛上提出了这个问题

import UIKit

class TableViewController: UITableViewController {

override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "Test"
self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as!UITableViewCell
let xrow = indexPath.row
let xsection = indexPath.section
//if (xrow == 0) && (xsection == 0) {
// initializing with hard values initially
cell.textLabel?.text = "initial"
cell.detailTextLabel?.text = "Initial"
//}

println("row = \(xrow), section = \(xsection)")
println("0: cell.textLabel?.text = \(cell.textLabel?.text)")
cell.textLabel?.text = "Section = \(xsection)"
println("1: cell.textLabel?.text = \(cell.textLabel?.text)")
println("0: cell.detailTextLabel?.text = \(cell.detailTextLabel?.text)")
cell.detailTextLabel?.text = ("section = \(xsection), row = \(xrow)") as String!
println("1: cell.detailTextLabel?.text = \(cell.detailTextLabel?.text)")
return cell
}

}


row = 0, section = 0
0: cell.textLabel?.text = Optional("initial")
1: cell.textLabel?.text = Optional("Section = 0")
0: cell.detailTextLabel?.text = nil
1: cell.detailTextLabel?.text = nil
row = 1, section = 0
0: cell.textLabel?.text = Optional("initial")
1: cell.textLabel?.text = Optional("Section = 0")
0: cell.detailTextLabel?.text = nil
1: cell.detailTextLabel?.text = nil
row = 2, section = 0
0: cell.textLabel?.text = Optional("initial")
1: cell.textLabel?.text = Optional("Section = 0")
0: cell.detailTextLabel?.text = nil
1: cell.detailTextLabel?.text = nil

最佳答案

因为如果单元格样式不支持,detailTextLabel 总是返回 nil

UITableViewCell detailTextLabel reference

所以改变

tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as!UITableViewCell

var cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier") as? UITableViewCell
if cell == nil{
cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "CellIdentifier")
}

关于swift - detailTextLabel 不会在 ios 8 swift 中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31098985/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com