swift-UITableView的根本使用

swift-UITableView的基本使用

废话不多说了,直接贴我今天写的代码吧:如果新手有什么不懂的,可以发我邮箱。

//

// singleInfo.swift 个人信息

// Housekeeper

//

// Created by卢洋on 15/10/27.

// Copyright © 2015奈文摩尔. All rights reserved.

//

importFoundation

importUIKit

classsingleInfo:UIViewController,UITableViewDataSource,UITableViewDelegate{

vardataTable:UITableView!; //数据表格

varitemString=["昵称","账号","性别","地区","我的爱车"]

 //当前屏幕对象

  varscreenObject=UIScreen.mainScreen().bounds;

//页面初始化

overridefuncviewDidLoad() {

super.viewDidLoad();

initView();

}

/**

UI初始化

*/

funcinitView(){

self.title="我的资料";

self.view.backgroundColor=UIColor.linghtGreyBg();

creatTable();

}

/**

我的资料表格初始化

*/

funccreatTable(){

letdataTableW:CGFloat=screenObject.width;

letdataTableH:CGFloat=screenObject.height;

letdataTableX:CGFloat=0;

letdataTableY:CGFloat=0;

dataTable=UITableView(frame:CGRectMake(dataTableX,dataTableY,dataTableW,dataTableH),style:UITableViewStyle.Grouped);

dataTable.delegate=self;      //实现代理

dataTable.dataSource=self;    //实现数据源

self.view.addSubview(dataTable);

}

//1.1默认返回一组

funcnumberOfSectionsInTableView(tableView:UITableView) ->Int{

return2;

}

// 1.2返回行数

functableView(tableView:UITableView,numberOfRowsInSection section:Int) ->Int{

if(section ==0){

return1;

}else{

return5;

}

}

//1.3返回行高

functableView(tableView:UITableView,heightForRowAtIndexPath indexPath:NSIndexPath) ->CGFloat{

if(indexPath.section==0){

return80;

}else{

return55;

}

}

//1.4每组的头部高度

functableView(tableView:UITableView,heightForHeaderInSection section:Int) ->CGFloat{

return10;

}

//1.5每组的底部高度

functableView(tableView:UITableView,heightForFooterInSection section:Int) ->CGFloat{

return1;

}

//1.6返回数据源

functableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath) ->UITableViewCell{

letidentifier="identtifier";

varcell=tableView.dequeueReusableCellWithIdentifier(identifier);

if(cell ==nil){

cell=UITableViewCell(style:UITableViewCellStyle.Value1,reuseIdentifier: identifier);

}

if(indexPath.section==0){

cell?.textLabel?.text="头像";

}else{

cell?.textLabel?.text=itemString[indexPath.row];

}

cell?.accessoryType=UITableViewCellAccessoryType.DisclosureIndicator;

returncell!;

}

//1.7表格点击事件

functableView(tableView:UITableView,didSelectRowAtIndexPath indexPath:NSIndexPath) {

//取消选中的样式

tableView.deselectRowAtIndexPath(indexPath,animated:true);

   //获取点击的行索引

if(indexPath.row==0){

letpushSingleInfo=singleInfo();

pushSingleInfo.hidesBottomBarWhenPushed=true;    //隐藏导航栏

self.navigationController?.pushViewController(pushSingleInfo,animated:true);

}

}

//内存警告

overridefuncdidReceiveMemoryWarning() {

super.didReceiveMemoryWarning();

print("个人信息内存警告");

}

}

效果图如下:

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...