Ios 使用swift下载json并在UITableView中显示
我参加了一次初级iOS工作面试的测试。这是为了从twitch.tv获取最新的游戏,并在UITableView上用自定义手机显示它们。下面是代码和注释。除了使用库获取json之外,还有人能指出错误吗。我认为数据模型可以更好,不使用三个不同的表Ios 使用swift下载json并在UITableView中显示,ios,json,swift,uitableview,Ios,Json,Swift,Uitableview,我参加了一次初级iOS工作面试的测试。这是为了从twitch.tv获取最新的游戏,并在UITableView上用自定义手机显示它们。下面是代码和注释。除了使用库获取json之外,还有人能指出错误吗。我认为数据模型可以更好,不使用三个不同的表 import UIKit class ViewController: UIViewController, UITableViewDelegate { @IBOutlet weak var tableView: UITableView! //This i
import UIKit
class ViewController: UIViewController, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
//This is a the quick fix, another way would be to create a data class with objects and properties: name, viewers and image.
//Then create an arrray of objects where there are stored. Although the implemented solution is a quick faster and has a little les memory usage.
//setting up different arrays for each data type to access them globally at some point
var JSONGameName: [String] = []
var JSONViewers: [Int] = []
var JSONGameImages: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
loadAndDecodeJson()
}
//this functions uses the default way to implement json in swift. For more advanced data or multiple json files
//the suggestions is to use an external library like SwiftyJson
func loadAndDecodeJson(){
//defining the url of the nsurl string to download
//if not secure URL allow it in the plist file
let url = NSURL(string: "https://api.twitch.tv/kraken/games/top")
//create the task to handle the url
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) -> Void in
//when task completes this will run
//need to check if data is not nil
if error == nil {
//serialize the json to a dictionary
do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
//loop through the data but first check if it hsa something in it
if jsonResult.count > 0 {
if let items = jsonResult["top"] as? NSArray {
for item in items {
if let viewers = item["viewers"] as? NSInteger {
print(viewers)
self.JSONViewers.append(viewers)
}
if let games = item["game"] as? NSDictionary {
if let name = games["name"] as? NSString {
print (name)
self.JSONGameName.append(name as String)
}
if let logo = games["logo"] as? NSDictionary {
if let small = logo["small"] as? NSString {
print (small)
self.JSONGameImages.append(small as String)
}
}
}
}
}
}
//reload table after json parsing is done
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
})
} catch {
print("error: parsing json")
}
} else {
//Show error message that there is probably no connection
let alert = UIAlertController(title: "Oopps..", message: "The was an error downloading data", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
//execute the async task
task.resume()
}
//defining number os section
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
//function to determine the number of list items
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return JSONGameName.count
}
//the view of every cell of the list
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//custom cell identifier
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ViewControllerCell
//setting up the images and casting them from strings to UIImages
let imageURL = NSURL(string: JSONGameImages[indexPath.row])
let data = NSData(contentsOfURL: imageURL!)
//setting game name, images and viewers on cells
cell.gameName?.text = JSONGameName[indexPath.row]
cell.gameViewers?.text = String (JSONViewers[indexPath.row])
cell.gameImage?.image = UIImage(data: data!)
return cell
}
}
这是定制单元
import UIKit
class ViewControllerCell: UITableViewCell {
@IBOutlet weak var gameImage: UIImageView!
@IBOutlet weak var gameName: UILabel!
@IBOutlet weak var gameViewers: 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
}
}
附言:我无缘无故地投了一些反对票。这个问题有什么问题?我有一个更好的解决方案和头脑风暴
import UIKit
class ViewController: UIViewController, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
//This is a the quick fix, another way would be to create a data class with objects and properties: name, viewers and image.
//Then create an arrray of objects where there are stored. Although the implemented solution is a quick faster and has a little les memory usage.
//setting up different arrays for each data type to access them globally at some point
var JSONGameName: [String] = []
var JSONViewers: [Int] = []
var JSONGameImages: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
loadAndDecodeJson()
}
//this functions uses the default way to implement json in swift. For more advanced data or multiple json files
//the suggestions is to use an external library like SwiftyJson
func loadAndDecodeJson(){
//defining the url of the nsurl string to download
//if not secure URL allow it in the plist file
let url = NSURL(string: "https://api.twitch.tv/kraken/games/top")
//create the task to handle the url
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) -> Void in
//when task completes this will run
//need to check if data is not nil
if error == nil {
//serialize the json to a dictionary
var errorJson:NSError?=nil
var jsonResult=NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &errorJson) as? NSDictionary
//loop through the data but first check if it hsa something in it
if jsonResult!.count > 0 {
if let items = jsonResult!["top"] as? NSArray {
for item in items {
if let viewers = item["viewers"] as? NSInteger {
print(viewers)
self.JSONViewers.append(viewers)
}
if let games = item["game"] as? NSDictionary {
if let name = games["name"] as? NSString {
print (name)
self.JSONGameName.append(name as String)
}
if let logo = games["logo"] as? NSDictionary {
if let small = logo["small"] as? NSString {
print (small)
self.JSONGameImages.append(small as String)
}
}
}
}
}
}
//reload table after json parsing is done
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
})
} else {
//Show error message that there is probably no connection
let alert = UIAlertController(title: "Oopps..", message: "The was an error downloading data", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
//execute the async task
task.resume()
}
//defining number os section
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
//function to determine the number of list items
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return JSONGameName.count
}
//the view of every cell of the list
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//custom cell identifier
var cell:CustomTableViewCell? = tableView.dequeueReusableCellWithIdentifier("Cell") as? CustomTableViewCell
cell=nil
if cell==nil{
cell=NSBundle.mainBundle().loadNibNamed("CustomTableViewCell", owner: self, options: nil)[0] as? CustomTableViewCell
}
//setting up the images and casting them from strings to UIImages
let imageURL = NSURL(string: JSONGameImages[indexPath.row])
let data = NSData(contentsOfURL: imageURL!)
//setting game name, images and viewers on cells
cell!.gameName?.text = JSONGameName[indexPath.row]
cell!.gameViewers?.text = String (JSONViewers[indexPath.row])
cell!.gameImage?.image = UIImage(data: data!)
return cell!
}
}
从下面的链接下载源代码,它在我这边工作。如果有任何错误,请告诉我?
您最好创建一个名为Game的类,然后将名称、查看器和图像作为该类的属性。然后,您可以将一组游戏对象([Game])作为tableview的数据源。这将是更好的数据模型。我确实尝试过,但在这样做时,我得到了一个数组越界错误。也许我把密码弄错了。你能在主类上设置它吗?你能解释一下你对社区的期望吗?当然,这是我对下载json并在UIViewTable中显示它的需求的解决方案。我被拒绝了,因为其他候选人的代码更好。所以我在问,如何才能做得更好、更高效等等。这是针对swift 1.2的,不推荐使用。除了N错误,我看不到任何重大变化。