我有一个基于视图的应用程序,其中包含以下文件
主视图控制器有一个按钮
显示视图控制器,以表格的形式显示一组国家/地区
状态视图控制器将再次以表格的形式显示第二步中选择的国家/地区的一组状态
这是按钮 - >国家(tableview)和选择单元格后 - >状态(的tableview)
嗯,一切正常,但我点击特定国家时无法导航到各个国家/地区。例如。如果我选择AUS,那么我就不会得到AUS的状态。
代码如下。
.h文件
{
IBOutlet UIButton *btn;
}
-(IBAction)click;
.m文件
-(IBAction)click
{
display *dis=[[display alloc]initWithNibName:@"display"bundle:nil];
[self presentModalViewController:dis animated:YES];
}
.h文件
@interface display : UITableViewController
{
NSArray *countries;
}
@property(nonatomic,retain)NSArray *countries;
@end
display.m文件
- (void)viewDidLoad {
NSArray *count=[[NSArray alloc] initWithObjects:@"India",@"USA",@"UK",@"AUS",@"SA",@"PAK",@"BANGLADESH",nil];
self.countries=count;
[count release];
self.countries=count;
self.title=@"Select a Country";
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [countries count];
}
// Customize the appearance of table view cells.
- (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] autorelease];
}
// Configure the cell...
//countries=
cell.textLabel.text=[countries objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
//UITableViewCell *thiscell=[tableView cellForRowAtIndexPath:indexPath];
if(indexPath.row==0)
{
states *detailViewController = [[states alloc] initWithNibName:@"states" bundle:nil];
// ...
// Pass the selected object to the new view controller.
//detailViewController.indexPath=self.indexPath;
//self.dis.indexValue=indexPath;
[self.navigationController pushViewController:detailViewController animated:YES];
//detailViewController.indexPath=self.indexPath;
[self presentModalViewController:detailViewController animated:YES];
[detailViewController release];
}
if(indexPath.row==1)
{
states *detailViewController = [[states alloc] initWithNibName:@"states" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[self presentModalViewController:detailViewController animated:YES];
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
[detailViewController release];
}
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
states.h文件
@interface states : UITableViewController {
NSArray *state;
NSArray *state1;
NSArray *state2;
//NSIndexPath *indexPath;
}
@property(nonatomic,retain) NSArray *state;
@property(nonatomic,retain) NSArray *state1;
@property(nonatomic,retain) NSArray *state2;
//@property(nonatomic,retain)display *dis;
//@property(nonatomic,retain)NSIndexPath *indexPath;
@end
states.m文件
- (void)viewDidLoad {
NSArray *sta=[[NSArray alloc]initWithObjects:@"goa",@"ap",@"karnataka",nil];
self.state=sta;
[sta release];
self.state=sta;
[super viewDidLoad];
NSArray *sta1=[[NSArray alloc]initWithObjects:@"Los Angeles",@"California",@"Las Vegas",nil];
self.state1=sta1;
[sta release];
self.state1=sta1;
[super viewDidLoad];
NSArray *sta2=[[NSArray alloc]initWithObjects:@"Southhampton",@"Nottingham",@"London",nil];
self.state2=sta2;
[sta release];
self.state2=sta2;
[super viewDidLoad];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [self.state count];
}
// Customize the appearance of table view cells.
- (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] autorelease];
}
// Configure the cell...
if(indexPath.row==0)
{
cell.textLabel.text=[state objectAtIndex:indexPath.row];
}
else if(indexPath.row==1)
{
cell.textLabel.text=[state1 objectAtIndex:indexPath.row];
}
return cell;
}
因此,当我运行此代码时,它运行正常,但每当我选择任何国家/地区时,我都会获得南安普顿和加利福尼亚州。
我不想为所有状态创建多个视图控制器,这样做是愚蠢的。所以我创建了一个数组,并使用状态视图控制器显示所选国家/地区的不同状态数组。
我应该如何将indexpath
国家/地区的值传递给州视图控制器,以便我可以访问各个国家/地区的所有相应州? (注意:我没有为状态创建所有数组,只是创建了两个才能检查。)
答案 0 :(得分:0)
嘿,只需在countriesView的didSelectRowAtIndexPath委托方法中使用stateView的setTableArray ...
像Bellow这样的setTableArray方法:
-(void)setTableArray:(NSMutableArray *)arr
{
arrData = arr;
[arrData retain];
//[tblView reloadData];
}
这里arrData是MutableArray ....
当你在那时推送stateView fromdidSelectRowAtIndexPath时,只需设置一个类似于下面的...
[detailViewController setTableArray:yourArray];
在这里,您只需获取所选国家/地区索引的数据并传递您的阵列的数据... 希望,这有助于你...... :)
答案 1 :(得分:0)
为什么要比较显示视图控制器的didSelectRowAtIndexPath中的indexPath?你可以放置这样的代码。如果你只想传递indexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//UITableViewCell *thiscell=[tableView cellForRowAtIndexPath:indexPath];
states *detailViewController = [[states alloc] initWithNibName:@"states" bundle:nil];
//Pass the selected object to the new view controller.
detailViewController.m_SelectedIndexPath=indexPath;
[self.navigationController pushViewController:detailViewController animated:YES]; [self presentModalViewController:detailViewController animated:YES];
[detailViewController release];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
和在states.m,cellForRowAtIndexPath中你可以像这样检查m_SelectedIndexPath:
- (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] autorelease];}
// Configure the cell...
if(m_SelectedIndexPath == 0)
{
cell.textLabel.text=[state objectAtIndex:indexPath.row];
}
else if(m_SelectedIndexPath==1)
{
cell.textLabel.text=[state1 objectAtIndex:indexPath.row];
}
return cell;
}
你要推动的第二件事以及提出tableview。你应该推或出。