質問

I have a table and I want to fill it with custom tableViewCells, in order to provide the user with information on orders. The problem is that the tableview shows the cells but does not fill them with the information I need. When I inserted breakpoints, I found out that the cells are nill, not matter what I do. this is my 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.

役に立ちましたか?

解決 4

changing it to this solved it:

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

他のヒント

Add this in viewDidLoad to indicate the cell to use:

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)

enter image description here

According to Apple developer docs

Because the prototype cell is defined in a storyboard, the dequeueReusableCellWithIdentifier: method always returns a valid cell. You don’t need to check the return value against nil and create a cell manually.

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.

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 *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. I am also assuming that you are using UITableViewController and not a ViewController with a UITableView as an outlet.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top