2011-01-14 77 views
0

嘿傢伙!我是Objective C新手,需要一些幫助。我在UIViewController中創建了一個UITableView。我想知道如何動態地填充我的UITableView。數據是我存儲在NSMutableArray中的一堆標籤。所以每行顯示數組的內容。一旦數組重新載入新數據,第二行將隨着數據添加到數組而繼續顯示數據。預先感謝任何幫助!動態添加數據到UITableView

回答

0

@tableView你想要一個特定行即第二行陣列之後顯示的數據(即假設myArray)重新加載,我建議ü使NSMutableArray類型的一個以上的陣列假設它是copymyArray ....和在myArray的重載之後,將myArray的對象賦予copymyArray。 現在在第二行索引行給copymyArray ........和tableview的rest索引給myArray。 希望這可以幫助你!

+0

我不確定你的意思。請給我看一個示例代碼。 – narda 2011-01-14 07:48:10

+0

我唯一的方法就是創建一個包含新數據的另一個數組,即重載數組的數據....並將此數組放到第二行,我希望你明白! – Sudhanshu 2011-01-14 08:11:49

0

只要做到這樣..................

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *[email protected]"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; 

} 

我有一個按鍵陣列,所以如果你我使用的密鑰well.But只需要有沒有鍵的數組。然後你不需要寫ValuForKey:@「Id」,你可以跳過key.Hope的值,這將有助於你我給了你完整的代碼。