0

I was trying to implement a tableview like this one

please read the comment to see the link <1>

My implementation is:
1. i create my cell from a nib! please read the comment to see the link <2>

  1. For testing purpose i hardcode the heading part of the cell, and i create the rest in the code like this ...

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:                (NSIndexPath *)indexPath
    {
        UITableViewCell *myCell = (UITableViewCell*) 
        [self.tableView dequeueReusableCellWithIdentifier:@"CheckedOutOrderTableCell"];
    
        [[NSBundle mainBundle] loadNibNamed:@"CheckedOutOrderTableCell" owner:self options:NULL];
        myCell = nibLoadedTableCell;
    
        UILabel *orderConditionLabel = [[UILabel alloc] initWithFrame:CGRectMake(250, (85+([[bigDictionary objectAtIndex:indexPath.row]count]*35)), 64, 21)];
        [orderConditionLabel setText:@"Delivered"];
        [orderConditionLabel setFont:[UIFont systemFontOfSize:14]];
        orderConditionLabel.textColor = [UIColor darkGrayColor];
        orderConditionLabel.backgroundColor = [UIColor clearColor];
    
        for(NSInteger i=0; i<[[bigDictionary objectAtIndex:indexPath.row]count]; i++)
        {
            UILabel *quantityLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 85+(i*35), 42, 21)];  
            UILabel *foodLabel = [[UILabel alloc] initWithFrame:CGRectMake(85, 85+(i*35), 175, 21)];
            UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(220, 85+(i*35), 60, 21)];
    
            [quantityLabel setFont:[UIFont systemFontOfSize:15]];
            [foodLabel setFont:[UIFont systemFontOfSize:15]];
            [priceLabel setFont:[UIFont systemFontOfSize:15]];
    
            [quantityLabel setText:[NSString stringWithFormat:@"%@",[[[bigDictionary objectAtIndex:indexPath.row]objectAtIndex:i]valueForKey:@"quantity"]]];           
            [foodLabel setText:[NSString stringWithFormat:@"%@",[[[bigDictionary objectAtIndex:indexPath.row]objectAtIndex:i]valueForKey:@"item_name"]]];        
            [priceLabel setText:[NSString stringWithFormat:@"$ %@.00", [[[bigDictionary objectAtIndex:indexPath.row]objectAtIndex:i]valueForKey:@"price"]]];   
    
            [self.view addSubview:quantityLabel];
            [self.view addSubview:foodLabel];
            [self.view addSubview:priceLabel]; 
            quantityLabel = nil;
            foodLabel = nil;
            priceLabel = nil;
        }
        [self.view addSubview:orderConditionLabel];
        orderConditionLabel = nil;
        return myCell;
    }
    
  2. and of course i also rewrite heightForRowAtIndexPath: and i finally got this one...

https://i.stack.imgur.com/cF7Y4.png

  1. everything seems going right :D all the label are dynamic created and the position goes perfect... then i tried to save any other record.... and got crashed :'(

https://i.stack.imgur.com/RAXMF.png

  1. I check the data I fed into those cells using NSLOG and below is the structure of my "bigDictionary"

    2012-05-17 12:17:50.330 LazBuy[53187:11903] show structure (
            (
                    {
                "item_name" = "\U8292\U679c\U9752\U8336";
                price = 30;
                quantity = 1;
            },
                    {
                "item_name" = "\U6587\U5c71\U6e05\U8336";
                price = 20;
                quantity = 3;
            },
                    {
                "item_name" = "\U8292\U679c\U9752\U8336";
                price = 30;
                quantity = 3;
            }
        ),
            (
                    {
                "item_name" = "\U51cd\U9802\U70cf\U9f8d\U8336";
                price = 15;
                quantity = 3;
            }
        )
    )
    
  2. The data are fed properly, I do not know what is going wrong!

Why the label didnt update properly, do I need to release them? But I cant release as ARC do not let me do the release line.

3

1 Answer 1

0

You are adding the labels to self.view? Theses should be on the tableviewcell.

[self.view addSubview:quantityLabel];
[self.view addSubview:foodLabel];
[self.view addSubview:priceLabel];

and

[self.view addSubview:orderConditionLabel];

Also your tableviewcell reuse should look more like this

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
 static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"TVCell" owner:self options:nil];
        cell = tvCell;
        self.tvCell = nil;
    }

    // Set your labels text

    return cell;
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.