简体   繁体   English

自定义tableviewcell为零

[英]custom tableviewcell is nil

I have a table and I want to fill it with custom tableViewCells, in order to provide the user with information on orders. 我有一张桌子,我想用自定义tableViewCells填充它,以便向用户提供有关订单的信息。 The problem is that the tableview shows the cells but does not fill them with the information I need. 问题在于tableview显示了单元格,但没有用我需要的信息填充它们。 When I inserted breakpoints, I found out that the cells are nill, not matter what I do. 当我插入断点时,我发现单元格为零,不管我做什么。 this is my cellForRowAtIndexPath: 这是我的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;
}

Does anybody have an idea what i'm doing wrong? 有人知道我在做什么错吗? any help would be very appreciated. 任何帮助将不胜感激。

Add this in viewDidLoad to indicate the cell to use: 将其添加到viewDidLoad中以指示要使用的单元格:

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

If you're defining your custom cell as a Prototype Cell inside a Storyboard , make sure you have assigned the proper identifier ( orderOverviewCell ) 如果要将自定义单元格定义为StoryboardPrototype Cell ,请确保已分配了正确的标识符( orderOverviewCell

在此处输入图片说明

According to Apple developer docs 根据Apple开发人员文档

Because the prototype cell is defined in a storyboard, the dequeueReusableCellWithIdentifier: method always returns a valid cell. 由于原型单元格是在情节提要中定义的,因此dequeueReusableCellWithIdentifier:方法始终返回有效单元格。 You don't need to check the return value against nil and create a cell manually. 您无需对照nil检查返回值并手动创建一个单元格。

Couple of things to check! 几件事要检查! If you are using Storyboards and your cell is a prototype cell which is contained inside a UITableView instead of a separate Nib file then make sure the unique identifier is correct. 如果您使用的是Storyboards,并且您的单元格是包含在UITableView中而不是单独的Nib文件中的原型单元格,则请确保唯一标识符正确。

The case as well as spelling matters so make sure you have named the "orderOverviewCell" correctly in the code as well as in the storyboard when you click on the cell. 大小写和拼写都很重要,因此请确保在单击单元格时在代码以及情节提要中正确命名了“ orderOverviewCell”。

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

The above code will instantiate the cell and return a valid cell so you don't need to check for nil or anything. 上面的代码将实例化单元格并返回一个有效的单元格,因此您无需检查nil或其他任何内容。 I am also assuming that you are using UITableViewController and not a ViewController with a UITableView as an outlet. 我还假设您使用的是UITableViewController,而不是使用带有UITableView作为出口的ViewController。

将其更改为此解决了:

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

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

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