Swift learns the sixth day of the project-informed daily client (ii) interface development UITableView

Source: Internet
Author: User



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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.