我有一個視圖控制器有UITableView,之後,我有兩個不同的視圖控制器的名字是WebLabelViewController,第二個是LabelViewController。我做了一些編碼,但仍然有些問題。我想分配不同的視圖控制器在每個單元格意味着當我點擊第一行單元格時,它將採取我webLabelViewController當我單擊2行單元格時,它應該帶我LabelViewController和每件事情正常工作,但問題是繼續..當我點擊第一個單元格我的viewController完全發送數據到webViewController意味着索引[0],但當即時通訊單擊第二個單元其發送數據,但數據來自第一個單元意味着相同的索引[0],它應該是索引[1] ..所以我很困惑... 這裏是我寫的一些代碼...請幫助我即時通訊新編碼。如何通過點擊不同的單元格將數據從一個uitableview傳遞到多個控制器
接口文件意味着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
您是否嘗試將整個變量中的選定行索引保存?將它保存在didSelectRowAtIndexPath –