我有一个具有 UITableView 的 View Controller ,之后我有两个不同的 View Controller ,第一个名称是 WebLabelViewController,第二个是 LabelViewController。我做了一些编码,但仍然有问题。我想在每个单元格上分配不同的 View Controller 意味着当我点击第一行单元格时它会带我 webLabelViewController 当我点击 2 行单元格时它应该带我 LabelViewController 并且一切正常但问题在于 segue.. 当我单击第一个单元格我的 viewController 将数据发送到 webViewController 完全意味着索引 [0] 但是当我单击第二个单元格时它发送数据但是来自第一个单元格的数据意味着相同的索引 [0] 并且它应该是索引 [1].. 所以我很困惑... 这是我写的一些代码...请帮助我,我是编码新手。
接口(interface)文件就是viewController.h
import < UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
}
@property NSArray *firstArray;
@property(nonatomic, retain) UITableView *tableView;
@end
带有代码 ViewController.m 的实现文件
#import "ViewController.h"
#import "WebLabelViewController.h"
#import "LabelViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = _firstArray[0][@"cellname"];
_firstArray = @[
@{@"cellname":@"Stud", @"detail":@"STUDYc", @"about":@"M ", @"link":@"http://"},
@{@"cellname":@"Onlin", @"detail":@"UK", @"about":@"MM ", @"link":@"http:/"},
@{@"cellname":@"Becom", @"detail":@"J ", @"about":@"QM ", @"link":@"http://www.},
@{@"cellname":@"Contact Us", @"detail":@"TELEP", @"about":@"q ", @"link":@"http"},
@{@" cellname ":@"About Th", @"detail":@"GIFT", @"about":@"M", @"link":@"http"},
];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (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 [_firstArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = _firstArray[indexPath.row][@"cellname"];
cell.detailTextLabel.text = _firstArray[indexPath.row][@"detail"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
[self performSegueWithIdentifier:@"segue 1" sender:self];
} else if (indexPath.row >= 1 && indexPath.row <= 3) {
[self performSegueWithIdentifier:@"segue 2" sender:self];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"segue 1"]){
// NSIndexPath *path = [self.tableView indexPathForSelectedRow];
WebLabelViewController *wlvc = [segue destinationViewController];
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
wlvc.xyz = [NSString stringWithFormat:@"%@", _firstArray [path.row][@"about"]];
wlvc.abc = [NSString stringWithFormat:@"%@", _firstArray [path.row][@"link"] ];
} else if ([segue.identifier isEqualToString:@"segue 2"]) {
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
LabelViewController *lbvc = [segue destinationViewController];
lbvc.def = [NSString stringWithFormat:@"%@", _firstArray[path.row][@"about"]];
}
}
@end
最佳答案
当您调用performSegueWithIdentifier
时,您只需将NSIndexPath
作为sender
传递即可。 sender
可以是您喜欢的任何对象:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
[self performSegueWithIdentifier:@"segue 1" sender:indexPath];
} else if (indexPath.row >= 1 && indexPath.row <= 3) {
[self performSegueWithIdentifier:@"segue 2" sender:indexPath];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"segue 1"]){
NSIndexPath *path = (NSIndexPath *)sender;
WebLabelViewController *wlvc = (WebLabelViewController *)segue.destinationViewController;
wlvc.xyz = [NSString stringWithFormat:@"%@", self.firstArray[path.row][@"about"]];
wlvc.abc = [NSString stringWithFormat:@"%@", self.firstArray[path.row][@"link"]];
} else if ([segue.identifier isEqualToString:@"segue 2"]) {
NSIndexPath *path = (NSIndexPath *)sender;
LabelViewController *lbvc = (LabelViewController *)segue.destinationViewController;
lbvc.def = [NSString stringWithFormat:@"%@", self.firstArray[path.row][@"about"]];
}
}
关于ios - 如何通过单击不同的单元格将数据从一个 uitableview 传递到多个 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31328798/