Swift Foundation--tableview Exercises

Source: Internet
Author: User

Novice Practice Swift Version TableView

Viewcontroller.swift

Swift_lianxi

Created by Dllo on 16/1/7.

COPYRIGHT©2016 year Z_han. All rights reserved.

//

Import UIKit

Class Viewcontroller:uiviewcontroller, Uitableviewdatasource, uitableviewdelegate {

var tableview:uitableview?

var dataarray:nsmutablearray?

Override Func Viewdidload () {

Super.viewdidload ()

Additional setup after loading the view, typically from a nib.

Self.dataarray = Nsmutablearray ()

TableView = Uitableview.init (Frame:self.view.frame, Style:UITableViewStyle.Plain)

TableView?. delegate = Self

TableView?. DataSource = Self

Self.view.addSubview (tableview!)

TableView?. RegisterClass (tableviewcell.self, Forcellreuseidentifier: "Tableviewcell")

TableView?. RowHeight = 110

Self.getdata ()

}

Func GetData () {

Let Manager:afhttpsessionmanager = Afhttpsessionmanager ()

Supported Interface types

Manager.responseSerializer.acceptableContentTypes = Nsset (objects: "Text/plain", "Text/json", "Application/json", " Text/javascript "," text/html ") as? Set<string>

Manager. GET ("http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php", Parameters:nil, Progress:nil, Success: {(Tast:nsurlsessiondatatask, responseobject:anyobject?), Void in

Print (Responseobject)

Let responseobject:nsdictionary = Responseobject as! Nsdictionary

Let Resultarray:nsarray = Responseobject.objectforkey ("result") as! Nsarray

Print (Resultarray)

For DIC in resultarray{

Let resultdic:nsdictionary = dic as! Nsdictionary

Let Model:model = Model ()

Assign value

Model.setvaluesforkeyswithdictionary (Resultdic as![ String:anyobject])

Self.dataarray?. AddObject (model)

}

Self.tableview?. Reloaddata ()

}) {(Task:nsurlsessiondatatask, error:nserror), Void in

Print ("Failed \ (Error)")

}

}

Func TableView (Tableview:uitableview, Numberofrowsinsection section:int), Int {

Return (Self.dataarray?. Count)!;

}

Func TableView (Tableview:uitableview, Cellforrowatindexpath indexpath:nsindexpath), UITableViewCell {

Let Cell:tableviewcell? = Tableview.dequeuereusablecellwithidentifier ("Tableviewcell") as? Tableviewcell

Let model = self.dataarray! [Indexpath.row]

Cell?. Model = model as? Model

Cell?. Selectionstyle = Uitableviewcellselectionstyle.none

Return cell!

}

func TableView (Tableview:uitableview, Didselectrowatindexpath indexpath:nsindexpath) {

Let DETAILVC:D Etailviewcontroller? = Detailviewcontroller ()

Self.presentviewcontroller (detailvc!, Animated:true, Completion:nil)

    }

Tableviewcell.swift

Swift_lianx

//

Created by Dllo on 16/1/7.

COPYRIGHT©2016 year Z_han. All rights reserved.

//

Import UIKit

Class Tableviewcell:uitableviewcell {

var imagevieg:uiimageview?

var titlelabel:uilabel?

var bgimage:uiimageview?

var model:model? {

Set (Newmodel) {

Let Url:nsurl = Nsurl.init (string: (Newmodel?. PIC_URL)!!)!

Self.imagevieg?. Sd_setimagewithurl (URL, placeholderimage:uiimage (named: "Picholder")!)

Self.titlelabel?. Text = Newmodel?. Moviename

}

get{

Return Self.model

}

}

Override Init (Style:uitableviewcellstyle, reuseidentifier:string?) {

Super.init (Style:style, Reuseidentifier:reuseidentifier)

Background

Bgimage = Uiimageview.init (Image:uiimage (named: "Bg_eventlistcell"))

Bgimage?. frame = CGRectMake (10, 10, 375-20, 100)

Self.contentView.addSubview (bgimage!)

Image

Imagevieg = Uiimageview.init (Image:uiimage (named: "Picholder"))

Imagevieg?. frame = CGRectMake (10, 10, 60, 80)

Bgimage?. Addsubview (imagevieg!)

Title

Titlelabel = Uilabel.init (Frame:cgrectmake (90, 20, 375-10-10-60-20, 60))

Bgimage?. Addsubview (titlelabel!)

}

Required init? (Coder Adecoder:nscoder) {

FatalError ("Init (coder:) have not been implemented")

}

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

}

Swift Foundation--tableview Exercises

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.