【问题标题】:Dynamically adding data to a UITableView将数据动态添加到 UITableView
【发布时间】:2011-01-14 14:59:21
【问题描述】:

大家好!我是 Objective C 的新手,我需要一点帮助。我在 UIViewController 中创建了一个 UITableView。我想知道如何动态填充我的 UITableView。数据是我存储在 NSMutableArray 中的一堆标签。所以每一行都显示了数组的内容。一旦数组重新加载了新数据,第二行将继续显示添加到数组中的数据。提前感谢您的帮助!

【问题讨论】:

    标签: iphone objective-c uitableview


    【解决方案1】:

    @tableView 因为你想要一个特定的行,即第二行来显示数组之后的数据(即假设myArray)重新加载我建议你再制作一个NSMutableArray类型的数组假设它是copymyArray ....在重新加载 myArray 后,将 myArray 的对象提供给 copymyArray。 现在在第二行索引行给出 copymyArray........而在 tableview 的其余索引给出 myArray。 希望这可以帮助你!

    【讨论】:

    • 我不确定你的意思。请给我一个示例代码。
    • 我唯一的意思是制作一个包含新数据的另一个数组,即重新加载数组的数据....并将这个数组提供给第二行我希望你理解!
    【解决方案2】:

    就这样吧........

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *celltype=@"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:celltype];
    
    
        for (UIView *view in cell.contentView.subviews) {
            [view removeFromSuperview];
        }
        if (cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] autorelease];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.backgroundColor=[UIColor clearColor];
            //cell.textLabel.text=[[resultarray objectAtIndex:indexPath.row] valueForKey:@"Service"];
    
            cell.backgroundView=[[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cell.png"]]autorelease];
                                // [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back.png"]] autorelease];
    
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    
    
        }
    
    
        UILabel *productCodeLabel = [[UILabel alloc] initWithFrame:CGRectMake(105, 7, 60,20 )];
        productCodeLabel.textColor = [UIColor whiteColor];
        productCodeLabel.backgroundColor=[UIColor clearColor];
        productCodeLabel.text=[NSString stringWithFormat:@"%@",@"Code"];
        [cell.contentView addSubview:productCodeLabel];
        [productCodeLabel release];
    
        UILabel *CodeValueLabel = [[UILabel alloc] initWithFrame:CGRectMake(170, 7, 140,20 )];
        CodeValueLabel.textColor = [UIColor whiteColor];
        CodeValueLabel.backgroundColor=[UIColor clearColor];
        CodeValueLabel.text=[NSString stringWithFormat:@"%@",[[productArray objectAtIndex:indexPath.row]valueForKey:@"ID"]];
        [cell.contentView addSubview:CodeValueLabel];
        [CodeValueLabel release];
    
    
    
        UILabel *productNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(105, 35, 60,20 )];
        productNameLabel.textColor = [UIColor whiteColor];
        productNameLabel.backgroundColor=[UIColor clearColor];
        productNameLabel.text=[NSString stringWithFormat:@"%@",@"Name"];
        [cell.contentView addSubview:productNameLabel];
        [productNameLabel release];
    
        UILabel *NameValueLabel = [[UILabel alloc] initWithFrame:CGRectMake(170, 35, 140,20 )];
        NameValueLabel.textColor = [UIColor whiteColor];
        NameValueLabel.backgroundColor=[UIColor clearColor];
        NameValueLabel.text=[NSString stringWithFormat:@"%@",[[productArray objectAtIndex:indexPath.row]valueForKey:@"Title"]];
        [cell.contentView addSubview:NameValueLabel];
        [NameValueLabel release];
    
    
    
        UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(105, 68, 60,20 )];
        dateLabel.textColor = [UIColor whiteColor];
        dateLabel.backgroundColor=[UIColor clearColor];
        dateLabel.text=[NSString stringWithFormat:@"%@",@"Date"];
        [cell.contentView addSubview:dateLabel];
        [dateLabel release];
    
        UILabel *dateValueLabel = [[UILabel alloc] initWithFrame:CGRectMake(170, 68, 140,20 )];
        dateValueLabel.textColor = [UIColor whiteColor];
        dateValueLabel.backgroundColor=[UIColor clearColor];
        dateValueLabel.text=[NSString stringWithFormat:@"%@",[[productArray objectAtIndex:indexPath.row]valueForKey:@"PostedDate"]];
        dateValueLabel.font=[UIFont systemFontOfSize:14];
        dateValueLabel.numberOfLines=3;
        dateValueLabel.adjustsFontSizeToFitWidth=YES;
        [dateValueLabel setLineBreakMode:UILineBreakModeCharacterWrap];
        [cell.contentView addSubview:dateValueLabel];
        [dateValueLabel release];
    
        /*
         UILabel *DetailLabel = [[UILabel alloc] initWithFrame:CGRectMake(185, 15, 100,20 )];
         DetailLabel.textColor = [UIColor blackColor];
         DetailLabel.backgroundColor=[UIColor clearColor];
    
         [cell.contentView addSubview:DetailLabel];
         [DetailLabel release];*/
        return cell;
    
    }
    

    我有带键的数组,所以我也使用键。但如果你只是有没有键的数组。那么你不需要写 ValuForKey:@"Id",你可以跳过键的值。希望这个会帮助你我给你完整的代码。

    【讨论】:

    • 谢谢萨比!我尝试了类似的东西。在同一个函数下,在检查单元格是否不等于 nil 后,我从 0 开始运行一个循环,直到数组中剩余的元素数。使用循环值,我访问了每个标签并将其设置为 cell.contentView,然后释放标签。然而什么也没出现。我确信我的方法存在缺陷。
    • 你需要和我一样对单元格使用相同的方法,然后你也可以实现自己的逻辑。但是不要在 if (cell==nil)block 中做其他事情,做什么从那以后,循环 aur what ever....如果它对您有帮助,请尝试接受答案。
    • 我采用了与您提到的相同的方法。我也没有包含其他内容。完成 if 循环后,我放了一个 for 循环。我实际上有一个改变,这可能会让事情变得更容易。我希望标签仅在我按下特定按钮时显示在表格中。
    • 真的无法从我这边为您提供更多解决方案,没有看到您的代码。一次您说您正在循环,第二次您说您想在按钮上显示/隐藏标签单击。按钮在哪里,是在 tableview 单元中还是在那个之外。您可以简单地使用 if else 块并设置 label.hide=TRUE;或者在这里显示您的代码,以便您查看,帮助您
    猜你喜欢
    • 1970-01-01
    • 2014-07-15
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-27
    相关资源
    最近更新 更多