2015-07-09 14 views
0

我有一個視圖控制器有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 
+0

您是否嘗試將整個變量中的選定行索引保存?將它保存在didSelectRowAtIndexPath –

回答

1

你可以簡單地通過NSIndexPathsender當你調用performSegueWithIdentifiersender可以是任何你喜歡的物體:

- (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"]]; 
    } 
} 
+0

噢,非常感謝你,你呢? – King

0

我對此的思路;子類TableViewCell並添加一個viewController屬性。

這裏有一個很好的參考,如果你想了解如何繼承細胞。

http://www.appcoda.com/customize-table-view-cells-for-uitableview/

你可以建立出,將實例化的viewController定製電池的方法。假設你將一個按鈕添加到這個單元格;單擊單元格上的按鈕可以啓動viewController。

關於如何顯示viewController的細節取決於你是否使用storyboard/nibs或以編程方式創建它。

相關問題