认为不工作,细胞不选择

时间:2014-11-07 20:08:57

标签: ios objective-c uitableview

晚上,

我设法按字母顺序对表格进行分区(:D),但现在单元格没有选择。 这个想法是第一个视图提供了一个单词列表。然后选择一个单词,这将导致以词汇表为特色的详细视图。但是,单元格没有选择(变成灰色而不是蓝色?)并且从原型单元格运行到新视图的故事板中的segue没有做任何事情(单击模拟器中的单元格不会导致segue)。

有人有建议吗?

我已将下面的代码包含在内,并在需要时准备好prepareforsegue代码。

谢谢你们:)

@interface RCViewController ()

@end

@implementation RCViewController

static NSString *CellIdentifier = @"Cell Identifier";

@synthesize fruits;

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

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

    if ([buffer objectForKey:firstLetter]) {
        [(NSMutableArray *)[buffer objectForKey:firstLetter]addObject:fruit];
    }
    else {
        NSMutableArray *mutableArray = [[NSMutableArray alloc]initWithObjects:fruit, 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.alphabetizedFruits allKeys];
return [keys count];
}

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

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


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


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

[cell.textLabel setText:fruit];

return cell;
 }

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

 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"showDetail"]) {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    detailViewController *destViewController = segue.destinationViewController;
    destViewController.word = [fruits 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.fruits = [wordsDictionary valueForKey:@"Word"];

self.alphabetizedFruits = [self alphabetizedFruits:self.fruits];

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

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

@end

1 个答案:

答案 0 :(得分:0)

您需要添加didSelectRowAtIndexPath方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // confirm cell is being selected
    NSLog(@"didSelectRowAtIndexPath");

    // 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];
}