swift-UITableView的根本使用

前端之家收集整理的这篇文章主要介绍了swift-UITableView的根本使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
swift-UITableView的基本使用

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

//@H_404_4@

// singleInfo.swift 个人信息@H_404_4@

// Housekeeper@H_404_4@

//@H_404_4@

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

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

//@H_404_4@

@H_404_4@

importFoundation@H_404_4@

importUIKit@H_404_4@

classsingleInfo:UIViewController,UITableViewDataSource,UITableViewDelegate{@H_404_4@

vardataTable:UITableView!; //数据表格@H_404_4@

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

 //当前屏幕对象@H_404_4@

  varscreenObject=UIScreen.mainScreen().bounds;@H_404_4@

@H_404_4@

//页面初始化@H_404_4@

overridefuncviewDidLoad() {@H_404_4@

super.viewDidLoad();@H_404_4@

initView();@H_404_4@

}@H_404_4@

/**@H_404_4@

UI初始化@H_404_4@

*/@H_404_4@

funcinitView(){@H_404_4@

self.title="我的资料";@H_404_4@

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

creatTable();@H_404_4@

}@H_404_4@

/**@H_404_4@

我的资料表格初始化@H_404_4@

*/@H_404_4@

funccreatTable(){@H_404_4@

letdataTableW:CGFloat=screenObject.width; @H_404_4@

letdataTableH:CGFloat=screenObject.height;@H_404_4@

letdataTableX:CGFloat=0;@H_404_4@

letdataTableY:CGFloat=0;@H_404_4@

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

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

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

self.view.addSubview(dataTable);@H_404_4@

}@H_404_4@

//1.1默认返回一组@H_404_4@

funcnumberOfSectionsInTableView(tableView:UITableView) ->Int{@H_404_4@

return2;@H_404_4@

}@H_404_4@

@H_404_4@

// 1.2返回行数@H_404_4@

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

if(section ==0){@H_404_4@

return1;@H_404_4@

}else{@H_404_4@

return5;@H_404_4@

}@H_404_4@

}@H_404_4@

@H_404_4@

//1.3返回行高@H_404_4@

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

@H_404_4@

if(indexPath.section==0){@H_404_4@

return80;@H_404_4@

}else{@H_404_4@

return55;@H_404_4@

@H_404_4@

}@H_404_4@

}@H_404_4@

@H_404_4@

//1.4每组的头部高度@H_404_4@

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

return10;@H_404_4@

}@H_404_4@

@H_404_4@

//1.5每组的底部高度@H_404_4@

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

return1;@H_404_4@

}@H_404_4@

//1.6返回数据源@H_404_4@

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

letidentifier="identtifier";@H_404_4@

varcell=tableView.dequeueReusableCellWithIdentifier(identifier);@H_404_4@

if(cell ==nil){@H_404_4@

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

}@H_404_4@

@H_404_4@

if(indexPath.section==0){@H_404_4@

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

}else{@H_404_4@

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

}@H_404_4@

cell?.accessoryType=UITableViewCellAccessoryType.DisclosureIndicator;@H_404_4@

returncell!;@H_404_4@

}@H_404_4@

//1.7表格点击事件@H_404_4@

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

//取消选中的样式@H_404_4@

tableView.deselectRowAtIndexPath(indexPath,animated:true);@H_404_4@

   //获取点击的行索引@H_404_4@

if(indexPath.row==0){@H_404_4@

letpushSingleInfo=singleInfo();@H_404_4@

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

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

}@H_404_4@

}@H_404_4@

@H_404_4@

//内存警告@H_404_4@

overridefuncdidReceiveMemoryWarning() {@H_404_4@

super.didReceiveMemoryWarning();@H_404_4@

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

}@H_404_4@

}@H_404_4@

效果图如下:@H_404_4@

@H_404_4@ 原文链接:https://www.f2er.com/swift/323490.html

猜你在找的Swift相关文章