缺少自定义UITableViewCell中的附件

时间:2014-06-05 07:12:29

标签: ios objective-c uitableview

在为UITableView创建自定义单元格之前,我使用它来渲染行:

static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

然后我创建了两个UILabels的CustomCell 并用以下代码替换上面的代码:

static NSString *CellIdentifier = @"customCell";

    OrderCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil){
        [tableView registerNib:[UINib nibWithNibName:@"MyCustomCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

但现在配件不可见了。 它只显示在第一行,它看起来不像以前 在创建自定义单元格时,还有什么我需要做的才能显示附件吗?

3 个答案:

答案 0 :(得分:2)

修改您的代码,如下所示。将附件类型设置为条件之外..

static NSString *CellIdentifier = @"customCell";

OrderCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell == nil){
    [tableView registerNib:[UINib nibWithNibName:@"MyCustomCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


}
 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

您已将accessoryType设置在条件if(cell == nil)第一次调用..

希望它解决问题..

答案 1 :(得分:1)

如果您正在使用情节提要,那么您必须在viewDidLoad

中注册您的自定义笔尖
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"YourClass" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"YourCellIdentifier"];
}

如果您不使用情节提要,则必须在cellForRowAtIndexPath

中注册
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"YourCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if(!cell)
    {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"YourCustomClass" owner:self options:nil] lastObject];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    ////

    return cell;
}

答案 2 :(得分:0)

您必须在 viewDidLoad 中注册您的笔尖,而不是 cellForRowAtIndexPath

- (void)viewDidLoad
{
   [super viewDidLoad];
   static NSString *CellIdentifier = @"customCell";
   [tableView registerNib:[UINib nibWithNibName:@"MyCustomCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
}