我有一个应用程序可以完成它应该对UITableView的前8行做的所有事情...一旦我添加第9行(因此tableview必须滚动)应用程序崩溃...
我尝试了很多变化,但似乎没有任何效果......我可以根据自己的喜好将细胞添加到我的行中,但是一旦我填充了#34;该表中的第9行,应用程序崩溃
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
以下是用于在tableview中的行中生成信息的一些代码(tabledata是一个NSMutableArray,而areaNumber.text来自customCell)
-(IBAction)save_area:(id)sender {
{
UITableView *tableView = self.myTable;
NSInteger sections = tableView.numberOfSections;
NSMutableArray *cells = [[NSMutableArray alloc] init];
for (int section = 0; section < sections; section++) {
NSInteger rows = [tableView numberOfRowsInSection:section];
for (int row = 0; row < rows; row++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
SodTableCell *cell = [self.myTable cellForRowAtIndexPath:indexPath];//**here, for those cells not in current screen, cell is nil**
[cells addObject:cell];
[tabledata removeObjectAtIndex:indexPath.row];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSObject * object_areaNumber = [prefs objectForKey:@"tablerow_area_input_by_user"];
if(object_areaNumber != nil){
[tabledata insertObject:cell.areaNumber.text atIndex:indexPath.row];
}
NSUserDefaults *save1 = [NSUserDefaults standardUserDefaults];
[save1 setObject:self.tabledata forKey:@"tablerow_area_input_by_user"];
[save1 synchronize];
NSLog(@"From save button %@",[save1 valueForKey:@"tablerow_area_input_by_user"]);
}
}
}
}
该表可以逐行创建超过9个单元格的行,如果需要则可以创建数百个,并且滚动工作...这是当我将信息填充到第9行并保存使用代码输入的信息时往上看。任何帮助,非常感谢...
修改
在我的tableView中查看cellForRowAtIndexPath:
- (UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
SodTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(cell == nil){
cell = [[SodTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
if(indexPath.row >= tabledata.count && [self isEditing]){
cell.areaNumber.text = @"new";
}else{
cell.areaNumber.delegate = self;
cell.areaNumber.text = [tabledata objectAtIndex:indexPath.row];
NSUserDefaults *save1 = [NSUserDefaults standardUserDefaults];
[save1 setObject:self.tabledata forKey:@"tablerow_area_input_by_user"];
[save1 synchronize];
NSLog(@"From cell configuration %@",[save1 valueForKey:@"tablerow_area_input_by_user"]);
}
return cell;
}
答案 0 :(得分:1)
我不会以正确的方式谈论如何使用UITableView
,我只是告诉你为什么会崩溃。
错误表明您正在尝试将nil
对象插入数组。我猜,问题的根源在下面。
[tabledata insertObject:cell.areaNumber.text atIndex:indexPath.row];
要解决此问题,请在插入阵列之前检查cell.areaNumber.text
。
if(object_areaNumber != nil && cell.areaNumber.text){
[tabledata insertObject:cell.areaNumber.text atIndex:indexPath.row];
}
答案 1 :(得分:0)
<强>解决强>
我已经回顾了所有关于我对UITableViews和相关代码的误解的评论......
如果你查看以下链接(到我的Vimeo页面),你会看到一些漂亮的东西......
我能够在原始问题中加载超过8个单元格(如原始问题中所述),甚至能够加载空行,用户可以保存并稍后填写!一切保存在NSUserDefaults中,可以保存,编辑或删除......
再一次,谢谢你的帮助......当然,实际帮助我的评论总是比那些只是指出我不知道自己在做什么的人更受欢迎和建设性的......