2011-03-21 35 views
0

我在uitableview上有問題, 我在分組表中使用了3個部分, 通過使用indexPath.section爲每個部分插入數據都很好,但第三部分填充了第一和第二部分數據, 如何刪除該數據以及如何填寫自己的數據意味着單獨的數據? 代碼是: -UITableView數據插入

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    if (section == 0) { 

     return @"Product Details"; 
    } 

    if (section == 1) { 

     return @"Ingredients"; 
    } 
    if (section == 2) { 
     return @"My Allergies"; 
    } 
return nil; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if (indexPath.section == 0) { 
     return 150; 
    } 
    if (indexPath.section == 1) { 
     return 100; 
    } 
    if (indexPath.section==2) { 
     return 45; 
    } 


    return 0; 

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

    } 
    NSMutableString *redStr1 =[[NSMutableString alloc] initWithString:@"No" ]; 
    NSMutableString *yellowStr1 =[[NSMutableString alloc] initWithString:@"No" ]; 
    NSMutableString *greenStr1 =[[NSMutableString alloc] initWithString:@"No" ]; 


    int a = [appDelegate.reIndex intValue]; 


    NSDictionary *aDict1 = [[NSDictionary alloc]init]; 
    aDict1 = [appDelegate.ProductArray objectAtIndex:a]; 

    // NSMutableString *str = [aDict1 objectForKey:@"IngredientInfo"]; 

    NSMutableArray *array =[aDict1 objectForKey:@"IngredientInfo1"];  
    NSMutableString *nameStr = [[NSMutableString alloc] init]; 
    NSMutableString *nameClr = [[NSMutableString alloc] init]; 



    for (int s=0; s<[array count]; s++) { 
     NSDictionary *nameDict = [array objectAtIndex:s]; 
     [nameStr appendString:[nameDict objectForKey:@"Name"]]; 
     nameClr = [nameDict objectForKey:@"HalaStatus"]; 

     if ([nameClr isEqualToString:@"Red"]) { 

      [redStr1 setString:@"Yes"]; 
     } 

     if ([nameClr isEqualToString:@"Yellow"]) { 
      [yellowStr1 setString:@"Yes"]; 
     } 

     if ([nameClr isEqualToString:@"Green"]) { 
      [greenStr1 setString:@"Yes"]; 
     } 

     if (s == [array count]-1) { 
      [nameStr appendFormat:@"."]; 
     } 
     else { 
      [nameStr appendFormat:@","]; 
     } 

    } 


if (indexPath.section == 0) 
    { 

    cell.userInteractionEnabled =NO; 
    imgview1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"images (1).jpg"] ]; 
    [imgview1 setFrame:CGRectMake(12, 2, 100, 145)]; 
    [cell addSubview:imgview1]; 
     [imgview1 release]; 

    imgview = [[UIImageView alloc]initWithFrame:CGRectMake(255,2 , 50, 45)]; 
    [cell addSubview:imgview]; 
    if ([redStr1 isEqualToString:@"Yes"]) { 
     [imgview setImage:[UIImage imageNamed:@"Red.png"]]; 
    } 
    if ([redStr1 isEqualToString:@"No"] && [yellowStr1 isEqualToString:@"Yes"]) { 
     [imgview setImage:[UIImage imageNamed:@"Yellow.png"]]; 
    } 
    if ([redStr1 isEqualToString:@"No"] && [yellowStr1 isEqualToString:@"No"] && [greenStr1 isEqualToString:@"Yes"]) { 
     [imgview setImage:[UIImage imageNamed:@"Green.png"]]; 
    } 

    } 


if (indexPath.section == 1) { 
    UITextView *textview1; 
    textview1 =[[UITextView alloc]initWithFrame:CGRectMake(12, 2, 294, 96)]; 
    textview1.text = nameStr; 
    textview1.editable =NO; 
    [textview1 setFont:[UIFont systemFontOfSize:15]]; 
    [cell addSubview:textview1]; 



} 


if (indexPath.section == 2) { 


cell.textLabel.text = [arr objectAtIndex:indexPath.row]; 

} 

return cell; 
} 
+0

問題可能是的tableView:numberOfRowsInSection :.你可以發佈該方法嗎? – Felix 2011-03-21 11:05:42

回答

0

,如果你有一個數組,並與indexPath.row訪問這cellForRowAtIndexPath:你會得到所有部分同樣可以說三大要素。

,你所要做的是內部數組的數組:

Level 1 Section 1 
    Level 2 Row 1 
    Level 2 Row 2 
    Level 2 Row 3 
Level 1 Section 2 
    Level 2 Row 1 
    Level 2 Row 2 
Level 1 Section 3 
    Level 2 Row 1 
    Level 2 Row 2 
    Level 2 Row 3 
+0

我將得到你的先生 – Anand 2011-03-21 07:13:39

+0

你把你的數據分成幾部分。第一個數組包含您的部分,每個元素包含另一個數組:數據/行。然後你可以通過指定'[[myArray objectAtIndex:section] objectAtIndex:row];' – 2011-03-21 07:16:49