开发者

Dynamically adding data to a UITableView

Hey guys! I'm new to Objective C and I need a little help. I've created a UITableView in a UIViewController. I'd like to know how to populate my UITableView dynamically. The data is a bunch of labels that I've stored in a NSMutableArray. So each row displays the contents of the array. Once the array is reloaded with fresh data, the second row will then keep displaying the data as it is a开发者_如何学Pythondded to the array. Thanks in advance for any help!


@tableView as you want a particular row i.e second row to display the data after the array(i.e suppose myArray) reload for that i suggest u to make one more array of NSMutableArray type suppose it is copymyArray....and after the reload of myArray give the objects of myArray to copymyArray. Now at second row index row give copymyArray........and on rest index of tableview give myArray. Hope this may help u!


Just do this way..................

- (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;

}

I have array with keys,so i am using key as well.But if you simply have array without keys.Then you don't need to write ValuForKey:@"Id",You can skip value for key.Hope this will help You i gave you the complete code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜