0

このテーブルビューにアクセサリ (チェックされている/チェックされていない) を追加しようとしていますが、何らかの理由で 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;

}
4

1 に答える 1

0

さて、コンパイラは何が起こっているかについてかなり良いヒントを与えています: mutating method sent to immutable object. item を として宣言して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メソッドを呼び出します。

お役に立てれば!

于 2013-09-13T00:14:22.507 に答える