Swift Basics-Using Tableviewcontroller to customize lists

Source: Internet
Author: User

first set up a swift project, delete the contents of storyboard, add a navigation Controller, and then set the class of storyboard corresponding interface, in navigation The Controller interface sets the is initial view controller for the view controller, which uses the custom list content, so create a new class that inherits UITableViewCell. Then set the prototype cells class for table view in storyboard, and use the Prepareforsegue method in TableView for clicking on the item to enter the details screen .

Jietableviewcontroller.swift

jietableviewcontroller.swift//jietableview////Created by Jiezhang on 14-10-5.//Copyright (c) 2014 Jiezhang. All rights Reserved.//import Uikitclass Jietableviewcontroller:uitableviewcontroller {var listvideos:nsmutablearray        ! Override Func Viewdidload () {super.viewdidload () var bundle = Nsbundle.mainbundle () Let Plistpath: string! = Bundle.pathforresource ("Videos", OfType: "plist") Listvideos = Nsmutablearray (contentsoffile:plistpath)/ /Uncomment the following line to preserve selection between presentations//Self.clearsselectiononviewwillappear = False//Uncomment the following line to display a Edit button in the navigation bar for this view contr        Oller. The following paragraph is set to edit on the left, add Item Self.navigationItem.leftBarButtonItem = Self.editbuttonitem () to the right to let AddButton = Uibarbuttonitem (BARBUTTONSYSTEMITEM:UIBARBUTTONSYSTEMITEM.ADD, target:self, Action: "Insertnewobject:") sElf.navigationItem.rightBarButtonItem = AddButton} func insertnewobject (sender:anyobject) {var ite M:nsdictionary = Nsdictionary (objects:["http://c.hiphotos.baidu.com/video/pic/item/ F703738da977391224eade15fb198618377ae2f2.jpg "," New Data ", Nsdate.date (). Description], Forkeys: [" video_img "," Video_ Title "," Video_subtitle "]) Listvideos.insertobject (item, atindex:0) Let Indexpath = Nsindexpath (forrow:0, I nsection:0) self.tableView.insertRowsAtIndexPaths ([Indexpath], withrowanimation:. Automatic)} override func didreceivememorywarning () {super.didreceivememorywarning ()//Dispose    of any resources the can be recreated. }//MARK:-Number of Table view data source//Return section override Func Numberofsectionsintableview (Tableview:uitableview)-& Gt        Int {//#warning potentially incomplete method implementation.        Return the number of sections. Return 1}//Returns the number of rows in a section override Func TableView (tabLeview:uitableview, Numberofrowsinsection section:int), Int {//#warning incomplete method implementation.        Return the number of the rows in the section. Return Listvideos.count}//Provides data for table view cells, which is a method that must be implemented override Func TableView (Tableview:uitableview, Cellforrowati Ndexpath Indexpath:nsindexpath), UITableViewCell {Let cellidentifier:string = ' videoitem ' let cell = Tableview.dequeuereusablecellwithidentifier (Cellidentifier, Forindexpath:indexpath) as JieTableViewCell var row = indexpath.row var rowdict:nsdictionary = Listvideos.objectatindex (row) as Nsdictionary let url:string        = Rowdict.objectforkey ("video_img") as String let Dataimg:nsdata = NSData (Contentsofurl:nsurl (String:url)) Cell. Jievideoimg.image = UIImage (data:dataimg) cell. Jievideotitle.text = Rowdict.objectforkey ("Video_title") as? String cell. Jievideosubtitle.text = Rowdict.objectforkey ("Video_subtitle") as? String return cell} override func TableView (Tableview:uitableview, Didselectrowatindexpath indexpath:n  Sindexpath) {}//Support cell editing function override Func TableView (Tableview:uitableview, Caneditrowatindexpath Indexpath:        Nsindexpath), Bool {//Return NO If you don't want the specified item to be editable.    return true}//Override to support editing the table view. Override Func TableView (Tableview:uitableview, Commiteditingstyle Editingstyle:uitableviewcelleditingstyle, Forrowatindexpath indexpath:nsindexpath) {if Editingstyle = =.            Delete {//delete the row from the data source Listvideos.removeobjectatindex (Indexpath.row) Tableview.deleterowsatindexpaths ([Indexpath], withrowanimation:. Fade)} else if Editingstyle = =. Insert {//Create a new instance of the appropriate class, insert it into the array, and add a new row to the         Table View}}   /*//Override to support rearranging the table view. Override Func TableView (tableview:uitableview!, Moverowatindexpath fromindexpath:nsindexpath!, Toindexpath: nsindexpath!)    {} */*//Override to support conditional rearranging of the table view. Override Func TableView (tableview:uitableview!, Canmoverowatindexpath indexpath:nsindexpath!), Bool {//Ret        Urn NO If you don't want the item to be re-orderable. return true} *//MARK:-Navigation//pass value to the newly entered interface override Func Prepareforsegue (segue:uistoryboards Egue, sender:anyobject!)                {if Segue.identifier = = "ShowDetail" {if let Indexpath = Self.tableView.indexPathForSelectedRow () { Let object:nsdictionary = Listvideos[indexpath.row] as nsdictionary (Segue.destinationviewco Ntroller as Jiedetailviewcontroller). Detailitem = Object}}}

Jietableviewcell.swift

  jietableviewcell.swift//  jietableview////  Created by Jiezhang on 14-10-5.//  Copyright (c) 2014 Jiezhang. All rights Reserved.//import Uikitclass Jietableviewcell:uitableviewcell {    @IBOutlet weak var jievideoimg: uiimageview!    @IBOutlet weak var jievideotitle:uilabel!    @IBOutlet weak var jievideosubtitle:uilabel!        Override Func awakefromnib () {        super.awakefromnib ()        //initialization code    }    override Func SetSelected (Selected:bool, Animated:bool) {        super.setselected (selected, animated:animated)               }}
Jiedetailviewcontroller.swift

jiedetailviewcontroller.swift//jietableview////Created by Jiezhang on 14-10-5.//Copyright (c) 2014 Jiezhang. All rights Reserved.//import Uikitclass Jiedetailviewcontroller:uiviewcontroller {@IBOutlet var big_video_img:ui    imageview!            Accept the value passed in Var detailitem:nsdictionary? Func Configureview () {if let detail:nsdictionary = self.detailitem {self.title = Detail.objectforkey ("Video_title") as? string Let url:string = Detail.objectforkey ("video_img") as a string let Dataimg:nsdata = NSData (c Ontentsofurl:nsurl (string:url)) Self.big_video_img.image = UIImage (data:dataimg)}} Overr  IDE Func viewdidload () {super.viewdidload () Configureview ()} override func didreceivememorywarning ()    {super.didreceivememorywarning ()//Dispose of any of the resources that can is recreated. }//MARK:-Navigation override func Prepareforsegue (Segue:uistoryboaRdsegue, sender:anyobject!) {           }}









Source Address: Https://github.com/jwzhangjie/JieTableView

Swift Basics-Using Tableviewcontroller to customize lists

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.