標籤:uitableview 個人中心 列表 介面
類似於的個人中心 可以使用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;//分割線 [email protected][@"我的分享",@"密碼管理",@"使用者協議",@"關於"];
實現一下幾個代理
#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 *[email protected]"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)]; [email protected]"李晨"; [cell.contentView addSubview:nameLabel]; }else if (indexPath.section==1) { cell.textLabel.text=[dataSource objectAtIndex:indexPath.row]; }else{ [email protected]"退出登陸"; cell.textLabel.textAlignment=NSTextAlignmentCenter; } return cell;}
到此為止基本實現了整個介面
如果有問題可加qq討論
蘋果開發群 :414319235 歡迎加入
UIViewControllerUITableViewDataSource,UITableViewDelegate
iOS 使用tableView實現 個人中心列表