我遇到了一些我的代码问题。我非常积极的有一个非常简单的解决方案,我似乎无法看到它!基本上我使用单个tableview类来显示不同的数据(内容,项目,移动)。我没有3个不同的表格视图控制器,而是有一个跟踪哪个单元被点击并显示相关数据。所有作品都非常好!我正在使用一个单独的nib文件的自定义单元类,所以我需要正确实现一个if语句(或一个switch语句)来检查什么单元被挖掘(self.title),然后创建基于此的自定义单元。将多个自定义单元格动态加载到单个表格视图中
很明显,我遇到的问题是范围,cell.imageView不知道它在引用什么,因为它在if语句中超出了范围。
我一直在使用
id cell;
我得到同样的错误尝试。什么是我完成这个任务的一种方式,以便每个“标题”使用不同的自定义单元格?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
if (title == @"Content" || title == @"Favorite Content")
{
ContentCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"ContentCell" owner:self options:nil];
for (id currentObject in objects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (ContentCell *)currentObject;
break;
}
}
}
}
else if (title == @"Items" || title == @"Favorite Items")
{
ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"ItemCell" owner:self options:nil];
for (id currentObject in objects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (ItemCell *)currentObject;
break;
}
}
}
}
else if (title == @"Moves" || title == @"Favorite Moves")
{
MoveCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"MoveCell" owner:self options:nil];
for (id currentObject in objects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (MoveCell *)currentObject;
break;
}
}
}
}
cell.imageView.image = [UIImage imageNamed:[[self.theData objectAtIndex:indexPath.row] objectForKey:@"image"]];
cell.contentName.text = [[self.theData objectAtIndex:indexPath.row] objectForKey:@"name"];
return cell;
}
任何帮助将是伟大的!我敢肯定,我的盯着屏幕的时间已经让我错过了一些简单的事情。谢谢!
你必须改变CellIdentifier值。每次你设置相同的值,那就是。 – priyanka