iOS 使用tableView实现 个人中心列表
2015-12-02 14:00
633 查看
类似于微信的个人中心 可以使用UITableViewl来实现。
最终效果
直接上代码
首先使
UIViewController
实现协议
UITableViewDataSource,UITableViewDelegate
创建两个属性
UITableView *personalTableView;
NSArray *dataSource;
[objc] view
plaincopy
@interface UserInfoViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *personalTableView;
NSArray *dataSource;
}
初始化
[objc] view
plaincopy
personalTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 44+20, SCREEN_WIDTH, SCREEN_HEIGHT-20-44-49) style:UITableViewStyleGrouped];
[self.view addSubview:personalTableView];
personalTableView.delegate=self;
personalTableView.dataSource=self;
personalTableView.bounces=NO;
personalTableView.showsVerticalScrollIndicator = NO;//不显示右侧滑块
personalTableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;//分割线
dataSource=@[@"我的分享",@"密码管理",@"用户协议",@"关于"];
实现一下几个代理
[objc] view
plaincopy
#pragma mark tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//分组数 也就是section数
return 3;
}
//设置每个分组下tableview的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section==0) {
return 1;
}else if (section==1) {
return dataSource.count;
}else{
return 1;
}
}
//每个分组上边预留的空白高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 20;
}
//每个分组下边预留的空白高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if (section==2) {
return 40;
}
return 20;
}
//每一个分组下对应的tableview 高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section==0) {
return 80;
}
return 40;
}
//设置每行对应的cell(展示的内容)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifer=@"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifer];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
}
if (indexPath.section==0) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"userinfo"];
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(12, 0, 80, 80)];
imageView.image=[UIImage imageNamed:@"usericon.png"];
[cell.contentView addSubview:imageView];
UILabel *nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 0, 60, 80)];
nameLabel.text=@"李晨";
[cell.contentView addSubview:nameLabel];
}else if (indexPath.section==1) {
cell.textLabel.text=[dataSource objectAtIndex:indexPath.row];
}else{
cell.textLabel.text=@"退出登陆";
cell.textLabel.textAlignment=NSTextAlignmentCenter;
}
return cell;
}
到此为止基本实现了整个界面
如果有问题可加qq讨论
苹果开发群 :414319235 欢迎加入
最终效果
直接上代码
首先使
UIViewController
实现协议
UITableViewDataSource,UITableViewDelegate
创建两个属性
UITableView *personalTableView;
NSArray *dataSource;
[objc] view
plaincopy
@interface UserInfoViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *personalTableView;
NSArray *dataSource;
}
初始化
[objc] view
plaincopy
personalTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 44+20, SCREEN_WIDTH, SCREEN_HEIGHT-20-44-49) style:UITableViewStyleGrouped];
[self.view addSubview:personalTableView];
personalTableView.delegate=self;
personalTableView.dataSource=self;
personalTableView.bounces=NO;
personalTableView.showsVerticalScrollIndicator = NO;//不显示右侧滑块
personalTableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;//分割线
dataSource=@[@"我的分享",@"密码管理",@"用户协议",@"关于"];
实现一下几个代理
[objc] view
plaincopy
#pragma mark tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//分组数 也就是section数
return 3;
}
//设置每个分组下tableview的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section==0) {
return 1;
}else if (section==1) {
return dataSource.count;
}else{
return 1;
}
}
//每个分组上边预留的空白高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 20;
}
//每个分组下边预留的空白高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if (section==2) {
return 40;
}
return 20;
}
//每一个分组下对应的tableview 高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section==0) {
return 80;
}
return 40;
}
//设置每行对应的cell(展示的内容)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifer=@"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifer];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
}
if (indexPath.section==0) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"userinfo"];
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(12, 0, 80, 80)];
imageView.image=[UIImage imageNamed:@"usericon.png"];
[cell.contentView addSubview:imageView];
UILabel *nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 0, 60, 80)];
nameLabel.text=@"李晨";
[cell.contentView addSubview:nameLabel];
}else if (indexPath.section==1) {
cell.textLabel.text=[dataSource objectAtIndex:indexPath.row];
}else{
cell.textLabel.text=@"退出登陆";
cell.textLabel.textAlignment=NSTextAlignmentCenter;
}
return cell;
}
到此为止基本实现了整个界面
如果有问题可加qq讨论
苹果开发群 :414319235 欢迎加入
相关文章推荐
- IOS TableView 详解
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- ios:TableView的用法
- IOS学习之整体框架类图
- ios 推送证书没有密钥 解决方案
- 使用coreImage滤镜
- ios简单国际化
- iOS开发多线程篇—GCD的基本使用
- iOS音频文件拼接+裁剪(音频文件拼接做了性能优化)
- iOS开发之手势识别
- iOS_tableview 修改分割线位置(兼容IOS8)
- iOS开发之事件的产生和传递
- IOS回调
- iOS 9 中的变化汇总
- iOS跳转系统设置界面
- 用CocoaPods做iOS程序的依赖管理
- iOS 代码块在程序中只执行一次
- iOS证书说明和发布内购流程整理
- iOS 8 新特性总结
- iOS_导入libxml2.2框架后,找不到<libxml/tree.h>的解决办法