Ios 更改TableView目标C中的数据时出错
我一直在研究Nutting、Olsson和Mark iOS7教科书中的一个tableview示例。我有一个tableview运行良好,所以我在视图中添加了一个按钮。当我触摸按钮内部时,它调用addData方法。addData只是将另一个对象附加到表中显示的数组中 代码编译得很好,但是当我点击按钮时,它崩溃了。为什么会这样?我不明白错误信息,但这是代码Ios 更改TableView目标C中的数据时出错,ios,objective-c,uitableview,unrecognized-selector,Ios,Objective C,Uitableview,Unrecognized Selector,我一直在研究Nutting、Olsson和Mark iOS7教科书中的一个tableview示例。我有一个tableview运行良好,所以我在视图中添加了一个按钮。当我触摸按钮内部时,它调用addData方法。addData只是将另一个对象附加到表中显示的数组中 代码编译得很好,但是当我点击按钮时,它崩溃了。为什么会这样?我不明白错误信息,但这是代码 #import "ViewController.h" @interface ViewController () @property (copy,
#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'
这段代码有什么问题?无论您如何声明矮人,它都是NSArray而不是NSMutableArray。因此addObject崩溃。我相信“复制”属性和可变类型的组合并不能达到您认为它应该达到的效果 最好让“矮人”只是一个普通的“强壮”属性,但要初始化
self.dwarves = [@[@1.5,@2.,@2.5,@3.,@3.5,@4.,@4.5,@5.,@5.5,@6.,@6.5,@7.] mutableCopy];
使用以下代码设置数组,然后可以从中添加/删除对象
[[NSMutableArray alloc]initWithArray:@[@1.5、@2.5、@3.5、@4.5、@5.5、@6.5、@7.5]代码>self.dwarves=@[@1.5、@2.5、@3.5、@4.5、@5.5、@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.]];
将允许您修改/更新/编辑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
}
非常感谢!现在可以了!如果我想在调用addData时重新加载表视图,以便它显示100,我该怎么做呢?在创建矮人时应该会收到警告,因为当您将其键入为可变数组时,您正在创建一个不可变数组。你没有得到一个黄色的小三角警告吗?没有,但纳舍的解决方案非常有效!