动态添加数据到UITableView

时间:2011-01-14 05:59:40

标签: iphone objective-c uitableview

嘿伙计们!我是Objective C的新手,我需要一些帮助。我在UIViewController中创建了一个UITableView。我想知道如何动态填充我的UITableView。数据是我存储在NSMutableArray中的一堆标签。因此每行显示数组的内容。一旦数组重新加载了新数据,第二行将在数据添加到数组时继续显示数据。在此先感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

@tableView因为你想要一个特定的行,即第二行显示数组之后的数据(即假设myArray)重新加载我建议你再做一个NSMutableArray类型的数组假设它是copymyArray ....并且在重新加载myArray之后,将myArray的对象赋予copymyArray。 现在在第二行索引行给copymyArray ........并在tableview的rest索引上给myArray。 希望这可以帮助你!

答案 1 :(得分:0)

就这样做..................

- (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”,你可以跳过key的值。希望这个将帮助你我给你完整的代码。