如何通过单击不同的单元格将数据从一个 uitableview 传递到多个控制器
how to pass data from one uitableview to multiple controllers by clicking on different cells
我有一个具有 UITableView 的视图控制器,之后我有两个不同的视图控制器,第一个是 WebLabelViewController,第二个是 LabelViewController。我做了一些编码,但仍然有问题。我想在每个单元格上分配不同的视图控制器意味着当我点击第一行单元格时它会带我 webLabelViewController 当我点击 2 行单元格时它应该带我 LabelViewController 并且一切正常但问题在于 segue.. 当我单击第一个单元格我 viewController 将数据发送到 webViewController 完全意味着索引 [0] 但是当我单击第二个单元格时它发送数据但是来自第一个单元格的数据意味着相同的索引 [0] 并且它应该是索引 [1]。 .所以我很困惑......
这是我写的一些代码...请帮助我,我是编码新手。
接口文件意思是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"]];
}
}
我对此的思路;将 TableViewCell 子类化并向其添加 viewController 属性。
如果您想学习如何对单元格进行子类化,这是一个很好的参考。
http://www.appcoda.com/customize-table-view-cells-for-uitableview/
您可以在自定义单元格中构建一个方法来实例化 viewController。假设您要在该单元格上附加一个按钮;单击单元格上的按钮可以启动 viewController。
如何显示 viewController 的细节取决于您是使用 storyboards/nibs 还是以编程方式创建它。
我有一个具有 UITableView 的视图控制器,之后我有两个不同的视图控制器,第一个是 WebLabelViewController,第二个是 LabelViewController。我做了一些编码,但仍然有问题。我想在每个单元格上分配不同的视图控制器意味着当我点击第一行单元格时它会带我 webLabelViewController 当我点击 2 行单元格时它应该带我 LabelViewController 并且一切正常但问题在于 segue.. 当我单击第一个单元格我 viewController 将数据发送到 webViewController 完全意味着索引 [0] 但是当我单击第二个单元格时它发送数据但是来自第一个单元格的数据意味着相同的索引 [0] 并且它应该是索引 [1]。 .所以我很困惑...... 这是我写的一些代码...请帮助我,我是编码新手。
接口文件意思是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"]];
}
}
我对此的思路;将 TableViewCell 子类化并向其添加 viewController 属性。
如果您想学习如何对单元格进行子类化,这是一个很好的参考。
http://www.appcoda.com/customize-table-view-cells-for-uitableview/
您可以在自定义单元格中构建一个方法来实例化 viewController。假设您要在该单元格上附加一个按钮;单击单元格上的按钮可以启动 viewController。
如何显示 viewController 的细节取决于您是使用 storyboards/nibs 还是以编程方式创建它。