在iphone中动态填充数据

时间:2012-02-24 05:20:02

标签: objective-c ios uitableview

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return [self.LoadFile count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 4;
}
 int title =0;

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    for (title = 0 ; title <= section; title++)
    {
        NSLog(@"section - %d ",title);
        NSDictionary *dict = [LoadFile objectAtIndex:title];
        NSString *titlename = [[NSString alloc]initWithFormat:@"%@",[dict valueForKey:@"name"]];
        NSLog(@"%@",titlename);
        if (section == title)
            return titlename;
    }
}

这里我使用的是在页面本身上创建的两个数组,在这种情况下它可以正常工作但是当我需要它从DB填充时我应该怎么做。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    while (i < sec)
    {


    dict = [LoadFile objectAtIndex:0];
    NSString *addr = [[NSString alloc]initWithFormat:@"%@",[dict valueForKey:@"address"]];
    NSString *con = [[NSString alloc]initWithFormat:@"%@",[dict valueForKey:@"contact"]];
    NSString *sal = [[NSString alloc]initWithFormat:@"%@",[dict valueForKey:@"salary"]];
    NSString *tec = [[NSString alloc]initWithFormat:@"%@",[dict valueForKey:@"tech"]];       

    NSArray *arr = [[NSArray alloc] initWithObjects:addr,con,sal,tec,nil];
  // NSArray *arr = [[NSArray alloc] initWithObjects:i,i,i,i,nil];
      //NSArray *arr2 = [[NSArray alloc] initWithObjects:@"12",@"22",@"32",@"42",nil];
    NSLog(@"%@",arr);
    if(indexPath.section == i)
    {
        cell.textLabel.text = [arr objectAtIndex:indexPath.row];
        i++;
    }


}
    return cell;
}

    for (i = 0 ; i <= sec-1; i++)
    {
        NSLog(@"section - %d ",title);
        NSDictionary *dict = [LoadFile objectAtIndex:title];
        if (indexPath.section == 0)
        { 
            UILabel *Address = [[UILabel alloc]initWithFrame:CGRectMake(1,9, 200, 20)];
            Address.textColor =  [UIColor blueColor];
            Address.backgroundColor = [UIColor clearColor];
            [Address setFont:[UIFont boldSystemFontOfSize:14.0]];
            [Address setTag:i];
            Address.text = [dict objectForKey:@"address"];
            [cell.contentView addSubview:Address];
            UILabel *tech = [[UILabel alloc]initWithFrame:CGRectMake(1,9, 200, 20)];
            tech.textColor =  [UIColor blueColor];
            tech.backgroundColor = [UIColor clearColor];
            [tech setFont:[UIFont boldSystemFontOfSize:14.0]];
            [tech setTag:i];
            tech.text = [dict objectForKey:@"tech"];
            [cell.contentView addSubview:tech];

        }
        else if (indexPath.section == 1)
        {

            UILabel *tech = [[UILabel alloc]initWithFrame:CGRectMake(1,9, 200, 20)];
            tech.textColor =  [UIColor blueColor];
            tech.backgroundColor = [UIColor clearColor];
            [tech setFont:[UIFont boldSystemFontOfSize:14.0]];
            [tech setTag:i];
            tech.text = [dict objectForKey:@"tech"];
            [cell.contentView addSubview:tech];
          }
   }
   else if (indexPath.section == 0)
    {
        UILabel *contact = [[UILabel alloc]initWithFrame:CGRectMake(1,9, 200, 20)];
        contact.textColor =  [UIColor blueColor];
        contact.backgroundColor = [UIColor clearColor];
        [contact setFont:[UIFont boldSystemFontOfSize:14.0]];
        contact.text = [dict objectForKey:@"contact"];
        // NSLog(@"Val-%@",[dict objectForKey:@"name"]);
        [contact setTag:-4];
        [cell.contentView addSubview:contact];
    }
    else if (indexPath.section == 0)
    {
        UILabel *salary = [[UILabel alloc]initWithFrame:CGRectMake(1,9, 200, 20)];
        salary.textColor =  [UIColor blueColor];
        salary.backgroundColor = [UIColor clearColor];
        [salary setFont:[UIFont boldSystemFontOfSize:14.0]];
        salary.text = [dict objectForKey:@"salary"];
        // NSLog(@"Val-%@",[dict objectForKey:@"name"]);
        [salary setTag:-4];
        [cell.contentView addSubview:salary];
    }
    else
    {
        UILabel *tech = [[UILabel alloc]initWithFrame:CGRectMake(1,9, 200, 20)];
        tech.textColor =  [UIColor blueColor];
        tech.backgroundColor = [UIColor clearColor];
        [tech setFont:[UIFont boldSystemFontOfSize:14.0]];
        tech.text = [dict objectForKey:@"tech"];
        // NSLog(@"Val-%@",[dict objectForKey:@"name"]);
        [tech setTag:-4];
        [cell.contentView addSubview:tech];
}

我想动态地从数据库中的表填充数据

1 个答案:

答案 0 :(得分:1)

使用新数据更新数组后,只需调用[self.tableview reloadData];