UITableViewCell具有动态数量的UILables

时间:2012-05-17 04:40:24

标签: ios dynamic uitableview uilabel automatic-ref-counting

我试图实现像这样的表视图

请阅读评论以查看链接< 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:我终于得到了这个......
  3. http://i.stack.imgur.com/cF7Y4.png

    1. 一切似乎都是正确的:D所有标签都是动态创建的,并且位置变得完美......然后我试图保存任何其他记录....并且崩溃了:'(
    2. 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. 数据输入正确,我不知道出了什么问题!

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

1 个答案:

答案 0 :(得分: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;
}