分享

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

 安安0118 2016-05-26

类似于微信的个人中心 可以使用UITableViewl来实现。

最终效果



直接上代码


首先使

UIViewController

实现协议 

UITableViewDataSource,UITableViewDelegate

创建两个属性


   UITableView *personalTableView;

   NSArray *dataSource;

    



  1. @interface UserInfoViewController ()<UITableViewDataSource,UITableViewDelegate>  
  2. {  
  3.     UITableView *personalTableView;  
  4.     NSArray *dataSource;  
  5.       
  6. }  


初始化


  1. personalTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 44+20, SCREEN_WIDTH, SCREEN_HEIGHT-20-44-49) style:UITableViewStyleGrouped];  
  2.    [self.view addSubview:personalTableView];  
  3.    personalTableView.delegate=self;  
  4.    personalTableView.dataSource=self;  
  5.    personalTableView.bounces=NO;  
  6.    personalTableView.showsVerticalScrollIndicator = NO;//不显示右侧滑块  
  7.    personalTableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;//分割线  
  8.    dataSource=@[@"我的分享",@"密码管理",@"用户协议",@"关于"];  

实现一下几个代理


  1. #pragma mark tableViewDelegate  
  2. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  3. {  
  4.     //分组数 也就是section数  
  5.     return 3;  
  6. }  
  7.   
  8. //设置每个分组下tableview的行数  
  9. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  10. {  
  11.     if (section==0) {  
  12.         return 1;  
  13.     }else if (section==1) {  
  14.         return dataSource.count;  
  15.     }else{  
  16.         return 1;  
  17.     }  
  18. }  
  19. //每个分组上边预留的空白高度  
  20. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section  
  21. {  
  22.       
  23.     return 20;  
  24. }  
  25. //每个分组下边预留的空白高度  
  26. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section  
  27. {  
  28.     if (section==2) {  
  29.         return 40;  
  30.     }  
  31.     return 20;  
  32. }  
  33. //每一个分组下对应的tableview 高度  
  34. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
  35. {  
  36.     if (indexPath.section==0) {  
  37.         return 80;  
  38.     }  
  39.     return 40;  
  40. }  
  41.   
  42. //设置每行对应的cell(展示的内容)  
  43. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  44. {  
  45.     static NSString *identifer=@"cell";  
  46.     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifer];  
  47.     if (cell==nil) {  
  48.         cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];  
  49.     }  
  50.       
  51.     if (indexPath.section==0) {  
  52.         cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"userinfo"];  
  53.           
  54.         UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(12, 0, 80, 80)];  
  55.         imageView.image=[UIImage imageNamed:@"usericon.png"];  
  56.         [cell.contentView addSubview:imageView];  
  57.           
  58.         UILabel *nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 0, 60, 80)];  
  59.         nameLabel.text=@"李晨";  
  60.         [cell.contentView addSubview:nameLabel];  
  61.     }else if (indexPath.section==1) {  
  62.         cell.textLabel.text=[dataSource objectAtIndex:indexPath.row];  
  63.     }else{  
  64.         cell.textLabel.text=@"退出登陆";  
  65.         cell.textLabel.textAlignment=NSTextAlignmentCenter;  
  66.     }  
  67.     return cell;  
  68. }  

到此为止基本实现了整个界面




如果有问题可加qq讨论

苹果开发群 :414319235  欢迎加入


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多