我有一个带有表格视图的视图控制器,在情节提要中设计,该表格视图使用三个不同的自定义单元格,每个单元格用于三个不同的部分。我已将每个自定义单元格定义为原型单元格,为每个单元格使用不同的标识符,并在 Xcode 中的每个单元格中定义一个自定义单元格对象,由 Interface Builder 与每个单元格的每个控件链接。
我已将自定义单元格作为部分顺序放置,您可以在此处查看。每个标记的正方形都是一个自定义原型单元。
这是表格视图代码:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section==2){
return 40;
}else if (indexPath.section==1){
return 40;
} else if (indexPath.section==0){
return 205;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section==0){
return 1;
} else if (section==1){
return [[selectedTeam valueForKey:@"jugadores"]count];
} else if (section==2){
return [[selectedTeam valueForKey:@"tecnicos"]count];
}
}
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section==0){
NSString *imageService = @"http://backend.exular.net/contenido/";
imageCell *cellImage = [table dequeueReusableCellWithIdentifier:@"imatge"];
if (cellImage == nil) {
cellImage = [[imageCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:@"imatge"];
}
cellImage.leagueName.text = [selectedTeam valueForKey:@"nom"];
NSString *path= [[NSString stringWithFormat:@"%@%@" ,imageService, [selectedTeam valueForKey:@"img"]] stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
[cellImage.teamPicture setImageWithURL:[NSURL URLWithString:path] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
imageService=nil;
return cellImage;
} else if (indexPath.section==1){
teamCell *cellTeam = [table dequeueReusableCellWithIdentifier:@"equipo"];
if (cellTeam == nil) {
cellTeam = [[teamCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:@"equipo"];
}
NSDictionary *teamDic = [[selectedTeam valueForKey:@"jugadores"] objectAtIndex:indexPath.row];
cellTeam.name.text = [teamDic valueForKey:@"nom"];
cellTeam.height.text = [teamDic valueForKey:@"height"];
cellTeam.number.text = [teamDic valueForKey:@"altura"];
cellTeam.date.text = [teamDic valueForKey:@"date"];
cellTeam.position.text = [teamDic valueForKey:@"posicion"];
return cellTeam;
} else if (indexPath.section==2){
NSString *CellIdentifier = @"staff";
staffCell *cellStaff = [table dequeueReusableCellWithIdentifier:@"staff"];
if (cellStaff == nil) {
cellStaff = [[staffCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:@"staff"];
}
NSDictionary *staffDic = [[selectedTeam valueForKey:@"tecnicos"] objectAtIndex:indexPath.row];
cellStaff.name.text = [staffDic valueForKey:@"nom"];
cellStaff.role.text = [staffDic valueForKey:@"posicion"];
return cellStaff;
}
return nil;
}
Xcode 给了我这个错误:Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<teamCell 0x8d4f6b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key role.'
执行中断
teamCell *cellTeam = [table dequeueReusableCellWithIdentifier:@"equipo"];
我会感谢你的帮助。
非常感谢。