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("个人信息内存警告"); } } 效果图如下:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 关于Cocos2d-x的一些认识(3)
- ruby-on-rails – Hartl的Rails教程第9章练习6
- 什么是在swift中使用strongSelf的正确方法?
- flash性能优化方案整理(最全)
- ruby-on-rails – Rails:从Active Record Session Store迁
- flex_基于MX List列表组件示例;
- c# – ServiceStack RequestBindingException
- ruby-on-rails – 物化选择输入显示两个插入符号
- postgres 之 initdb 源码分析 五
- Cannot write to RAM for Flash Algorithms MDK422