Now we can display the JSON data from the server in the previous section to the interface, which we use UITableView to display.
First we customize a UITableViewCell, named Newscell, the following steps:
This will get the following file:
Well, after the cell is finished, we start initializing the UITableView
//tableView
tabNewList.delegate=self
tabNewList.dataSource=self
var nib = UINib(nibName:"NewsCell", bundle: nil)
self.tabNewList?.registerNib(nib, forCellReuseIdentifier: identifier)
Fitting UITableView
1 func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
2 // #warning Potentially incomplete method implementation.
3 // Return the number of sections.
4 return 1
5 }
6
7
8 func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat
9 {
10 return 104
11 }
12
13 func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
14 // #warning Incomplete method implementation.
15 // Return the number of rows in the section.
16 return jsonArrStories.count
17 }
18
19
20 func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
21
22 var cell = tableView?.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as? NewsCell
23 var index = indexPath!.row
24 var data = self.jsonArrStories[index] as NSDictionary
25
26 var sTitle=data.objectForKey("title") as String
27 cell!.NLabContent.text=sTitle
28 var arrImgURL=data.objectForKey("images") as NSArray
29 var imagURL=arrImgURL[0] as String
30 println("imageURL:\(imagURL)")
31 cell!.NImg.setImage(imagURL,placeHolder: UIImage(named: "001p9BkFgy6KqjPYZg74b&690.jpeg"))
32
33 return cell
34 }
Line 24th gets data from the previous server
The 25th to 31st line sets the title, content, and picture separately.
Cell Start Click event
1 func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!)
2 {
3 var index = indexPath!.row
4 var data = self.jsonArrStories[index] as NSDictionary
5 var aId : Int=data.objectForKey("id") as Int
6 var board:UIStoryboard = UIStoryboard(name:"Main", bundle:nil);
7 var detailConrol=board.instantiateViewControllerWithIdentifier("KDNewsDetailController") as KDNewsDetailController
8 detailConrol.aId=aId
9 println("detailConrol.id\(aId)")
10 // self.showViewController(detailConrol, sender: self)
11 self.presentModalViewController(detailConrol, animated: true)
12 }
Line 3rd to 5th gets the ID number of the currently clicked News
Line 6th to 7th gets the storyboard with the name main and initializes the Kdnewsdetailcontroller class with the Instantiateviewcontrollerwithidentifier method.
Finally, when it gets data from the network, it can add Uiactivityindicatorview to control the user's operation.
Swift learns the sixth day of the project-informed daily client (ii) interface development UITableView