我正在使用 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>
当我单击第一行时,它应该显示相同的数据。但它按选择的顺序显示。如何解决这个问题?