2016-02-03 53 views
1

是否可以将两个值分配给UITableView中的单元格?Swift将两个值分配给UITableViewCell

我有一个结构类似的JSON文件:

{ 
    "band": [ 
     "Name": "The Kooks", 
     "id": "1258" 
    ] 
} 

我可以得到标签在单元格中显示并把它传递到一个新的视图控制器,但我怎么也分配ID,以便我也可以传递?

我是新来的快,所以请不要吃我。

编辑1:

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 
} 

项目是空的,所以我得到它像这样:

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) 
      } 

    } 
+0

请用String (格式:“String%@%@”,变量); –

+0

erm?谨慎解释? – JamesG

+0

您不会将其分配给单元格。您将ID保存在您的模型中,然后通过'prepareForSegue'中的选定单元格的'indexPath'来检索它。 –

回答

0

这些属性创建类或模型和对象赋值保存对象的NSArray的作品因为数据源使用选定的索引路径从数据源获取对象,并使用prepareForSegue将对象传递给新的视图控制器。

+0

不,这是Objective-C不快。 – MarkP

+0

是的好我忘记你是对的。 –

1

您不应该将bname Dictionary中的每个值添加到self.items。 尝试BNAME添加到self.items,代码:

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) 
     } 

} 

和的cellForRowAtIndexPath使用它: 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 
} 
+0

cell.idLabel:类型'UITableViewCell'的值没有成员'idLabel' – JamesG

+0

同名错误为nameLabel – JamesG

+0

idLabel&nameLabel就是我的例子,您可以显示它: cell.textLabel?.text =“\(id)& \(名称)” –

相关问题