在didSelectRowAtIndexPath中生成EXC_BAD_ACCESS

时间:2011-12-05 12:26:50

标签: objective-c json uitableview

请考虑以下代码:

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

    // Configure the cell.
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }


    NSDictionary *dict1 = [rows objectAtIndex:indexPath.row];
    NSLog(@"%@", dict1);
    if ([dict1 objectForKey:@"faqQues"] != [NSNull null]) {
        cell.textLabel.text = [dict1 objectForKey:@"faqQues"];

    } 



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

    faqQuesID = [[rows objectAtIndex: indexPath.row] integerValue];    
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    //NSString *faqQuesID = [rows objectAtIndex:indexPath.row];    
    NSLog(@"faqQuesID ######### %@",faqQuesID);

    [prefs setInteger:faqQuesID forKey:@"faqQuesID"];
    [prefs setInteger:faqTypeID forKey:@"passFaqType"];

    helpDetailsViewController *hdVController = [[helpDetailsViewController alloc] initWithNibName:@"helpDetailsViewController" bundle:nil];     
    [self presentModalViewController:hdVController animated:YES];
    [hdVController release];
}


        cell.textLabel.textAlignment = UITextAlignmentLeft;
        cell.textLabel.font = [UIFont fontWithName:@"Arial" size:13.0];
        cell.textLabel.textColor = [UIColor blackColor];    
        cell.textLabel.highlightedTextColor = [UIColor blueColor];
        cell.textLabel.textAlignment = UITextAlignmentCenter;
        return cell;   

    }

// [prefs setInteger:10 forKey:@“faqQuesID”];如果我手动整数然后它确实工作,但当我收到值indexPath.row然后它确实显示错误 //控制台错误是

2011-12-05 18:16:30.312 test[3602:c203] -[__NSCFDictionary integerValue]: unrecognized selector sent to instance 0x719e400
2011-12-05 18:16:30.314 test[3602:c203] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary integerValue]: unrecognized selector sent to instance 0x719e400'

//我浪费了几个小时请告诉我如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

在你的cellForRowAtIndexPath中:

NSDictionary *dict1 = [rows objectAtIndex:indexPath.row];

rows是一系列词典。行。

在你的didSelectRow中:

faqQuesID = [[rows objectAtIndex: indexPath.row] integerValue];

我们可以分解为:

NSDictionary *dict = [rows objectAtIndex:indexPath.row];
faQuesID = [dict integerValue];

NSDictionary没有integerValue方法 - 这正是错误消息告诉您的方法。大概你想从字典中的特定对象中获取整数。

faqQuesID = [[[rows objectAtIndex: indexPath.row] objectForKey:@"faqQuesID"]integerValue];

假设您在密钥NSNumber下存储了@"faqQuesID"

因此,您的didSelectRow方法应该类似于:

NSDictionary *faq = [rows objectAtIndex: indexPath.row];
[prefs setInteger:[[faq objectForKey:@"faqQuesID"] integerValue] forKey:@"faqQuesID"];