如何使用indexpath进行xml解析

时间:2012-11-21 08:02:18

标签: objective-c ios5 nsxmlparser nsindexpath

我使用xml解析器将数据从一个视图传递到另一个视图。当我单击任何行时(在视图上加载xml之后),它应该相应地显示整个细节。      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    UILabel *titleLabel = nil;
    UILabel *priceLabel = nil;

    UILabel *titleValueLabel = nil;
    UILabel *priceValueLabel = nil;

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

        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
        titleLabel.text = @"Model:";

        priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 50, 20)];
        priceLabel.text = @"Price:";


        priceValueLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 20, 50, 20)];
        titleValueLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 1, 320, 20)];

        [cell.contentView addSubview:titleLabel];
        [cell.contentView addSubview:priceLabel];


        [cell.contentView addSubview:titleValueLabel];
        [cell.contentView addSubview:priceValueLabel];

    }


    NSDictionary *tempProduct = [self.listData objectAtIndex:indexPath.row];
    NSDictionary *productTitle = [tempProduct valueForKey:TITLE];
    NSString *titleValue = [productTitle valueForKey:TEXT];
    titleValue = [titleValue stringByTrimmingCharactersInSet:
              [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    titleValueLabel.text = titleValue;
    [cell.contentView addSubview:titleValueLabel];


    NSDictionary *productPrice = [tempProduct valueForKey:PRICE];
    NSString *priceValue = [productPrice valueForKey:TEXT];
    priceValue = [priceValue stringByTrimmingCharactersInSet:
              [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    priceValueLabel.text = priceValue;
    [cell.contentView addSubview:priceValueLabel];


    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

我的样本xml数据是

<?xml version="1.0" encoding="UTF-8"?>
<scan>
    <products>
        <title>samsung galaxy</title>
        <desc>smart phone</desc>
        <price>250$</price>
    </products>
    <products>
        <title>samsung galaxy1</title>
        <desc>smart phone1</desc>
        <price>251$</price>
     </products>
    <products>
        <title>samsung galaxy2</title>
        <desc>smart phone2</desc>
        <price>252$</price>
    </products>
    <products>
        <title>samsung galaxy3</title>
        <desc>smart phone3</desc>
        <price>350$</price>
    </products>
    </scan>

当我点击第一行时,它应显示相同的数据。但它按选择的顺序显示。 如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

你可以像那样传递索引

- (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];
        NSDictionary *tempProduct = [self.listData objectAtIndex:indexPath.row];
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
       [Name setText:[tempProduct valueForKey:@"TITLE"]];

        priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 50, 20)];
        [Name setText:[tempProduct valueForKey:@"Price"]];


        priceValueLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 20, 50, 20)];
        titleValueLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 1, 320, 20)];

        [cell.contentView addSubview:titleLabel];
        [cell.contentView addSubview:priceLabel];


        //[cell.contentView addSubview:titleValueLabel];
       // [cell.contentView addSubview:priceValueLabel];

    }



    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

修改

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


            NSDictionary *tempProduct = [self.listData objectAtIndex:indexPath.row];

          objAppDelegate.strCatagoryTitle=[tempProduct valueForKey:@"TITLE"];
          objAppDelegate.strCatagoryPrice=[tempProduct valueForKey:@"Price"];

//hear  objAppDelegate.strCatagoryTitle or objAppDelegate.strCatagoryPrice globle string.
//we can use it at our cntrSecondViewController class using this Delegate object 
//objAppDelegate = (cntrAppDelegate1 *) [[UIApplication sharedApplication]delegate];


 cntrSecondViewController *cntrinnerService = [[cntrSecondViewController alloc] initWithNibName:@"cntrSecondViewController" bundle:nil];
        [self.navigationController pushViewController:cntrinnerService animated:YES];


}