如何在两个 ViewControllers 之间传递数据,其中第一个 VC 有两个 tableView,第二个 tableView 选择的数据应该传递给第二个 VC? [英] How to pass data between two ViewControllers where first VC having two tableViews, second tableView selected data should pass to second VC?
问题描述
我在 ViewController 中有两个 tableView.我想将第二个 tableView 数据传递给第二个 ViewController.
I have two tableViews in a ViewController. I want to pass the second tableView data to second ViewController.
现在每当点击类别时,相关数据将显示在子类别表中.现在,如果我单击身份平面和实体图"等子类别数据,则该单元格标签文本应传递给下一个 viewController.我可以传递数据,但是每当单击第二个表格单元格时,它都不会传递到下一个 ViewController.问题是我无法通过传递数据进入下一个视图.我在DidSelectRowAtIndexPath"方法中遇到问题.请帮我解决这个问题并将代码发给我.
Now whenever clicking on Category, that related data will display in SubCategory table. Now if i click on SubCategory data like "Identity planar and solid figures", that cell label text should be pass to next viewController. i can pass data but whenever clicking on Second table cell, that is not going to next ViewController. The problem is i am not able to go to next View with passing data. I am getting problem at "DidSelectRowAtIndexPath" method. Please help me on this problem and send me the code.
这是我的代码
-(void)viewDidLoad
{
NSMutableArray *Mute_Category=[[NSMutableArray alloc]initWithObjects:@"Geometry", @"Mixed operations", nil];
NSMutableArray *Mute_Sub_Category=[[NSMutableArray alloc]initWithObjects:@"Identify planar and solid figures", @"Open and closed shapes and qualities of polygons", @"Nets of 3-dimensional figures", @"Types of angles", nil];
tag=1;
[table_category reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
if (tag==1)
{
return [Mute_category count];
}
else
{
return [Mute_Sub_Category count];
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
cell=[[UITableViewCell alloc]init];
if (tag==1)
{
cell.textLabel.text=[Mute_category objectAtIndex:indexPath.row];
}
else
{
cell.textLabel.text=[Mute_Sub_Category objectAtIndex:indexPath.row];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *localStr=[Mute_category objectAtIndex:indexPath.row];
tag=2;
[table_sub_category reloadData];
}
推荐答案
您可以使用属性来访问其他类的数据:
You can use a property to access the data from other classes:
@interface YourSecondView : UIViewController {
}
@property (nonatomic, retain) NSString* dataString;
不要忘记在 YourSecondView.m
上 @synthesize
在表视图控制器中,您可以像这样传递数据
In the table View Controller you can pass data like that
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
YourSecondView * SV = [[YourSecondView alloc]init];
sv.dataString = [Mute_category objectAtIndex:indexPath.row];
// here write what you want to do next...
}
这篇关于如何在两个 ViewControllers 之间传递数据,其中第一个 VC 有两个 tableView,第二个 tableView 选择的数据应该传递给第二个 VC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!