您的位置:首页 > 移动开发 > IOS开发

iOS 使用tableView实现 个人中心列表

2015-06-17 12:01 519 查看
类似于微信的个人中心 可以使用UITableViewl来实现。

最终效果



直接上代码

首先使

UIViewController

实现协议

UITableViewDataSource,UITableViewDelegate

创建两个属性

UITableView *personalTableView;
NSArray *dataSource;



@interface UserInfoViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *personalTableView;
    NSArray *dataSource;
    
}


初始化

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=@[@"我的分享",@"密码管理",@"用户协议",@"关于"];


实现一下几个代理

#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 欢迎加入
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: