我的一些代码存在问题。我很肯定有一个非常简单的解决方案,我似乎无法看到它!基本上我使用单个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;
}
任何帮助都会很棒!我确信我盯着屏幕的时间让我错过了一些简单的事情。谢谢!
答案 0 :(得分:4)
每次使用新的自定义单元格时,都必须更改NSString *CellIdentifier;
值。你必须输入你输入的相同值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *CellIdentifier;
UITableViewCell *cell;
if (title == @"Content" || title == @"Favorite Content")
{
CellIdentifier = @"Cell";
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")
{
CellIdentifier = @"";
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")
{
CellIdentifier = @"";
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;
}