将选定的行传递给详细视图

时间:2014-11-12 21:38:33

标签: ios objective-c uitableview

晚上,

我正在配置详细信息视图。数据保存在plist文件中,每个项目都有一个单词和定义。 tableview中的单元格标有单词。我正在寻找每个单词,以显示单词和定义的详细视图。 目前代码显示错误的单词。我一直在尝试在tableview.m中设置单词和定义,然后使用它们在详细视图中设置标签。 所以我的查询是为什么显示错误的单词以及如何获取正确的定义以匹配单词。

我的代码如下。 谢谢。

#import "RCViewController.h"
#import "detailViewController.h"

@interface RCViewController ()

@end

@implementation RCViewController

static NSString *CellIdentifier = @"Cell Identifier";

@synthesize words;
@synthesize alphabetizedWords;
@synthesize wordDictionary;


-(NSDictionary *)alphabetizedWords:(NSArray *)wordsArray {
    NSMutableDictionary *buffer = [[NSMutableDictionary alloc]init];

    for (int i=0; i <words.count; i++) {
        NSString *word = [words objectAtIndex:i];
        NSString *firstLetter = [[word substringToIndex:1]uppercaseString];

        if ([buffer objectForKey:firstLetter]) {
            [(NSMutableArray *)[buffer objectForKey:firstLetter]addObject:word];
        }
        else {
            NSMutableArray *mutableArray = [[NSMutableArray alloc]initWithObjects:word, nil];
            [buffer setObject:mutableArray forKey:firstLetter];
        }
    }
    NSArray *keys = [buffer allKeys];
    for (int j; j<keys.count; j++) {
        NSString *key = [keys objectAtIndex:j];
        [(NSMutableArray *)[buffer objectForKey:key]sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    }
    NSDictionary *result = [NSDictionary dictionaryWithDictionary:buffer];
    return result;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSArray *keys = [self.alphabetizedWords allKeys];
    return [keys count];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *unsortedKeys = [self.alphabetizedWords allKeys];
    NSArray *sortedKeys = [unsortedKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    NSString *key = [sortedKeys objectAtIndex:section];
    NSArray *wordsForSection = [self.alphabetizedWords objectForKey:key];
    return  [wordsForSection count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


    NSArray *unsortedKeys = [self.alphabetizedWords allKeys];
    NSArray *sortedKeys = [unsortedKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    NSString *key = [sortedKeys objectAtIndex:[indexPath section]];
    NSArray *wordsForSection = [self.alphabetizedWords objectForKey:key];
    NSString *word = [wordsForSection objectAtIndex:[indexPath row]];

    [cell.textLabel setText:word];

    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSArray *keys = [[self.alphabetizedWords allKeys]sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    NSString *key = [keys objectAtIndex:section];
    return key;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // perform the segue by getting the cell selected and passing it to the prepareForSegue method
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [self performSegueWithIdentifier:@"showDetail" sender:cell];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];


        detailViewController *destViewController = segue.destinationViewController;
        destViewController.word = [words objectAtIndex:indexPath.row];

    }
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSString *path = [[NSBundle mainBundle]pathForResource:@"words" ofType:@"plist"];
    NSArray *wordsDictionary = [NSArray arrayWithContentsOfFile:path];

    self.words = [wordsDictionary valueForKey:@"Word"];

    self.alphabetizedWords = [self alphabetizedWords:self.words];

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


#import <UIKit/UIKit.h>

@interface detailViewController : UIViewController


@property (weak, nonatomic) IBOutlet UILabel *wordLabel;
@property (nonatomic, strong) NSString *word;
@property (weak, nonatomic) IBOutlet UILabel *definitionLabel;
@property (strong, nonatomic)NSString *definition;

@end


#import "detailViewController.h"

@interface detailViewController ()

@end

@implementation detailViewController

@synthesize word;
@synthesize wordLabel;
@synthesize definition;
@synthesize definitionLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    wordLabel.text = word;
    definitionLabel.text = definition;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

2 个答案:

答案 0 :(得分:0)

您需要在prepareForSegue中使用相同的逻辑来获取您在cellForRowAtIndexPath中使用的单词,

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        detailViewController *destViewController = segue.destinationViewController;

        NSArray *unsortedKeys = [self.alphabetizedWords allKeys];
        NSArray *sortedKeys = [unsortedKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
        NSString *key = [sortedKeys objectAtIndex:[indexPath section]];
        NSArray *wordsForSection = [self.alphabetizedWords objectForKey:key];
        NSString *word = [wordsForSection objectAtIndex:[indexPath row]];
        destViewController.word = word;

    }
}

答案 1 :(得分:0)

拥有一个名为alphabetizedWords的字典是毫无意义的,因为字典本质上是无序的。您应该在数组中设置数据结构。也许你想要一个字典数组,其中每个字典包含一个字中的一个字和另一个字中的定义。您可以对数组进行排序并用于提供表格视图数据源,还可以响应点击并将信息传递给详细视图控制器。