kur*_*ple 32 uitableview detailtextlabel swift
细节(副标题)文本不会出现.但是,这些数据是可用的,因为当添加println()调用时,它会向控制台输出带有预期数据的Optional("data").在故事板中,UITableViewController设置为正确的类,Table View Cell Style设置为"Subtitle",重用标识符设置为"cell".如何显示字幕信息?
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
}
Run Code Online (Sandbox Code Playgroud)
wpe*_*rse 31
同样的问题(从我读过的,也许是iOS 8中的一个错误?),这就是我们解决它的方法:
从故事板中删除原型单元格
删除此行:
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
let cellIdentifier = "Cell"
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: cellIdentifier)
}
更新Swift 3.1,Xcode版本8.3.3:
let cellIdentifier = "Cell"
var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.value2, reuseIdentifier: cellIdentifier)
}
Run Code Online (Sandbox Code Playgroud)
let cell = UITableViewCell(style: UITableViewCell.CellStyle.value2, reuseIdentifier: "cellId")
Run Code Online (Sandbox Code Playgroud)
如果以编程方式执行此操作而不在接口构建器中使用单元格,则此代码的工作方式类似于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
}
Run Code Online (Sandbox Code Playgroud)
如果在尝试将文本设置为非 nil 值时将文本设置为 nil,则包含文本的实际视图将丢失。这是在 iOS8 中引入的。尝试改为设置为空白@" "字符。
试试这个对我有用(swift 5)
let cell = UITableViewCell(style: .value1, reuseIdentifier: "cellId")
cell.textLabel.text = "Déconnexion"
cell.imageView.image = UIImage(named: "imageName")
Run Code Online (Sandbox Code Playgroud)
目标c:
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellId"];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39588 次 |
| 最近记录: |