【问题标题】:How to make a cell hold multiple text labels in ios如何让一个单元格在ios中保存多个文本标签
【发布时间】:2015-11-21 23:53:33
【问题描述】:

我的单元格包含大学名称或用户名,但它没有同时包含这两个值,即使我在情节提要中的单元格上有文本标签 覆盖初始化(样式:UITableViewStyle,类名:字符串!){ super.init(风格:风格,类名:类名) }

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    // Configure the PFQueryTableView
    self.parseClassName = "_User"
    self.textKey = "username"
    self.pullToRefreshEnabled = true
    self.paginationEnabled = false
}

override func queryForTable() -> PFQuery {
    var query = PFQuery(className: "_User")
    query.orderByAscending("username")
    return query
}

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


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
    let cellIdentifier = "cell"

    var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? PFTableViewCell
    if cell == nil {
        cell = PFTableViewCell(style: .Default, reuseIdentifier: cellIdentifier)
    }
    if let usernametable = object?["username"] as? String {
        cell?.textLabel?.text = usernametable
    }
    if let collegename = object?["collegename"] as? String {
       cell?.textLabel?.text = collegename
    }

    return cell
}

【问题讨论】:

  • 您正在设置cell?.textLabel?.text = usernametable,然后覆盖标签文本cell?.textLabel?.text = collegename。你能显示你的PFTableViewCell 代码吗?
  • 除了声明文本标签之外,我在单元格上没有任何代码,我需要添加什么才能使其工作?
  • 你应该使用声明的UILabels var 而不是cell?.textLabel
  • 不客气,我写了它作为答案。

标签: swift uitableview parse-platform


【解决方案1】:

您应该使用声明的UILabels var 而不是cell?.textLabel

在您的自定义单元格中,您可能有类似

@IBOutlet weak var usernameLabel: UILabel!
@IBOutlet weak var collegeLabel: UILabel!

所以不要使用

if let usernametable = object?["username"] as? String {
    cell?.usernameLabel.text = usernametable
}
if let collegename = object?["collegename"] as? String {
   cell?.textLabel?.text = collegename
}

您应该使用自定义单元格的UILabels

if let usernametable = object?["username"] as? String {
    cell?.usernameLabel.text = usernametable
}

if let collegename = object?["collegename"] as? String {
   cell?.collegeLabel.text = collegename
}

【讨论】:

    猜你喜欢
    • 2022-01-17
    • 2017-05-09
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    相关资源
    最近更新 更多