2
我做了一个NSArray与NSDictionary对象包含从api下载的内容。我还在main.storyboard上创建了一个带有UIImage标签和两个文本标签作为其内容的原型单元格的tableview对象。 如何将数据从数组放到表中,以便每个与我的原型样式相同的单元格显示数组中NSDictionary的内容。如何在Swift中使用多种内容类型的字典创建表格?
我做了一个NSArray与NSDictionary对象包含从api下载的内容。我还在main.storyboard上创建了一个带有UIImage标签和两个文本标签作为其内容的原型单元格的tableview对象。 如何将数据从数组放到表中,以便每个与我的原型样式相同的单元格显示数组中NSDictionary的内容。如何在Swift中使用多种内容类型的字典创建表格?
您必须实现UITableViewDataSource方法
记住的tableView的源属性设置为视图控制器
比你从数组,并设置电池标签和ImageView的得到一个对象(你的NSDictionary)与它的数据。
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell
这里是完整的代码示例在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
}
}
有什么,你已经试图解决这个问题?如果你能从你身边表现出一些努力,它通常会更好地被社区接受。 – Erik 2014-10-19 18:10:31
解析非结构化数据是当前迅速比较烦人的任务之一。看看一些项目,如[swiftyJSON](https://github.com/SwiftyJSON/SwiftyJSON)的想法。 – cmyr 2014-10-19 18:15:24
是的,我尝试搜索到处寻找类似问题的解决方案,但他们都没有为我工作,我正在为它工作2-3小时。我是Swift的初学者。 – 2014-10-19 18:18:30