要select还是要select行列索引?

下面是我正在运行的代码。 我有一个导航控制器和桌面视图控制器和视图控制器的故事板设置。 我试图从NSDictionary,我已经为表设置的详细信息视图控制器的名称。 我应该使用prepareforsegue还是didselectrowatindexpath,如何从字典中获得名称来传递它?

#import "FMInboxViewController.h" #import "FMDetailViewController.h" @interface FMInboxViewController () @end @implementation FMInboxViewController @synthesize keyArray; @synthesize tableArray; @synthesize tblDictionary; @synthesize filteredArray; - (void)viewDidLoad { [super viewDidLoad]; NSMutableArray *ary=[[NSMutableArray alloc]init]; [ary addObject:@"Adam"]; [ary addObject:@"Fred"]; [ary addObject:@"Angel"]; // ... many similar entries [ary addObject:@"James"]; [ary addObject:@"Mukthesh"]; self.tblDictionary =[self fillingDictionary:ary]; } 

表视图数据源

 #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return [keyArray count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. NSArray *ary = [self.tblDictionary valueForKey:[keyArray objectAtIndex:section]]; return [ary count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; NSString *key = [keyArray objectAtIndex:[indexPath section]]; NSArray *array = (NSArray *)[self.tblDictionary valueForKey:key]; NSString *cellTitle = [array objectAtIndex:[indexPath row]]; cell.textLabel.text = cellTitle; // Configure the cell... return cell; } -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { NSString *title = [keyArray objectAtIndex:section]; return title; } //-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // NSString *key = [keyArray objectAtIndex:[indexPath section]]; // NSArray *array = (NSArray *)[self.tblDictionary valueForKey:key]; // self.selectedName = [array objectAtIndex:indexPath.row]; // NSLog(@"Selected Name in Did select: %@", self.selectedName); // // [self performSegueWithIdentifier:@"showDetail" sender:self]; //} -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"showDetail"]) { NSIndexPath *section = [self.tableView indexPathForSelectedRow]; NSString *key = [keyArray objectAtIndex:section]; NSArray *array = (NSArray *)[self.tblDictionary valueForKey:key]; NSString *cellTitle = [array objectAtIndex:[indexPath row]]; NSLog(@"Selected Name in Did select: %@", self.selectedName); } } 

帮手方法

 #pragma mark - Helper Methods - (NSMutableDictionary *)fillingDictionary:(NSMutableArray *)sentArray { keyArray = [[NSMutableArray alloc] init]; [keyArray removeAllObjects]; NSMutableDictionary *dic = [[NSMutableDictionary alloc] init]; [sentArray sortUsingSelector:@selector(compare:)]; for ( NSString *str in sentArray) { NSString *charVal = [str substringToIndex:1]; NSString *charStr = [NSString stringWithString:charVal]; NSLog(@" charStr = %@", charStr); if (![keyArray containsObject:charStr]) { NSMutableArray *charArray = [[NSMutableArray alloc] init]; [charArray addObject:str]; [keyArray addObject:charStr]; [dic setValue:charArray forKey:charStr]; } else { NSMutableArray *prevArray = (NSMutableArray *)[dic valueForKey:charStr]; [prevArray addObject:str]; [dic setValue:prevArray forKeyPath:charStr]; } } return dic; } @end 

好的,我改变了这个部分看起来像这样

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSString *key = [keyArray objectAtIndex:[indexPath section]]; NSArray *array = (NSArray *)[self.tblDictionary valueForKey:key]; self.selectedName = [array objectAtIndex:indexPath.row]; NSLog(@"Selected Name in Did select: %@", self.selectedName); } -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { FMDetailViewController *dvc = (FMDetailViewController *)segue.destinationViewController; dvc.name = self.selectedName; } 

但是现在,当我select行时,名称将不会显示在第一次按下的详细控制器中。 如果您返回并select其他名称,则按下的名字将显示在视图控制器中。 有关为什么发生这种情况的任

你需要使用两个,在didSelectRowAtIndexPath你应该调用[self performSegueWithIdentifier:@"identifier" sender:self];

在同一个视图控制器中,您应该有prepareForSegue方法从segue中抓取destinationViewController ,然后在该视图控制器上设置您希望设置的任何属性。

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { self.someProperty = [self.someArray objectAtIndex:indexPath.row]; [self performSegueWithIdentifier:@"segueID" sender:self]; } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { UIViewController *vcToPushTo = segue.destinationViewController; vcToPushTo.propertyToSet = self.someProperty; } 

如果可能的话(在几乎所有的标准情况下都是这种情况),我会使用Interface Builder来触发segue。 您仍然需要实现prepareForSegue:configuration目标视图控制器。

为了在IB中创build一个segue,当点击单元格或附件button(单元格的最右侧)时触发详细视图,请执行以下步骤:

  1. 控制从表格视图单元拖动到目标视图控制器,并释放鼠标或触控板。 这将打开小select面板。

  2. select触发源,“ select继续 ”或“ 配件操作 ”,以及继续(“ ”,“ 模态 ”或“ 自定义 ”)的types。

  3. 在属性检查器窗格中定义segue的“标识符”,例如“UserShowSegue”。

这里是一个来自IB的Demo storyboard的图片,它展示了如何设置“用户”视图控制器中的“表格视图单元格”以触发“推送”继续到详细视图控制器:

在这里输入图像说明

可能是一个古老的问题,但要更为详细地为谁所关心:

1-使用“(@彼得 – Foti)”提到。

Segue应该从ParentVC链接到DestinationVC(不是来自Prototype Cell)。

3-发送者应设置正确。

4-设置你的“@属性(someProperty)”。

示例代码:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { self.someProperty = [self.someArray objectAtIndex:indexPath.row]; [self performSegueWithIdentifier:@"segueID" sender:self.someProperty]; } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { UIViewController *vcToPushTo = segue.destinationViewController; vcToPushTo.propertyToSet = sender; } 

因为didSelectRowAtIndexPath可以被storyboard可视化编程所取代,所以我build议在prepareForSegue这样做所有的逻辑:

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { let cell = sender as! UITableViewCell let indexPath = tableView.indexPath(for: cell)! ... and then use indexPath to set up the destination }