Objective c 如何使用indexpath进行xml解析
我使用xml解析器将数据从一个视图传递到另一个视图。当我单击任何一行(在视图上加载xml后),它应该相应地显示整个详细信息。 -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{Objective c 如何使用indexpath进行xml解析,objective-c,ios5,nsxmlparser,nsindexpath,Objective C,Ios5,Nsxmlparser,Nsindexpath,我使用xml解析器将数据从一个视图传递到另一个视图。当我单击任何一行(在视图上加载xml后),它应该相应地显示整个详细信息。 -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{ static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView
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>
三星银河
智能手机
250$
三星galaxy1
智能电话1
251$
三星galaxy2
智能电话2
252$
三星galaxy3
智能电话3
350$
当我单击第一行时,它应该显示相同的数据。但是它是按照选择的顺序显示的。
如何解决这个问题?你可以这样传递索引
- (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];
}
你可以像那样传递索引
- (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];
}
你有对象的数组…?不。我没有。@nitin GoHelp请阅读我的答案我只是更改你问题的代码并将其作为我的答案你只是复制并通过CellForRowAtIndex路径你有对象的数组…?不。我没有。@nitin GoHelp请阅读我的答案我只是更改你问题的代码并将其作为我的答案,你只是复制并通过st在CellForRowAtIndexPath中上述代码有效。谢谢:)但在第一次单击后,它不会出现在任何地方。当我第二次单击时,它会出现在第一次单击后必须显示的选项中。请检查您是否选择了委托方法上述代码有效,但所选的第一行没有任何功能,除非有已选择其他行。它正在显示前一行的数据。如何解决此问题?@nitin Gohel您在选择特定行时是否正在推送视图或做什么…?让我们使用上面的代码。谢谢:)但在第一次单击后,它将不在任何位置。当我第二次单击时,它将转到第一次单击后必须显示的选项。please check you did selected delegate method上述代码正在运行,但除非选择了任何其他行,否则所选的第一行不会执行任何操作。它正在显示前一行的数据。如何解决此问题?@nitin Gohel您是在推送视图还是在选择特定行时做了什么?让我们来看看