0

我试图实现一个像这样的表格视图

请阅读评论以查看链接<1>

我的实现是:
1. 我用笔尖创建我的单元格!请阅读评论以查看链接<2>

  1. 出于测试目的,我对单元格的标题部分进行硬编码,然后在代码中创建其余部分,如下所示......

    - (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. 当然我也重写了 heightForRowAtIndexPath: 我终于得到了这个......

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

  1. 一切似乎都正常:D所有标签都是动态创建的,位置完美……然后我试图保存任何其他记录……然后崩溃了:'(

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

  1. 我使用 NSLOG 检查输入这些单元格的数据,下面是我的“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. 数据输入正确,我不知道出了什么问题!

为什么标签没有正确更新,我需要发布它们吗?但我不能发布,因为 ARC 不让我做发布线。

4

1 回答 1

0

您正在将标签添加到 self.view?论文应该在 tableviewcell 上。

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

[self.view addSubview:orderConditionLabel];

此外,您的 tableviewcell 重用应该看起来更像这样

- (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;
}
于 2012-05-17T04:55:16.093 回答