【问题标题】:UITableViewCell with dynamic number of UILables具有动态 UILable 数量的 UITableViewCell
【发布时间】:2012-05-17 04:40:24
【问题描述】:

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

请阅读评论以查看链接

我的实现是:
1. 我用笔尖创造我的细胞! 请阅读评论以查看链接

  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 不让我做发布行。

【问题讨论】:

  • 你想动态扩展uitableviewcell吗?
  • 是的!!!任何想法如何实现它..以更好/漂亮的方式:D

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


【解决方案1】:

您要将标签添加到 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-18
    • 2012-10-18
    • 2013-09-02
    • 2014-10-18
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多