2011-01-14 10 views
0

ちょっと!私はObjective Cの新人です。少し助けが必要です。私はUIViewControllerでUITableViewを作成しました。私は私のUITableViewを動的に取り込む方法を知りたいです。データは、私がNSMutableArrayに格納した一連のラベルです。各行には配列の内容が表示されます。配列に新しいデータが再ロードされると、2番目の行は配列に追加されたデータを表示し続けます。助けをあらかじめありがとう!動的にデータをUITableViewに追加する

答えて

0

あなたは、配列の後にデータを表示するために、特定の行、すなわち第二列にしたいと@tableView(すなわちmyArrayを仮定)私はuがNSMutableArrayタイプの1つの以上の配列は、それがcopymyArrayであると仮定作ることを示唆しているためリロード....とmyArrayのリロード後にmyArrayのオブジェクトにcopymyArrayを与えます。 2行目のインデックスの行にcopymyArrayを指定し、残りのテーブルビューにmyArrayを付けます。 これがあなたを助けてくれることを願っています!

+0

私はあなたが何を意味するか分かりません。サンプルコードを私に見せてください。 – narda

+0

私の唯一の手段は、フレッシュなデータを含む別の配列、つまりリロードされた配列のデータを作成することです。この配列を2行目に渡します。 – Sudhanshu

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の値をスキップすることができますあなたが私はあなたに完全なコードを教えてくれます。

+0

ありがとうsabby!私は似たようなことを試みた。同じ関数の下で、cellがnilと等しくないかどうかをチェックした後、配列の要素数まで0から始まるループを実行しました。ループ値を使用して、各ラベルにアクセスしてcell.contentViewとして設定し、ラベルを解放しました。しかし何も現れなかった。私のアプローチに欠陥があると確信しています。 – narda

+0

あなたは私のやり方と同じアプローチをしなければなりませんし、独自のロジックを実装することもできます。しかしif(cell == nil)ブロックで他のことをしないでください。いつも...そしてそれがあなたを助けたら答えを受け入れるようにしてください。 – Sabby

+0

私はあなたが言及したのと同じアプローチに従ってきました。私はもう一つも含めていない。私はifループを完了した後、私はforループを入れます。私は実際に物事を簡単にするかもしれない変化を持っています。特定のボタンを押したときにのみ、ラベルをテーブルに表示します。 – narda

関連する問題