将多个自定义单元动态加载到单个表视图中 [英] Multiple Custom Cells dynamically loaded into a single tableview
问题描述
我的某些代码有问题。我非常肯定有一个非常简单的解决方案,我似乎看不到它!基本上,我使用单个tableview类来显示不同的数据(内容,项目,移动)。我没有使用3个不同的表视图控制器,而是跟踪一个单元格并显示相关数据。一切都很好!我将自定义单元格类与单独的nib文件一起使用,因此我需要设法正确实现if语句(或switch语句),该语句检查被点击的单元格(self.title),然后基于该单元格创建自定义单元格。
I am having an issue with some of my code. I am quite positive there is a very simple solution, I just cant seem to see it! Basically I am using a single tableview class to display different data (content, items, moves). Instead of having 3 different table view controllers, I have one that keeps track of which cell is tapped and show the relevant data. All works really well! I am using custom cell classes with a separate nib file, so I need to way to properly implement an if statement (or a switch statement) that checks what cell was tapped (self.title) and then create the custom cell based on that.
显然,我遇到的范围问题是cell.imageView不知道它所引用的是什么,因为当它在if语句中时,它不在范围之内。
Obviously the issue I am having is with scope, cell.imageView has no idea what it is referencing because it is out of scope when its in an if statement.
我尝试使用
id cell;
我遇到相同的错误。对我来说,使每个标题使用不同的自定义单元格的方法是什么?
I get the same errors. What is a way for me to accomplish this so each "title" uses a different custom 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;
}
任何帮助都是伟大的!我确定我数小时的盯着屏幕已经使我错过了一些简单的事情。谢谢!
Any help would be great! I'm sure that my hours of staring at the screen have caused me to miss something simple. Thanks!
推荐答案
您必须每次更改 NSString * CellIdentifier;
的值您使用新的自定义单元格。
you have to change NSString *CellIdentifier;
value every time when you use new custom cell. you have to put same value which you enter
- (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;
}
这篇关于将多个自定义单元动态加载到单个表视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!