更改 TableView 中的数据时出错 Objective C
Errors Changing Data in a TableView Objective C
我一直在研究 Nutting、Olsson 和 Mark iOS7 教科书中的 table 观点示例。我有一个运行良好的 table 视图,因此我在视图中添加了一个按钮。当我在按钮内部触摸时,它会调用方法 addData。 addData 只是将另一个对象附加到 table.
中显示的数组
代码编译得很好,但是当我点击按钮时,它崩溃了。为什么是这样?我不明白错误信息,但这是代码。
#import "ViewController.h"
@interface ViewController ()
@property (copy, nonatomic) NSMutableArray* dwarves;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.dwarves = @[@1.5,@2.,@2.5,@3.,@3.5,@4.,@4.5,@5.,@5.5,@6.,@6.5,@7.];
UITableView *tableView = (id)[self.view viewWithTag:1];
UIEdgeInsets contentInset = tableView.contentInset;
contentInset.top = 20;
[tableView setContentInset:contentInset];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.dwarves count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@",self.dwarves[indexPath.row]];
return cell;
}
- (IBAction)addData:(id)sender {
[_dwarves addObject:@100];
}
@end
这是错误:
2015-05-28 18:37:24.449 TableView Practice[3630:145774] -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x7924aa40
2015-05-28 18:37:24.453 TableView Practice[3630:145774] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x7924aa40'
这段代码有什么问题?
不管你怎么声明dwarves,它都是一个NSArray,而不是一个NSMutableArray。因此 addObject 崩溃。我相信 "copy" 属性 和可变类型的组合不会做你认为应该做的事情。
最好让 "dwarves" 只是一个普通的 "strong" 属性,但初始化
self.dwarves = [@[@1.5,@2.,@2.5,@3.,@3.5,@4.,@4.5,@5.,@5.5,@6.,@6.5,@7.] mutableCopy];
使用以下代码设置您的数组,然后您可以从中 add/remove 个对象。
[[NSMutableArray alloc] initWithArray:@[@1.5,@2.,@2.5,@3.,@3.5,@4.,@4.5,@5.,@5.5,@6.,@6.5,@7.]];
self.dwarves = @[@1.5,@2.,@2.5,@3.,@3.5,@4.,@4.5,@5.,@5.5,@6.,@6.5,@7.];
你正在传递一个 NSArray
并且它是不可变的:
self.dwarves = [@[@1.5,@2.,@2.5,@3.,@3.5,@4.,@4.5,@5.,@5.5,@6.,@6.5,@7.] mutableCopy];
//or
self.dwarves = [[NSMutableArray alloc] initWithArray:@[@1.5,@2.,@2.5,@3.,@3.5,@4.,@4.5,@5.,@5.5,@6.,@6.5,@7.]];
将允许您 modify/update/edit 您的 NSMutableArray
中的值。
使其可变后,您需要:
- (IBAction)addData:(id)sender
{
[_dwarves addObject:@100];
UITableView *tableView = (id)[self.view viewWithTag:1];
[tableView reloadData];
// or
[(UITableView *)[self.view viewWithTag:1] reloadData];
// to see the changes you made after adding data to your dataSource
}
我一直在研究 Nutting、Olsson 和 Mark iOS7 教科书中的 table 观点示例。我有一个运行良好的 table 视图,因此我在视图中添加了一个按钮。当我在按钮内部触摸时,它会调用方法 addData。 addData 只是将另一个对象附加到 table.
中显示的数组代码编译得很好,但是当我点击按钮时,它崩溃了。为什么是这样?我不明白错误信息,但这是代码。
#import "ViewController.h"
@interface ViewController ()
@property (copy, nonatomic) NSMutableArray* dwarves;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.dwarves = @[@1.5,@2.,@2.5,@3.,@3.5,@4.,@4.5,@5.,@5.5,@6.,@6.5,@7.];
UITableView *tableView = (id)[self.view viewWithTag:1];
UIEdgeInsets contentInset = tableView.contentInset;
contentInset.top = 20;
[tableView setContentInset:contentInset];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.dwarves count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@",self.dwarves[indexPath.row]];
return cell;
}
- (IBAction)addData:(id)sender {
[_dwarves addObject:@100];
}
@end
这是错误:
2015-05-28 18:37:24.449 TableView Practice[3630:145774] -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x7924aa40
2015-05-28 18:37:24.453 TableView Practice[3630:145774] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x7924aa40'
这段代码有什么问题?
不管你怎么声明dwarves,它都是一个NSArray,而不是一个NSMutableArray。因此 addObject 崩溃。我相信 "copy" 属性 和可变类型的组合不会做你认为应该做的事情。
最好让 "dwarves" 只是一个普通的 "strong" 属性,但初始化
self.dwarves = [@[@1.5,@2.,@2.5,@3.,@3.5,@4.,@4.5,@5.,@5.5,@6.,@6.5,@7.] mutableCopy];
使用以下代码设置您的数组,然后您可以从中 add/remove 个对象。
[[NSMutableArray alloc] initWithArray:@[@1.5,@2.,@2.5,@3.,@3.5,@4.,@4.5,@5.,@5.5,@6.,@6.5,@7.]];
self.dwarves = @[@1.5,@2.,@2.5,@3.,@3.5,@4.,@4.5,@5.,@5.5,@6.,@6.5,@7.];
你正在传递一个 NSArray
并且它是不可变的:
self.dwarves = [@[@1.5,@2.,@2.5,@3.,@3.5,@4.,@4.5,@5.,@5.5,@6.,@6.5,@7.] mutableCopy];
//or
self.dwarves = [[NSMutableArray alloc] initWithArray:@[@1.5,@2.,@2.5,@3.,@3.5,@4.,@4.5,@5.,@5.5,@6.,@6.5,@7.]];
将允许您 modify/update/edit 您的 NSMutableArray
中的值。
使其可变后,您需要:
- (IBAction)addData:(id)sender
{
[_dwarves addObject:@100];
UITableView *tableView = (id)[self.view viewWithTag:1];
[tableView reloadData];
// or
[(UITableView *)[self.view viewWithTag:1] reloadData];
// to see the changes you made after adding data to your dataSource
}