如题,刚开始学习ios开发,自定义了一个UItableview,想做成qq分组的样式,数据从本地plist文件读取,可是运行的时候,界面没有数据,空白的,发现UITableViewDataSource里的cellForRowAtIndexPath这个方法没有被执行,刚开始学习,不懂如何解决,求大神给指点一下,新人第一次发帖,分数不多,请见谅
根视图实现代码如下:
@implementation MainViewController- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor lightGrayColor]];
[self setTitle:@"好友分组"];
self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain]; [self.tableView setDataSource:self];
[self.tableView setDelegate:self];
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[self.tableView registerClass:[GroupCell class] forCellReuseIdentifier:@"GroupCell"];
[self.view addSubview:self.tableView];
}-(NSArray *)dataSource {
if (!_dataSource) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"group"ofType:@"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *tempArray = [NSMutableArray array];
for (NSDictionary *dataDic in array) {
GroupModel *model = [GroupModel modelWithDictionary:dataDic];
[tempArray addObject:model];
}
_dataSource = [tempArray copy];
}
return _dataSource;
}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.dataSource.count;
}#pragma -- tableView DataSource --- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
GroupModel *model = self.dataSource[section];
return model.friendsArray.count;
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
GroupCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GroupCell"];
GroupModel *model = self.dataSource[indexPath.section];
FriendsModel *fmodel = model.friendsArray[indexPath.row];
[cell updateWithFriendsModel:fmodel];
[self.tableView reloadData];
return cell;
}
#pragma -- tableView Delegate --- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 64;
}
分组模型实现代码如下:
@implementation GroupModel- (void)setValue:(id)value forUndefinedKey:(NSString *)key {}- (void)setFriendsArray:(NSArray *)friendsArray {
NSMutableArray *tempArray = [NSMutableArray array];
for (NSDictionary *dataDic in friendsArray) {
FriendsModel *model = [[FriendsModel alloc]init];
[model setValuesForKeysWithDictionary:dataDic];
[tempArray addObject:model];
}
_friendsArray = [tempArray copy];
}+ (instancetype)modelWithDictionary:(NSDictionary *)dic {
GroupModel *model = [[self alloc]init];
[model setValuesForKeysWithDictionary:dic];
return model;
}
好友模型实现代码:
@implementation FriendsModel- (void)setValue:(id)value forUndefinedKey:(NSString *)key {}+(instancetype)modelWithDictionary:(NSDictionary *)dict {
FriendsModel *model = [[self alloc]init];
[model setValuesForKeysWithDictionary:dict];
return model;
}
自定义Cell实现代码如下:
@implementation GroupCell- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 64, 64)];
[self.contentView addSubview:self.iconImageView];
self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(70, 0, 200, 30)];
[self.contentView addSubview:self.nameLabel];
self.messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(70, 34, 220, 30)];
[self.contentView addSubview:self.messageLabel];
UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 0.3)];
[lineView setBackgroundColor:[UIColor redColor]];
[self.contentView addSubview:lineView];
}
return self;
}- (void)updateWithFriendsModel:(FriendsModel *)model {
[self.iconImageView setImage:[UIImage imageNamed:model.icon]];
[self.nameLabel setText:model.name];
[self.nameLabel setFont:[UIFont systemFontOfSize:16]];
[self.messageLabel setText:model.message];
[self.messageLabel setFont:[UIFont systemFontOfSize:15]];
}
根视图实现代码如下:
@implementation MainViewController- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor lightGrayColor]];
[self setTitle:@"好友分组"];
self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain]; [self.tableView setDataSource:self];
[self.tableView setDelegate:self];
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[self.tableView registerClass:[GroupCell class] forCellReuseIdentifier:@"GroupCell"];
[self.view addSubview:self.tableView];
}-(NSArray *)dataSource {
if (!_dataSource) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"group"ofType:@"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *tempArray = [NSMutableArray array];
for (NSDictionary *dataDic in array) {
GroupModel *model = [GroupModel modelWithDictionary:dataDic];
[tempArray addObject:model];
}
_dataSource = [tempArray copy];
}
return _dataSource;
}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.dataSource.count;
}#pragma -- tableView DataSource --- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
GroupModel *model = self.dataSource[section];
return model.friendsArray.count;
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
GroupCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GroupCell"];
GroupModel *model = self.dataSource[indexPath.section];
FriendsModel *fmodel = model.friendsArray[indexPath.row];
[cell updateWithFriendsModel:fmodel];
[self.tableView reloadData];
return cell;
}
#pragma -- tableView Delegate --- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 64;
}
分组模型实现代码如下:
@implementation GroupModel- (void)setValue:(id)value forUndefinedKey:(NSString *)key {}- (void)setFriendsArray:(NSArray *)friendsArray {
NSMutableArray *tempArray = [NSMutableArray array];
for (NSDictionary *dataDic in friendsArray) {
FriendsModel *model = [[FriendsModel alloc]init];
[model setValuesForKeysWithDictionary:dataDic];
[tempArray addObject:model];
}
_friendsArray = [tempArray copy];
}+ (instancetype)modelWithDictionary:(NSDictionary *)dic {
GroupModel *model = [[self alloc]init];
[model setValuesForKeysWithDictionary:dic];
return model;
}
好友模型实现代码:
@implementation FriendsModel- (void)setValue:(id)value forUndefinedKey:(NSString *)key {}+(instancetype)modelWithDictionary:(NSDictionary *)dict {
FriendsModel *model = [[self alloc]init];
[model setValuesForKeysWithDictionary:dict];
return model;
}
自定义Cell实现代码如下:
@implementation GroupCell- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 64, 64)];
[self.contentView addSubview:self.iconImageView];
self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(70, 0, 200, 30)];
[self.contentView addSubview:self.nameLabel];
self.messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(70, 34, 220, 30)];
[self.contentView addSubview:self.messageLabel];
UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 0.3)];
[lineView setBackgroundColor:[UIColor redColor]];
[self.contentView addSubview:lineView];
}
return self;
}- (void)updateWithFriendsModel:(FriendsModel *)model {
[self.iconImageView setImage:[UIImage imageNamed:model.icon]];
[self.nameLabel setText:model.name];
[self.nameLabel setFont:[UIFont systemFontOfSize:16]];
[self.messageLabel setText:model.message];
[self.messageLabel setFont:[UIFont systemFontOfSize:15]];
}
static NSString *cellID=@"GroupCell";
换成 [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GroupCell Class]) bundle:nil] forCellReuseIdentifier:cellID];
数据没加载进去。。返时没数据进不去cell