简体   繁体   English

使用同一TableViewCell原型的多个自定义UITableViewCell

[英]Multiple custom UITableViewCell using the same TableViewCell prototype

I like to see what are my options to do this in iOS7. 我想看看在iOS7中有哪些选择。 I have a TableViewCell prototype with identifier TableViewCell_RightDetailID in an UITableViewController . 我在UITableViewController有一个标识符为TableViewCell_RightDetailID的TableViewCell原型。 I also have two other custom tableviewcell classes that would like to use this prototype. 我还有另外两个想要使用此原型的自定义tableviewcell类。

Any ideas on how to do this? 有关如何执行此操作的任何想法? Not sure if its possible? 不确定是否可能?

@interface TableViewCellA : UITableViewCell
@interface TableViewCellB : UITableViewCell

在此处输入图片说明

I think something like this should work: 我认为这样的事情应该起作用:

TableViewController.m TableViewController.m

#import "TVC.h"
#import "TableViewCellA.h"
#import "TableViewCellB.h"

@implementation TVC

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0)
    {
        TableViewCellA *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell_RightDetailID"];
        // title, detaillabel etc
        return cell;

    }
    else
    {
        TableViewCellB *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell_RightDetailID"];
        // TO DO
        return cell;
    }
}

@end

Since the class of the cell is set in the prototype in the storyboard, it's not possible to change it short of copying the prototype in the storyboard (and using distinct reuse identifiers) 由于单元的类别是在情节提要中的原型中设置的,因此除非在情节提要中复制原型(并使用不同的重用标识符),否则无法进行更改。

This actually might be an argument for doing the layout in code and registering all three classes via [UITableView registerClass:forCellReuseIdentifier:] 实际上,这可能是用于在代码中进行布局并通过[UITableView registerClass:forCellReuseIdentifier:]注册所有三个类的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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