【问题标题】:Subtitle TableView Cell副标题 TableView 单元格
【发布时间】:2015-06-15 14:07:00
【问题描述】:

我正在尝试使用字幕样式在单元格标题中获取两个文本。我试过 "cell.textLabel!.text = data.valueForKeyPath("divelocation, divenumber") as?String" 没有运气。

谁能帮忙?谢谢

   override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell {


    // Configure the cell...

    let CellID: NSString = "Cell"
    var cell: UITableViewCell = tableView?.dequeueReusableCellWithIdentifier(CellID as String)! as! UITableViewCell

    if let ip = indexPath{

        var data: NSManagedObject = myList[ip.row] as! NSManagedObject
        cell.textLabel!.text = data.valueForKeyPath("divelocation, divenumber") as? String

        var ddate = data.valueForKeyPath("divedate") as! String
        var dnumber = data.valueForKeyPath("divenumber") as! String

        cell.detailTextLabel!.text = "\(dnumber) date: \(ddate)"
    }
    return cell
}

【问题讨论】:

  • 发生了什么?您是否正确看到了textLabel,而只有detailTextLabel 没有出现?您在原型单元格中指定了哪种单元格类型?
  • 如果我有 cell.textLabel!.text = data.valueForKeyPath("divelocation") 作为?字符串然后一切正常,我只想在潜水位置旁边添加潜水编号。我在原型单元格中使用字幕单元格
  • @Scubadivingfool 你的关键路径不应该是“divelocation.divenumber”吗?
  • 好的,让我们退后一步。你问题的标题说副标题有问题(即detailTextLabel)。但是在阅读了您的评论后,我正在重新阅读您的问题,看来问题出在textLabel 而不是detailTextLabel。因此,请编辑您的问题并澄清问题到底是什么。坦率地说,keypath divelocation, divenumber 在我看来非常可疑:你想在那里做什么?那是两个独立的字段吗?为什么不像divedatedivenumber 那样处理它?
  • 不相关,但为什么表视图和索引路径变量是可选的?您可以省去解开非可选选项的麻烦。大声笑。

标签: ios tableview cell subtitle


【解决方案1】:

您必须将样式设置为UITableViewCellStyle.Subtitle:

override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell {


// Configure the cell...

let CellID: NSString = "Cell"
var cell: UITableViewCell = tableView?.dequeueReusableCellWithIdentifier(CellID as String)! as! UITableViewCell

if let ip = indexPath{

    var data: NSManagedObject = myList[ip.row] as! NSManagedObject
    var divelocation = data.valueForKeyPath("divelocation") as! String
    var divenumber = data.valueForKeyPath("divenumber") as! String
    cell.textLabel!.text = "\(divelocation)" + "\(divenumber)"

    var ddate = data.valueForKeyPath("divedate") as! String
    var dnumber = data.valueForKeyPath("divenumber") as! String

    cell.detailTextLabel!.text = "\(dnumber) date: \(ddate)"
}
return cell
}

【讨论】:

  • 抱歉,我没有看到您将如何设置标签中的文本。
  • @Scubadivingfool 我明白了,编辑了答案。希望有帮助
  • cell.textLabel!.text = "\(divelocation), \(divenumber)"
【解决方案2】:

当初始化单元格时,你必须像这样初始化

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

【讨论】:

  • 说一个“必须”这样做有点过分。是的,如果您不使用原型并且不使用标识符注册 NIB 或类,则必须这样做。坦率地说,这种手动实例化单元格的模式不再是常见的做法(尽管如果你想这样做的话完全可以接受)。不用说,这不太可能是 OP 的问题,否则他的强制解包会导致他的应用程序崩溃,如果发生这种情况,他肯定会告诉我们。
  • 好的,以编程方式让您管理单元格的所有创建,nib 有时会做一些奇怪的事情:P
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-14
相关资源
最近更新 更多