HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 要Segue还是doSelectRowAtIndexPath?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
以下是我正在运行的代码.我有一个带有导航控制器,桌面控制器和视图控制器的故事板设置.我正在尝试从NSDictionary中传递名称,我将该表设置为详细视图控制器.@R_665_10675@用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;
}

但是现在当我选择行时,名称将不会出现在第一次按下的详细控制器中.如果您返回并选择其他名称,则您按下的名字将显示在视图控制器中.任何关于为什么会发生的建议?

解决方法

你需要使用这两个,在didSELEctRowATindexPath你应该调用[self performSegueWithIdentifier:@“identifier”sender:self];

在同一个View Controller中,您应该使用prepareForSegue方法从destue中@L_450_16@desTinationViewController,然后在该视图控制器上设置任何要设置的属性.

- (void)tableView:(UITableView *)tableView didSELEctRowATindexPath:(NSIndexPath *)indexPath
{
    self@L_47_18@meProperty = [self@L_47_18@meArray objectATindex:indexPath.row];
    [self performSegueWithIdentifier:@"seguEID" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UIViewController *vcToPushTo = segue.desTinationViewController;
    vcToPushTo.propertyToSet = self@L_47_18@meProperty;
}

大佬总结

以上是大佬教程为你收集整理的ios – 要Segue还是doSelectRowAtIndexPath?全部内容,希望文章能够帮你解决ios – 要Segue还是doSelectRowAtIndexPath?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。