gpt4 book ai didi

ios - 多个自定义单元格动态加载到单个表格 View 中

转载 作者:行者123 更新时间:2023-12-01 18:26:29 25 4
gpt4 key购买 nike

我的一些代码有问题。我很肯定有一个非常简单的解决方案,我似乎看不到它!基本上我使用单个 tableview 类来显示不同的数据(内容、项目、移动)。我没有 3 个不同的表格 View Controller ,而是有一个可以跟踪哪个单元格被点击并显示相关数据。一切都很好!我正在使用带有单独 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;

}

任何帮助都会很棒!我敢肯定,我盯着屏幕看的时间让我错过了一些简单的事情。谢谢!

最佳答案

你必须改变NSString *CellIdentifier;每次使用新的自定义单元格时的值(value)。您必须输入与输入相同的值

enter image description here

- (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;

}

关于ios - 多个自定义单元格动态加载到单个表格 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13302734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com