如何在Swift中使用多种内容类型的字典制作表格?

Sh.*_*h.v 2 xcode uitableview swift xcode6 ios8

我用NSDictionary对象创建了一个NSArray,其中包含从api下载的内容.我还在main.storyboard上创建了一个tableview对象,其中一个原型单元格带有UIImage标签,两个文本标签作为其内容.如何将数据从数组放到表中,以便每个与我的原型具有相同样式的单元格显示来自数组的NSDictionary的内容.

Kos*_*val 17

你必须实现UITableViewDataSource方法
记住的tableView的源属性设置为视图控制器
比你从数组一个对象(你的NSDictionary),并设置电池标签和ImageView的与它的数据.

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell  
Run Code Online (Sandbox Code Playgroud)

这是完整的代码示例Swift.Objective-C非常相似

class MasterViewController: UITableViewController {

   var objects = [
    ["name" : "Item 1", "image": "image1.png"],
    ["name" : "Item 2", "image": "image2.png"],
    ["name" : "Item 3", "image": "image3.png"]]

  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return objects.count
  }

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

    let object = objects[indexPath.row]

    cell.textLabel?.text =  object["name"]!
    cell.imageView?.image = UIImage(named: object["image"]!)
    cell.otherLabel?.text =  object["otherProperty"]!

    return cell
  }

}
Run Code Online (Sandbox Code Playgroud)