问题
- I am using split view controller in my project,
- in the split view i have RootViewCotroller (left), DetaolsViewConroller (right).
- In the Root view controller, I have 3 rows.
- Now I want different type of appearance in details view, when selecting different rows in rootview.
- ...
- in Details view:
- 1.Display a tableView when select the row1.
- 2.Dispaly a text field when select the row2.
- 3.Image view when select row3 from rootviewcontroller.
- ..
- I wrote the code like this in RootViecontroller, but no change in detailsView. -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
RightViewController *rightViewController = [[RightViewController alloc] initWithNibName:@"RightViewController"bundle:nil];
//[self.navigationController pushViewController:rightViewController animated:YES];
if(indexPath.row == 0)
{
UILabel *infoLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 450, 150)];
infoLabel.text = @"Customer:Barrick \r\n Mine:gold \r\n Location:USA";
[rightViewController.view addSubview:infoLabel];
UITableView *mapTable = [[UITableView alloc]initWithFrame:CGRectMake(165, 5, 450,150)style:UITableViewStyleGrouped];
[rightViewController.view addSubview:mapTable];
}
if(indexPath.row == 1)
{
UILabel *infoLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 450, 150)];
infoLabel.text = @"Eqipement";
[rightViewController.view addSubview:infoLabel];
}
if(indexPath.row == 2)
{
image view..
}
[rightViewController release];
}
- what to do..? Thanks in advance.
回答1:
You have to pass a value to the right view controller and you have to also set delegate for your right view controller.
Define your right View Controller in your left view controller like:
RightViewController *detail;
/// in implementation of left view contrloller
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger item = [indexPath row] +1;
NSNumber *detailItem = [NSNumber numberWithInteger:item];
self.detailCar.detailItem = detailItem;
}
Define in your rightViewController.h file :
@property (retain, nonatomic) NSNumber *detailItem;
In Your RightViewController's implementation file:
- (void)setDetailItem:(NSNumber *)newDetailItem {
NSLog(@"ConfigTItem");
if (_detailItem != newDetailItem) {
[_detailItem release];
_detailItem = [newDetailItem retain];
[self configureView];
}
else {
[self configureView];
}
}
- (void)configureView {
if (self.detailItem) {
[self viewDidLoad]; //
//set your method's and variable whatever you want to do in your view did load or directly here
}
}
回答2:
Don't try to render view from another controller. On click of the row pass the index value of row to the rightView controller. Then render the view depending upon the value of row in rightView controller.
来源:https://stackoverflow.com/questions/8636040/didselectrowatindexpath-method-for-root-tableview-in-splitviewcotroller