繁体   English   中英

自定义tableviewcell为零

[英]custom tableviewcell is nil

我有一张桌子,我想用自定义tableViewCells填充它,以便向用户提供有关订单的信息。 问题在于tableview显示了单元格,但没有用我需要的信息填充它们。 当我插入断点时,我发现单元格为零,不管我做什么。 这是我的cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OrderOverViewCell *cell = (OrderOverViewCell *)[tableView dequeueReusableCellWithIdentifier: @"orderOverviewCell"];

    OrderClass *orderClass = nil;
    if (filteredArray == nil) {
        if (sortedArray.count >0) {
        orderClass = sortedArray[indexPath.row];
        }
    }
    else if (filteredArray != nil) {
        orderClass = filteredArray[indexPath.row];
    }

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm"];
    NSString *orderDate = [dateFormatter stringFromDate:orderClass.orderDate];

    cell.orderTitle.text = [NSString stringWithFormat:@"%@%@ %@",orderClass.companyName,@",", orderDate];
    cell.orderDetail.text = [NSString stringWithFormat:@"%@ %@ %@ %@.", @"order nummer:",orderClass.orderNumber, @"betaling:", orderClass.orderPaymentType];


    if ([orderClass.orderDelivery isEqualToString:@"Bezorgen"]) {
        cell.orderDelivery.text = [NSString stringWithFormat:@"Levering: Bezorgen op %@, %@, %@", orderClass.orderAdress, orderClass.orderAdressZip, orderClass.orderCity];
    }

    if ([orderClass.orderDelivery isEqualToString:@"Afhalen"]) {
        cell.orderDelivery.text = [NSString stringWithFormat:@"Levering: Afhalen op ingestelde datum en tijd."];
    }

    cell.orderIndication.image = nil;
    if ([orderClass.preparing isEqualToString:@"1"]) {
        if ([orderClass.ready isEqualToString:@"1"] ){
            if ([orderClass.delivered isEqualToString:@"1"]){
                cell.orderIndication.image = [UIImage imageNamed:@"order_3.png"];
            }
            else {
                cell.orderIndication.image = [UIImage imageNamed:@"order_2.png"];
            }
        }
        else {
            cell.orderIndication.image = [UIImage imageNamed:@"order_1.png"];
        }
    }
    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"orderCellBG.png"]];

    return cell;
}

有人知道我在做什么错吗? 任何帮助将不胜感激。

将其添加到viewDidLoad中以指示要使用的单元格:

UINib *const cell = [UINib nibWithNibName:@"nibName" bundle:[NSBundle mainBundle]];
[_tableView registerNib:cell forCellReuseIdentifier:@"orderOverviewCell"];

如果要将自定义单元格定义为StoryboardPrototype Cell ,请确保已分配了正确的标识符( orderOverviewCell

在此处输入图片说明

根据Apple开发人员文档

由于原型单元格是在情节提要中定义的,因此dequeueReusableCellWithIdentifier:方法始终返回有效单元格。 您无需对照nil检查返回值并手动创建一个单元格。

几件事要检查! 如果您使用的是Storyboards,并且您的单元格是包含在UITableView中而不是单独的Nib文件中的原型单元格,则请确保唯一标识符正确。

大小写和拼写都很重要,因此请确保在单击单元格时在代码以及情节提要中正确命名了“ orderOverviewCell”。

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

上面的代码将实例化单元格并返回一个有效的单元格,因此您无需检查nil或其他任何内容。 我还假设您使用的是UITableViewController,而不是使用带有UITableView作为出口的ViewController。

将其更改为此解决了:

OrderOverViewCell *cell = (OrderOverViewCell *)[self.tableView dequeueReusableCellWithIdentifier: @"orderOverviewCell"];

暂无
暂无

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

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