Multiple Custom Cells dynamically loaded into a si

2020-08-01 06:32发布

问题:

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.

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.

I have tried using

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!

回答1:

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;

}