我有一个带有自定义Prototype Cell的tableview控制器。该单元格包含2个标签。我试图从Parse类返回2个值。一个字段称为Notes,是一个String值。另一个字段称为CreditAmount,是一个数字值。我很难在tableview中返回数值(Credit Amount)。
以下是tableview控制器的代码:
导入UIKit 导入Parse 进口基金会
class TableViewController:UITableViewController {
var note = [String]()
var credit = [NSInteger]()
var refresher:UIRefreshControl!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.reloadData()
updateNotes()
self.refresher = UIRefreshControl()
self.refresher.attributedTitle = NSAttributedString(string: "Pull to refresh")
self.refresher.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
self.tableView.addSubview(refresher)
}
func updateNotes() {
let query = PFQuery(className: "Paydown")
query.findObjectsInBackgroundWithBlock({ (objects:[AnyObject]?, error: NSError?) -> Void in
self.note.removeAll(keepCapacity: true)
self.credit.removeAll(keepCapacity: true)
if let objects = objects as? [PFObject] {
for object in objects {
// var noted = object as PFObject
self.note.append(object["Notes"] as! String)
self.credit.append(object["CreditAmount"] as! NSInteger)
}
self.tableView.reloadData()
} else {
//println(error)
}
self.refresher.endRefreshing()
})
}
func refresh() {
updateNotes()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return note.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! cell
myCell.notesLabel.text = note[indexPath.row]
myCell.amountLabel.text = credit[indexPath.row]
return myCell
}
}
以下是我的客户单元格的代码:
导入UIKit import Parse
类单元格:UITableViewCell {
@IBOutlet weak var notesLabel: UILabel!
@IBOutlet weak var amountLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
myCell.amountLabel.text = credit [indexPath.row]导致错误:无法将类型为NSInteger(aka int)的值赋给String类型的值。如何让号码字段起作用?
答案 0 :(得分:1)
更新
myCell.amountLabel.text = credit[indexPath.row]
要
myCell.amountLabel.text = "\(credit[indexPath.row])"