2012-02-24 129 views
0
- (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來填充,然後我應該怎麼辦的數據。在iphone填充動態

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

我想一旦你的陣列已經用新的數據更新,從數據庫表中填充數據,動態

+0

你是什麼意思「從數據庫填充」? – dbrajkovic 2012-02-24 05:22:18

+0

你想做什麼?... – EmilioPelaez 2012-02-24 05:23:14

+0

其實我想從sqllite db中獲取信息並在grouprd風格的tableview中顯示它。編號數據庫中的記錄可以動態更改。所以我怎麼能顯示數據的單元格中的一行特別section.Hope你明白我可能說 – Mania 2012-02-24 05:27:27

回答

1

,你只需要調用[self.tableview reloadData];

+0

我的數據庫表結構: 名稱,地址,聯繫方式,工資,技術(列名) //我要怎麼顯示:表視圖分組風格:SECTION1小區1小區2小區3小區4 第2節 小區1 小區2 小區3 小區4 爲前。 約翰(SECTION1 - 名稱字段) 紐約(小區1 - 地址欄) 9999999(小區2 - 接觸場) 12000 $(小區3 - 薪水場) iPhone(小區4 - 高科技) – Mania 2012-02-24 06:13:17

+0

那麼你可以使用switch語句,或者然後是其他語句。然後根據段號和行號根據你想要的內容設置單元格的內容。因此,如果第0行0 cell.textLabel.text =名稱 – dbrajkovic 2012-02-24 06:27:41

+0

好frnds我的prolem現在解決了填充數據庫中的數據... – Mania 2012-02-24 07:18:31