上节地址:http://blog.csdn.net/lwjok2007/article/details/46534123
上一节实现了简单的好友列表,但是信息不够丰富,本节将好友的头像,名称,签名等信息全部显示出来
此处我们需要自定义cell
创建一个类 继承
UITableViewCell
添加所需属性
#import <UIKit/UIKit.h> @interface UserTableViewCell : UITableViewCell @property (strong,nonatomic) UIImageView *headerphoto;//头像 @property (strong,nonatomic) UILabel *nameLabel;//昵称 @property (strong,nonatomic) UILabel *isOnLine;//是否在线 @property (strong,nonatomic) UILabel *introductionLabel;//个性签名,动态等 @property (strong,nonatomic) UILabel *networkLabel;//网络状态 @end
重写初始化方法 添加各属性
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { headerphoto=[[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 50, 50)]; [self.contentView addSubview:headerphoto]; nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(60, 5, 200, 25)]; nameLabel.backgroundColor=[UIColor clearColor]; nameLabel.font=[UIFont systemFontOfSize:16]; [self.contentView addSubview:nameLabel]; isOnLine=[[UILabel alloc]initWithFrame:CGRectMake(60, 40, 50, 5)]; isOnLine.font=[UIFont systemFontOfSize:10]; [self.contentView addSubview:isOnLine]; introductionLabel=[[UILabel alloc]initWithFrame:CGRectMake(120, 40, 180, 5)]; introductionLabel.font=[UIFont systemFontOfSize:10]; [self.contentView addSubview:introductionLabel]; networkLabel=[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-50, 5, 50, 25)]; networkLabel.font=[UIFont systemFontOfSize:10]; [self.contentView addSubview:networkLabel]; } return self; }
修改viewcontroller 中tablevew的delegate方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *str=[titleArray objectAtIndex:indexPath.section]; NSArray *arr=[dataDic objectForKey:str]; static NSString *CellIdentifier = @"UserCell"; UserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; cell=nil; if (cell == nil) { cell = [[UserTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.selectionStyle = UITableViewCellSelectionStyleGray; } NSDictionary *dic=[arr objectAtIndex:indexPath.row]; cell.headerphoto.image=[UIImage imageNamed:[dic valueForKey:@"usericon"]]; cell.nameLabel.text=[dic valueForKey:@"name"]; cell.isOnLine.text=@"[在线]"; cell.introductionLabel.text=@"无动态"; cell.networkLabel.text=@"4G"; return cell; }
先讲到这里,剩余部分下节再讲
如果有问题可加qq讨论
苹果开发群 :414319235 欢迎加入
源代码将在群共享发布
原文地址:http://blog.csdn.net/lwjok2007/article/details/46549111