[英]unable to load two table views in one view controller
Error:- use of un declared identifier cell. 错误:-使用未声明的标识符单元格。
unable to load two custom cells in one view controller. 无法在一个视图控制器中加载两个自定义单元格。
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
if (tableView == tableView_grantRecordAccess)
{
UITableViewCell *cell = [tableView_grantRecordAccess dequeueReusableCellWithIdentifier:@"EHSRecordAccessGrantCell"];
if (cell == nil) {
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"EHSRecordAccessGrantCell" owner:self options:nil];
// Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
cell = [topLevelObjects objectAtIndex:0];
}
return cell;
}
else if (tableView == tableView_accessRecordRequest) {
UITableViewCell *cell = [tableView_accessRecordRequest dequeueReusableCellWithIdentifier:@"EHSAccessRecordCell"];
if (cell == nil) {
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"EHSAccessRecordCell" owner:self options:nil];
// Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
cell = [topLevelObjects objectAtIndex:0];
}
}
return cell;
}
这是单元格引用的范围问题,如果您将在方法的第一行引用单元格,然后仅在两种情况下都使用该引用,则它将起作用。
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell *cell = nil;
if (tableView == tableView_grantRecordAccess)
{
cell = [tableView_grantRecordAccess dequeueReusableCellWithIdentifier:@"EHSRecordAccessGrantCell"];
if (cell == nil) {
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"EHSRecordAccessGrantCell" owner:self options:nil];
// Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
cell = [topLevelObjects objectAtIndex:0];
}
return cell;
}
else if (tableView == tableView_accessRecordRequest) {
cell = [tableView_accessRecordRequest dequeueReusableCellWithIdentifier:@"EHSAccessRecordCell"];
if (cell == nil) {
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"EHSAccessRecordCell" owner:self options:nil];
// Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
cell = [topLevelObjects objectAtIndex:0];
}
}
return cell;
}
add this line above if condition respectively //if (cell == nil) { ... } 在if条件分别上分别添加此行// // if(cell == nil){...}
//For first table condition //对于第一个表条件
if (![cell isKindOfClass:[EHSRecordAccessGrantCell
class]])cell = nil;
//For second table condition //对于第二个表条件
if (![cell isKindOfClass:[EHSAccessRecordCell
class]])cell = nil;
crash because of you have register both cell to table and whenever it comes to second one the cell already have the reference of the second one cell that is differet typeof 由于您已将两个单元格都注册到表而崩溃,并且每当涉及到第二个单元格时,该单元格就已经具有第二个单元格的引用,该第二个单元格是不同类型
Check in your storyboard for the right cell identifier. 在情节提要中检查正确的单元格标识符。
Select both of your table's prototype cell's identifier in identity inspector. 在身份检查器中选择表的原型单元格的标识符。
Looking at your error statement, I can assume- You may have forgotten to assign the identifier to your cell or you are using wrong identifier(may be a spelling mistake) in your code(cellForRowAtIndexPath method). 查看您的错误声明,我可以假设-您可能忘记了为该单元格分配标识符,或者您在代码(cellForRowAtIndexPath方法)中使用了错误的标识符(可能是拼写错误)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.