using JSON how to pass data from table view to section view on iphone
i have all that data with me in tableview but when i dont know how to transfer these values on section view when user press any cell
this is how i am getting values [[jsonArray objectAtIndex:indexPath.row] objectForKey:@"number"];
now how to transfer these values to section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
static NSString *CellIdentifier = @"Cell";
cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil ) {
NSLog(@" inside");
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10.0, 5.0, 220.0, 15.0)] autorelease];
mainLabel.tag =33;
// mainLabel.font = [UIFont systemFontOfSize:14.0];
[mainLabel setFont:[UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]]];
mainLabel.textAlignment = UITextAlignmentLeft;
mainLabel.textColor = [UIColor blackColor];
mainLabel.highlightedTextColor = [UIColor greenColor];
//mainLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:mainLabel];
mainLabel.backgroundColor=[UIColor clearColor];
}
// NSDictionary *itemAtIndex = (NSDictionary *)[jsonArray objectAtIndex:indexPath.row];
//[cell setData:itemAtIndex];
mainLabel.text = [[jsonArray objectAtIndex:indexPath.row] objectForKey:@"开发者_运维百科number"];
NSLog(@" dicst 5@",stream);
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic -- create and push a new view controller
NSLog(@" push");
//**[[jsonArray objectAtIndex:indexPath.row] objectForKey:@"number"];** how to do this so i can access from section view
aDetail = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:aDetail animated:YES];
[aDetail release];
}
Define a property on your DetailViewController
to hold the data, and pass it to that before pushing the view controller onto the navigation stack. Equivalently, add an extra parameter to the view controller's constructor. So given:
id detail = [[jsonArray objectAtIndex:indexPath.row] objectForKey:@"number"];
either do:
aDetail = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];
aDetail.dataObject = detail;
or:
aDetail = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle] dataObject: detail];
Then you can access your data from within the detail view controller. To add this property, you can do this:
@interface DetailViewController : UIViewController
{
//...
id dataObject;
}
//...
@property (nonatomic, retain) id dataObject;
@end
@implementation DetailViewController
@synthesize dataObject;
//...
@end
This means that your DetailViewController
now has API for being told about the object it's representing.
to Access indivudal item you can do something like this for those who might want this
jsonItem=dataObject;// where jsonItem is a NSDict
NSLog(@" v %@",jsonItem);// all data
jsonLabel.text = [jsonItem objectForKey:@"type"];// this is how you can display them
thanks
精彩评论