Reputation: 1601
Is it possible to assign two values to a cell in a UITableView?
I have a json file that is structured like this:
{
"band": [
"Name": "The Kooks",
"id": "1258"
]
}
I can get the label to display in the cell and pass it to a new view controller, but how do I also assign the id so that I can pass that too?
I am new to swift so please dont eat me.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
items is empty, so I get it like so:
Alamofire.request(.GET, testurl, parameters: ["bandName": bandName])
.responseJSON { response in
switch response.result {
case .Success:
if let value = response.result.value {
let json = JSON(value)
for (_,bands) in json {
for (_,bname) in bands {
let bandName = bname["Name"].stringValue
print(bandName)
self.items.append(bandName)
self.tableView.reloadData()
}
}
}
case .Failure(let error):
print(error)
}
}
Upvotes: 1
Views: 221
Reputation: 1693
you should not add every value in bname Dictionary to self.items. Try add bname to self.items,code:
Alamofire.request(.GET, testurl, parameters: ["bandName": bandName])
.responseJSON { response in
switch response.result {
case .Success:
if let value = response.result.value {
let json = JSON(value)
for (_,bands) in json {
for (_,bname) in bands {
self.items.append(bname)
self.tableView.reloadData()
}
}
}
case .Failure(let error):
print(error)
}
}
and in cellForRowAtIndexPath use it: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
if let dic = self.items[indexPath.row] as? NSDictionary{
if let id = dic["id"] as? String{
cell.idLabel.text = id
}
if let name = dic["Name"] as? String{
cell.nameLabel.text = name
}
}
return cell
}
Upvotes: 1
Reputation: 538
Create class or model with these properties and assign values in objects keep object in NSarray that works as datasource get object from datasource using selected indexpath and pass object to your new viewcontroller using prepareForSegue.
Upvotes: 0