【问题标题】:UITableView Data insertionUITableView 数据插入
【发布时间】:2011-03-21 06:57:35
【问题描述】:

我在 uitableview 上有问题, 我在分组表中取了 3 个部分, 使用 indexPath.section 为每个部分插入数据一切正常,但第 3 部分填充了第 1 和第 2 部分数据, 如何删除该数据以及如何填充我自己的数据意味着单独的数据? 代码是:-

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

【问题讨论】:

  • 问题可能出在 tableView:numberOfRowsInSection: 中。您也可以发布该方法吗?

标签: ios objective-c iphone xcode uitableview


【解决方案1】:

如果您有一个数组并在cellForRowAtIndexPath:indexPath.row 中访问它,您将获得所有部分相同的三个元素。

你要做的就是一个数组里面的一个数组:

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

【讨论】:

  • 您将数据拆分为多个部分。第一个数组包含您的部分,每个元素包含另一个数组:数据/行。然后你可以通过指定[[myArray objectAtIndex:section] objectAtIndex:row];来访问它
猜你喜欢
  • 1970-01-01
  • 2020-09-17
  • 1970-01-01
  • 2013-01-09
  • 2022-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-17
相关资源
最近更新 更多