如何使单元格在ios中保存多个文本标签

时间:2015-11-21 23:53:34

标签: swift uitableview parse-platform

我的手机持有同名或用户名,但即使我在故事板中的单元格上有文本标签,它也没有同时保存这两个值     覆盖init(style:UITableViewStyle,className:String!){         super.init(style:style,className:className)     }

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
}

1 个答案:

答案 0 :(得分:0)

您应该使用声明的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
}