Basic use of Swift-uitableview
Needless to say, just paste the code I wrote today: If the novice has what do not understand, you can send my mailbox.
//
Singleinfo.swift Personal Information
Housekeeper
//
Created by Lu Yang on 15/10/27.
Copyright? year Niven Moore. All rights reserved.
//
Import Foundation
Import UIKit
Class Singleinfo:uiviewcontroller, Uitableviewdatasource, uitableviewdelegate{
var datatable:uitableview! ; Data Tables
var itemstring=[" nickname", " account"," gender", " region", " my Car")
Current Screen Object
var screenobject=uiscreen.mainscreen(). Bounds;
Page Initialization
Override func Viewdidload() {
Super.viewdidload();
Initview();
}
/**
UI Initialization
*/
Func Initview() {
self.title= " my Profile";
SELF.VIEW.BACKGROUNDCOLOR=UICOLOR.LINGHTGREYBG();
Creattable();
}
/**
My profile table is initialized
*/
Func creattable() {
Let Datatablew:cgfloat=screenobject.width;
Let Datatableh:cgfloat=screenobject.height;
Let datatablex:cgfloat=0;
Let datatabley:cgfloat=0;
Datatable=uitableview(Frame:cgrectmake(Datatablex, Datatabley, Datatablew, Datatableh), style:UITableViewStyle.Grouped);
Datatable.delegate=self; Implementing a proxy
Datatable.datasource=self; Implementing a data source
Self.view.addSubview(dataTable);
}
1.1 Returns a group by default
Func numberofsectionsintableview(Tableview:uitableview), Int {
return 2;
}
1.2 Returns the number of rows
Func tableView(Tableview:uitableview, numberofrowsinsection section:int), Int {
If(section = = 0) {
return 1;
}else{
return 5;
}
}
1.3 return Row height
Func tableView(Tableview:uitableview, Heightforrowatindexpath indexpath:nsindexpath), cgfloat{
If(indexpath.section = = 0) {
return;
}else{
return;
}
}
1.4 Head height for each group
Func tableView(Tableview:uitableview, heightforheaderinsection section:int), cgfloat {
return ten;
}
1.5 The bottom height of each group
Func tableView(Tableview:uitableview, heightforfooterinsection section:int), cgfloat {
return 1;
}
1.6 Returning a data source
Func tableView(Tableview:uitableview, Cellforrowatindexpath indexpath:nsindexpath) UITableViewCell {
Let identifier= "Identtifier";
var cell=tableview.dequeuereusablecellwithidentifier(identifier);
If(cell = = Nil) {
Cell=uitableviewcell(style:UITableViewCellStyle.Value1, Reuseidentifier:identifier);
}
If(indexpath.section = = 0) {
Cell?. Textlabel?. text= " Avatar";
}else{
Cell?. Textlabel?. Text=itemstring[Indexpath.row];
}
Cell?. Accessorytype=uitableviewcellaccessorytype.disclosureindicator;
Return cell! ;
}
1.7 Table Click event
Func tableView(Tableview:uitableview, Didselectrowatindexpath indexpath:nsindexpath) {
uncheck the style
Tableview.deselectrowatindexpath(Indexpath, animated:true);
Get the row index of the click
If(Indexpath.row = = 0) {
Let Pushsingleinfo=singleinfo();
Pushsingleinfo.hidesbottombarwhenpushed=true; Hide Navigation bar
Self.navigationcontroller?. Pushviewcontroller(Pushsingleinfo, animated:true);
}
}
Memory Warning
Override func didreceivememorywarning() {
Super.didreceivememorywarning();
Print(" Personal Information Memory warning");
}
}
As follows:
Basic use of Swift-uitableview (example)