TableView附件中的NSInternalInconsistencyException错误?

时间:2013-09-13 00:05:22

标签: objective-c xcode4.5 uitableview

我正在尝试向此tableview添加一个附件(选中/取消选中),由于某种原因,xCode告诉我这行有问题: [item setObject:cell forKey:@“StrainTableCell” ];

控制台抛出了这个错误:

* 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:' - [__ NSCFDictionary setObject:forKey:]:发送到不可变对象的变异方法'*

有谁知道为什么?请参阅下面的代码段。

ViewController.h

#import <UIKit/UIKit.h>
#import "PickerViewController.h"


@interface PickerResultsTableViewController : UITableViewController  {

    NSIndexPath *currentDetailPath;
    bool loaded;


    NSArray *Strains;
    NSArray *searchResults;
    NSMutableArray *dataArray;
    NSMutableData *data;

}

- (IBAction)backbuttonpressed: (UIBarButtonItem *)sender;

@property (strong, nonatomic) NSArray *favoritesArrayset;
@property (strong, nonatomic) IBOutlet UITableView *PickerTableView;
@property (nonatomic, retain) NSArray *searchResults;
@property (nonatomic, strong) NSMutableSet * favoritesArray;

@end

ViewController.m

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

{

    static NSString *strainTableIdentifier = @"StrainTableCell";

    StrainTableCell *cell = (StrainTableCell *)[tableView dequeueReusableCellWithIdentifier:strainTableIdentifier];
    if (cell == nil)


        cell = [[StrainTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strainTableIdentifier];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;


    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StrainTableCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];



        if (tableView == PickerTableView) {
            NSLog(@"Using the search results");

            cell.titleLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Title"];
            cell.descriptionLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Description"];
            cell.ratingLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Rating"];
            cell.ailmentLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Ailment"];
            cell.actionLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Action"];
            cell.ingestLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Ingestion"];

            NSLog(@"%@", searchResults);



        } else {
            NSLog(@"Using the FULL LIST!!");
            cell.titleLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Title"];
            cell.descriptionLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Description"];
            cell.ratingLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Rating"];
            cell.ailmentLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Ailment"];
            cell.actionLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Action"];
            cell.ingestLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Ingestion"];

        }


    NSMutableDictionary *item = [Strains objectAtIndex:indexPath.row];
    cell.textLabel.text = [item objectForKey:@"text"];

    [item setObject:cell forKey:@"StrainTableCell"];
    BOOL checked = [[item objectForKey:@"checked"] boolValue];

    NSLog(@"%i",checked);
    UIImage *image = (checked) ? [UIImage   imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    button.frame = frame;
    [button setBackgroundImage:image forState:UIControlStateNormal];
    [button addTarget:self action:@selector(checkButtonTapped:event:)  forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor clearColor];
    cell.accessoryView = button;

    return cell;

}

1 个答案:

答案 0 :(得分:0)

好吧,编译器会给你一个很好的暗示:mutating method sent to immutable object。 即使您将项目声明为NSMutableDictionary,但这并不意味着您从[Strains objectAtIndex:indexPath.row]获取的对象实际上是一个可变字典。

即使你做了这样的事情:

NSDictionary *dict1 = [[NSDictionary alloc] init];

//the following line does not convert dict1 into a mutable dictionary
NSMutableDictionary *dict2 = (NSMutableDictionary*)dict1;

要解决此问题,请确保添加到Strains的对象是可变字典。您可以通过直接实例化一个或通过调用mutableCopy方法来完成此操作。

希望这有帮助!

相关问题