Basic use of swift-UITableView, swift-uitableview
If you don't talk much about it, paste the code I wrote today: If you don't understand anything, you can send me an email.
//
// SingleInfo. swift personal information
// Housekeeper
//
// Created by Lu Yang on 15/10/27.
// Copyright©Nevenmoore. All rights reserved.
//
Import Foundation
Import UIKit
Class singleInfo: UIViewController, UITableViewDataSource, UITableViewDelegate {
Var dataTable: UITableView !; // Data table
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 Documents ";
Self. view. backgroundColor = UIColor. linghtGreyBg ();
CreatTable ();
}
/**
My data table Initialization
*/
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, ableableh), style: UITableViewStyle. Grouped );
DataTable. delegate = self; // implement proxy
DataTable. dataSource = self; // implement the data source
Self. view. addSubview (dataTable );
}
// 1.1 A group is returned by default
Func numberOfSectionsInTableView (tableView: UITableView)-> Int {
Return 2;
}
// 1.2 rows returned
Func tableView (tableView: UITableView, numberOfRowsInSection section: Int)-> Int {
If (section = 0 ){
Return 1;
} Else {
Return 5;
}
}
// 1.3 returns the Row Height
Func tableView (tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath)-> CGFloat {
If (indexPath. section = 0 ){
Return 80;
} Else {
Return 55;
}
}
// 1.4 head height of each group
Func tableView (tableView: UITableView, heightForHeaderInSection section: Int)-> CGFloat {
Return 10;
}
// 1.5 bottom height of each group
Func tableView (tableView: UITableView, heightForFooterInSection section: Int)-> CGFloat {
Return 1;
}
// 1.6 Data Source returned
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 ){
// Cancel the selected Style
TableView. deselectRowAtIndexPath (indexPath, animated: true );
// Obtain the index of the row to be clicked
If (indexPath. row = 0 ){
Let pushSingleInfo = singleInfo ();
PushSingleInfo. hidesBottomBarWhenPushed = true; // hide the navigation bar
Self. navigationController ?. PushViewController (pushSingleInfo, animated: true );
}
}
// Memory warning
Override func didReceiveMemoryWarning (){
Super. didReceiveMemoryWarning ();
Print ("personal information memory warning ");
}
}
As follows: