Swift Basics-Using Tableviewcontroller to define your own list

Source: Internet
Author: User

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

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 sets the edit to the left. On the right, add Item Self.navigationItem.leftBarButtonItem = Self.editbuttonitem () Let AddButton = Uibarbuttonit EM (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: Nsindexpath) {}//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) {if fromindexpath! = toindexpath{var object:anyobject = Listvideos.objectatindex (fromInd Expath.row) Listvideos.removeobjectatindex (fromindexpath.row) if Toindexpath.row > Self.listvideo s.count{Self.listVideos.addObject (object)}else{self.listVideos.insertObject (ob Ject, AtIndex:toIndexPath.row)}}}//Override to support conditional rearranging of th e table View. In the edit state. Ability to drag the set item position override Func TableView (Tableview:uitableview, Canmoverowatindexpath indexpath:nsindexpath), Bool { Return 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:uistoryboardseg UE, 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!) {           }}

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvand6agfuz2ppzq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvand6agfuz2ppzq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>







watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvand6agfuz2ppzq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>


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

Swift Basics-Using Tableviewcontroller to define your own list

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.