简体   繁体   中英

Loading Multiple Types of UITableViewCell

In my table view I am trying to use the default table view cell for most of the rows but want to use a custom cell for one row. The problem is that the one row with a custom cell. I'm clearly not loading it correctly because I can't access the properties of the custom cell.

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];

    [self.tableView registerNib:[UINib nibWithNibName:@"KeepLoginCell" bundle:nil]
         forCellReuseIdentifier:@"KeepLoginCell"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = nil;

    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor whiteColor];

    if (indexPath.section == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];

        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
        }

        if (indexPath.row == 0) {
            cell.textLabel.text = @"Recent Purchases";
        }
    } else if (indexPath.section == 1) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];

        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
        }

        if (indexPath.row == 0) {
            cell.textLabel.text = @"Payment Method";
        }
    } else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            cell = (KeepLoginCell *)[tableView dequeueReusableCellWithIdentifier:@"KeepLoginCell"];

            if (!cell) {
                cell = [[KeepLoginCell alloc]init];
            }
        } else if (indexPath.row == 1) {
            cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];

            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
            }

            cell.textLabel.text = [self.settingsArray objectAtIndex:indexPath.row];
        }
    }

    return cell;
}

In this line :

cell = (KeepLoginCell *)[tableView dequeueReusableCellWithIdentifier:@"KeepLoginCell"];

Even if you cast the dequeue... message to return a KeepLoginCell* , your cell value is still declared as a generic UITableViewCell . Thus the compiler/static analyzer/xcode editor hints only show you what's relevant to UITableViewCell , not your custom subclass.

Use this instead:

KeepLoginCell *klcell = (KeepLoginCell *)[tableView dequeue…
[klcell setCustomProperty:…];
cell = klcell;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];    
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    }

    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor whiteColor];

    if (indexPath.section == 0) {                
        if (indexPath.row == 0) {
            cell.textLabel.text = @"Recent Purchases";
        } else { 
            //TODO: handle this case
        }
    } else if (indexPath.section == 1) {    
        if (indexPath.row == 0) {
            cell.textLabel.text = @"Payment Method";
        } else { 
            //TODO: handle this case
        }
    } else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            KeepLoginCell *customCell = (KeepLoginCell *)[tableView dequeueReusableCellWithIdentifier:@"KeepLoginCell"];

            if (!customCell) {
                customCell = [[KeepLoginCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"KeepLoginCell"];
            }
            //TODO: set values to your custom cell
            //e.g. customCell.myLabel.text = ...

            cell = customCell; 
        } else if (indexPath.row == 1) {
            cell.textLabel.text = [self.settingsArray objectAtIndex:indexPath.row];
        } else { 
            //TODO: handle this case
        }
    }

    return cell;
}

The problem is that you don't have a variable of type KeepLoginCell anywhere. So where you're sure that your cell is a KeepLoginCell , you'd either have to cast it on every call:

 [((KeepLoginCell*)cell) keepIt];

or you could just put it in a variable of this type

KeepLoginCell* keepLoginCell = (id)cell;
[keepLoginCell keepIt];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM