我对ios应用程序开发相当新,我在尝试从单元格中删除行时遇到了这个问题:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString removeObjectAtIndex:]: unrecognized selector sent to instance 0x7fef12743830'
这是我的代码:
头文件:
#import <UIKit/UIKit.h>
#import "ViewController.h"
@interface TableViewController : UITableViewController <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSArray *titles;
@property (nonatomic,strong) NSDictionary *animeNames;
@end
在viewDidLoad
方法中的我有这个代码设置self.titles
的值,我从plist
文件获取行数据
NSURL *url = [[NSBundle mainBundle] URLForResource:@"animes" withExtension:@"plist"];
self.animeNames = [NSDictionary dictionaryWithContentsOfURL:url];
self.titles = self.animeNames.allKeys;
实施档案
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView beginUpdates];
NSMutableArray *current = [self.titles objectAtIndex:indexPath.row];
[current removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView reloadData];
[tableView endUpdates];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
答案 0 :(得分:0)
您的titles
数组似乎包含NSString
个对象。所以你不能这样称呼它:
NSMutableArray *current = [self.titles objectAtIndex:indexPath.row];
[current removeObjectAtIndex:indexPath.row];
你也有一个NSArray
,它是不可变的,所以你应该使用NSMutableArray
代替(当你将它声明为NSMutableArray时,你还应该确保赋值数组也是一个NSMutableArray)。或者代替这些,您可以使用以下方法修复它:
NSMutableArray *current = [self.titles mutableCopy];
[current removeObjectAtIndex:indexPath.row];
self.titles = [current copy];