ios - 加载多种类型的 UITableViewCell

标签 ios objective-c uitableview

在我的 TableView 中,我尝试对大多数行使用默认的 TableView 单元格,但想对一行使用自定义单元格。问题是带有自定义单元格的一行。我显然没有正确加载它,因为我无法访问自定义单元格的属性。

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

最佳答案

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

关于ios - 加载多种类型的 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33435997/

相关文章:

ios 6 - MKAnnotationView 中的问题

ios - 我应该子类化还是使用类别

ios - 摆脱 UITableViewCell 自定义 accessoryView 的填充

ios - 在 Swift 中的 tableviewcell 之间添加空间

ios - 将声音附加到 SKEmitterNode

html - 在没有互联网的情况下在 iPad 上下载和查看原型(prototype)

objective-c - 使用 UIPanGestureRecognizer 围绕某个点旋转 UIView

ios - 在 UITableView 中包含选定单元格的部分之间滚动

ios - 如何在 Xamarin iOS 中获取 CGBitmap 的像素数据作为 byte[]

ios - 表格 View 单元格仅在滚动关闭然后重新打开后才能正确屏蔽