IOS SWIFT UITableView for a simple Weibo list

Source: Internet
Author: User

  weibo.swift//  uitableviewcellexample////  Created by Xuyan on 15/8/15.//  Copyright (c) 2015 Com.world. All rights Reserved.//import Foundationclass Weibo {    //attribute    var id:uint32    var img:string!    var username:string!    var mbtype:string!    var createdon:string!    var source:string!    var text:string!        Constructor    Init (Id:uint32,img:string,username:string,mbtype:string,createdon:string,source:string,text:stri NG) {        self.id = id        self.img = img        self.username = Username        self.mbtype = mbtype        Self.createdon = CreatedOn        Self.source = Source        self.text = text    }}

Customcell.swift custom Cells//uitableviewcellexample////Created by Xuyan on 15/8/15.//Copyright (c) 2015 Com.world . All rights reserved.//import Uikitimport Foundationclass customcell:uitableviewcell{//Avatar width, height var imgview1_w:cg Float = 50.0 var imgview1_h:cgfloat = 50.0 var imgview1:uiimageview! Avatar control var imgview2:uiimageview! Member type Var labelname:uilabel! User name Var labedate:uilabel! Date Var labelsource:uilabel! Client Var labelcontent:uilabel! Content var Weibo:weibo? Weibo content var height:cgfloat = 0.0//height override init (Style:uitableviewcellstyle, reuseidentifier:string!) {Super.init (Style:style, Reuseidentifier:reuseidentifier) Initview ()} func Initvie        W (), Void {//Avatar ImgView1 = Uiimageview () self.addsubview (imgView1)//user name LabelName = UILabel () Labelname.font = Uifont.systemfontofsize () Self.addsubview (LabElname)//member Type IMGVIEW2 = Uiimageview () self.addsubview (IMGVIEW2)//Date Labedate = UILabel () Labedate.font = Uifont.systemfontofsize () self.addsubview (labedate)// Client Labelsource = UILabel () Labelsource.font = Uifont.systemfontofsize (Self.addsubview) LABELSOURC e)//Content labelcontent = UILabel () Labelcontent.font = uifont.systemfontofsize (+) self. Addsubview (labelcontent)} func setData (Weibo:weibo), void{//set avatar size and position var Imgview        1_x:cgfloat = 10.0;        var imgview1_y:cgfloat = 10.0; var imgview1rect:cgrect = CGRectMake (imgview1_x, imgview1_y, Imgview1_w, imgview1_h) imgview1.image = UIImage (nam ed:weibo.img) Imgview1.frame = imgview1rect//Set user name LABEL size, location var labelname_x = Cgrectget MaxX (imgview1.frame) + 10.0//control spacing user name x point var labelname_y = imgview1_yUser name y dot labelname.numberoflines = 0 Labelname.linebreakmode = nslinebreakmode.bycharwrapping labelNam E.text = weibo.username var text1:nsstring = NSString (cstring:labelname.text!.        Cstringusingencoding (nsutf8stringencoding)!, encoding:nsutf8stringencoding)! var rect1 = text1.boundingrectwithsize (Cgsizemake (Self.frame.size.width, Cgfloat.max), Options: Nsstringdrawingoptions.useslinefragmentorigin | nsstringdrawingoptions.usesfontleading, attributes: [NSFontAttributeName:labelName.font], Context:nil) labelname . frame = CGRectMake (labelname_x, labelname_y, Rect1.width, rect1.height)//Member Avatar icon size and position var imgview2_x = CGR Ectgetmaxx (labelname.frame) + 10.0 var imgview2_y = imgview1_y var imgview2rect = CGRectMake (imgview2_x, img view2_y, Imgview2.image = UIImage (named: "") Imgview2.frame = imgview2rect//Based on release date content Get the space size of the text var labedate_x = labelname_x Labedate.numberofliNES = 0 Labedate.linebreakmode = nslinebreakmode.bycharwrapping labedate.text = weibo.createdon var t ext2:nsstring = NSString (cstring:labedate.text!.        Cstringusingencoding (nsutf8stringencoding)!, encoding:nsutf8stringencoding)! var rect2 = text2.boundingrectwithsize (Cgsizemake (Self.frame.size.width, Cgfloat.max), Options: Nsstringdrawingoptions.useslinefragmentorigin | nsstringdrawingoptions.usesfontleading, attributes: [NSFontAttributeName:labeDate.font], Context:nil) var Labeda te_y = Cgrectgetmaxy (imgview1.frame)-Rect2.height labedate.frame = CGRectMake (labedate_x, LabeDate_Y, Rect2.widt H, Rect2.height)//device var labelsource_x = Cgrectgetmaxx (labedate.frame) + var labelsource_        Y = labedate_y Labelsource.numberoflines = 0 Labelsource.linebreakmode = nslinebreakmode.bycharwrapping Labelsource.text = weibo.source var text3:nsstring = NSString (cstring:labelsource.text!. CstringusIngencoding (nsutf8stringencoding)!, encoding:nsutf8stringencoding)! var rect3 = text3.boundingrectwithsize (Cgsizemake (Self.frame.size.width, Cgfloat.max), Options: Nsstringdrawingoptions.useslinefragmentorigin | nsstringdrawingoptions.usesfontleading, attributes: [NSFontAttributeName:labelSource.font], Context:nil) labelso Urce.frame = CGRectMake (labelsource_x, labelsource_y, Rect3.width, rect3.height)//Weibo main content var Labe lcontent_x = imgview1_x var labelcontent_y = Cgrectgetmaxy (imgview1.frame) + 5 Labelcontent.numberoflines =        0 Labelcontent.linebreakmode = nslinebreakmode.bycharwrapping labelcontent.text = weibo.text! var attributes4 = [NSFontAttributeName:labelContent.font] var options4 = Nsstringdrawingoptions.useslinefragmento Rigin | nsstringdrawingoptions.usesfontleading var text4:nsstring = NSString (cstring:labelcontent.text!. Cstringusingencoding (nsutf8stringencoding)!, encoding:nsutf8stringencoding)! var rect4 = text4.boundingrectwithsize (Cgsizemake (Self.frame.size.width, Cgfloat.max), OPTIONS:OPTIONS4, attributes: Attributes4, context:nil) labelcontent.frame = CGRectMake (labelcontent_x, labelcontent_y, 355, Rect4.height)//34 5 under, is for the height of the iphone 6 = Cgrectgetmaxy (labelcontent.frame) + 10//row below the distance of 10 points} required init (coder Adeco Der:nscoder) {fatalerror ("init (coder:) have not been implemented")}}

 

viewcontroller.swift//uitableviewcellexample////Created by Xuyan on 15/8/15.//Copyright (c) 2015 Com.world. All rights Reserved.//import Uikitimport Foundationclass viewcontroller:uiviewcontroller,uitableviewdelegate,    Uitableviewdatasource {var tableview:uitableview!        var datas: [Weibo] = [] var cells: [Anyobject] = [] override func Viewdidload () {super.viewdidload ()        Additional setup after loading the view, typically from a nib. InitData () TableView = UITableView (Frame:cgrectmake (5,, self.view.frame.size.width-10, Self.view.fram                e.size.height-64), style:UITableViewStyle.Plain) Tableview.datasource = self tableview.delegate = self Self.view.addSubview (TableView)} func Numberofsectionsintableview (Tableview:uitableview), in        t {return 1} func TableView (Tableview:uitableview, Numberofrowsinsection section:int), Int { Return datAs.count} func TableView (Tableview:uitableview, Cellforrowatindexpath indexpath:nsindexpath), Uitablevie Wcell {var Wb:weibo = Datas[indexpath.row] var identity = "Cellidentity" var obj:anyobject? = Ta        Bleview.dequeuereusablecellwithidentifier (Identity) var cell:customcell!            if (obj = = nil) {cell = Customcell (Style:UITableViewCellStyle.Default, reuseidentifier:identity) Cell.setdata (WB)} else {cell = obj as! Customcell} return cell} func TableView (Tableview:uitableview, Heightforrowatindexpath Indexpat H:nsindexpath), cgfloat {var Cell:customcell = Cells[indexpath.row] as! Customcell Cell.setdata (Datas[indexpath.row]) return cell.height} override func Didreceivememoryw    Arning () {super.didreceivememorywarning ()//Dispose of any resources the can be recreated.      } func InitData (), Void {          var wb1 = Weibo (id:100, img: "3.jpg", Username: "Echo", Mbtype: "", CreatedOn: "Yesterday 22:30", Source: "iphone 6 Pl US ", Text:" Hubei Province, located in central China, the middle of the Yangtze River, Dongting Lake North, so called Hubei, abbreviated as "Hubei", the capital of Wuhan. Hubei is between north latitude 29°05′ to 33°20′, longitude 108°21′ to 116°07′, east of Anhui, southeast of Jiangxi, Hunan, West even Chongqing, northwest and Shaanxi as neighbor, northern to Henan. Hubei East, west and north three sides of the mountains, the middle of the "land of Plenty" Jianghan plain. ") var wb2 = Weibo (id:101, img:" 1.jpg ", Username:" Alice ", Mbtype:" ", CreatedOn:" Yesterday 22:35 ", Source:" IP Hone 6 Plus ", Text:" Friends, friends, because I operate inadvertently QQ number stolen, published some filthy speech and fraud information. I would like to apologize to all of you for not believing these filthy remarks and fraudulent information. I apologize for the negative impact this has brought to you. Please beware of this phenomenon when using QQ and other social tools to avoid the recurrence of tragedies.  Finally, the crooks who wish to steal the number, die a hukou this evening ... ") var wb3 = Weibo (id:101, img:" 2.jpg ", Username:" Goblin ", Mbtype:" ", CreatedOn:" Yesterday 22:55 ", Source: "iphone 6 Plus", Text: "What ' s mean." ") var wb4 = Weibo (id:101, img:" 3.jpg ", Username:" Echo ", Mbtype:" ", CreatedOn:" Yesterday 23:46 ", Source:" iphone 4 ", Text: "Test micro-blog list." ") var wb5 = Weibo (id:101, img:" 3.jpg ", Username:" Echo ", Mbtype:" ", CreatedOn:" Yesterday 23:47 ", Source:" iphone 4 ", Text: "I have published another microblog." ") var wb6 = Weibo (iD:101, img: "3.jpg", Username: "Echo", Mbtype: ", CreatedOn:" Yesterday 23:48 ", Source:" iphone 4 ", text:" I have published another microblog. " ") var wb7 = Weibo (id:101, img:" 3.jpg ", Username:" Echo ", Mbtype:" ", CreatedOn:" Yesterday 23:49 ", Source:" iphone 4 ", Text: "Dear friends, friends, because I operate inadvertently QQ number stolen, published some dirty speech and fraud information." I would like to apologize to all of you for not believing these filthy remarks and fraudulent information. I apologize for the negative impact this has brought to you. Please beware of this phenomenon when using QQ and other social tools to avoid the recurrence of tragedies. Finally, I wish the thieves, who died this evening a hukou .... ") Datas.append (WB1) datas.append (WB2) datas.append (WB3) datas.append (WB4) Datas.app        End (WB5) datas.append (WB6) datas.append (wb7) var cell1 = Customcell () cells.append (CELL1) var cell2 = Customcell () cells.append (cell2) var cell3 = Customcell () cells.append (Cell3) v Ar cell4 = Customcell () cells.append (CELL4) var cell5 = Customcell () cells.append (CELL5) var c  Ell6 = Customcell () cells.append (cell6) var cell7 = Customcell () cells.append (CELL7)} override Func PreFerredstatusbarstyle (), Uistatusbarstyle {return uistatusbarstyle.lightcontent}} 

IOS SWIFT UITableView for a simple Weibo list

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.