사용자가 정의한 데이터로 가득 찬 테이블보기가 있습니다. 배열은 nsuserdefaults에서 수집되고 테이블 뷰 컨트롤러에 표시됩니다. 나는 삭제 기능을 구현하려고합니다. 그것을 잘 보이지만 삭제 누를 때 오류가테이블 뷰에서 요소를 삭제하십시오. 대상 코드 :
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object'
내가 그런
#import <UIKit/UIKit.h>
@interface FavoritesViewController: UITableViewController <UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *favoriteItems;
@end
나는 기분이 내가
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
favoriteItems= [[NSUserDefaults standardUserDefaults] objectForKey:@"favoritesArray"];
[self.tableView reloadData];
}
를 초기화 조각 Favourites.h
에 대해 가지고있는 코드를 제공 위의 코드는 문제가있는 곳입니다. NSUserDefaults; 기대하는 NSArray입니다. 그래서 나는 그것도 너무 변경할 수 있습니까? 그냥 이익을 위해 삭제 방법- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//remove the deleted object from your data source.
//If your data source is an NSMutableArray, do this
[self.favoriteItems removeObjectAtIndex:indexPath.row];
[[NSUserDefaults standardUserDefaults] setObject:favoriteItems forKey:@"favoritesArray"];
[self.tableView reloadData]; // tell table to refresh now
}
}
까지 다음 세트는 정말 사전에 도움을 주셔서 감사합니다.