[英]Strange behavior of UITableView when editing
I have a editable UITableView
( with adding and deleting elements ) in my application. 我的应用程序中有一个可编辑的
UITableView
( 带有添加和删除元素 )。 It working strange. 它的工作很奇怪。
While I have only one or two lines in it, everything is working perfect. 虽然我只有一两行,但一切工作都很完美。 But if I add more items, I have an exception
'*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'
但是,如果我添加更多项,则会出现异常
'*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'
I was unable to put breakpoints and handle it. 我无法放置断点并处理它。 Maybe, somebody can help me?
也许有人可以帮我吗?
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int plusRow = 0;
if ([[self tableView] isEditing]) plusRow = 1;
return [typeList count] + plusRow;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([indexPath row] > ([typeList count]-1)) {
NSString * appendCellDescription = @"";
if ([[self tableView] isEditing]) appendCellDescription = @"Add";
[[cell textLabel] setText:appendCellDescription];
} else {
NSLog(@"Accessing array: %d", [indexPath row]);
[[cell textLabel] setText:[[typeList getObject:[indexPath row]] description]];
}
return cell;
}
#pragma mark - Editing table view
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"Row: %d, count: %d", [indexPath row], [typeList count]);
if (indexPath.row > [typeList count]-1) {
return UITableViewCellEditingStyleInsert;
} else {
return UITableViewCellEditingStyleDelete;
}
}
- (IBAction)btnModifyClick:(id)sender{
if ([[self tableView] isEditing]) {
[[self tableView] setEditing:FALSE animated:YES];
[[self tableView] reloadData];
} else {
[[self tableView] setEditing:TRUE animated:YES];
[[self tableView] reloadData];
}
}
Generally This Error describe, When mistake in Array Index such like you array have 3 item and you tring to get/abstract 4th item from Array, at that time this type of Error generate. 通常,此错误描述,当诸如数组之类的数组索引中的错误有3项并且您试图从Array中获取/提取第4项时,此时会产生这种类型的错误。
Best way to find error use BreakPint. 查找错误的最佳方法是使用BreakPint。 and Debug your project line by line.
并逐行调试项目。 and find where your app.
并找到您的应用的位置。 crush ??
暗恋 i am sure that it cruse Around any array.
我敢肯定,它遍及任何数组。
EDIT: 编辑:
I thinks mistake in array name typeList
check it in cellForRowAtIndexPath
method with BreakPoint. 我认为数组名称
typeList
错误会在带有BreakPoint的cellForRowAtIndexPath
方法中进行检查。
numberOfRowsInSection
is returning higher number of items count than the one fetched in cellForRowAtIndexPath
datasource method, try debugging the returned value: numberOfRowsInSection
返回的项目数比cellForRowAtIndexPath
数据源方法中获取的项目数高,请尝试调试返回的值:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int plusRow = 0;
if ([[self tableView] isEditing]) plusRow = 1;
NSLog(@"%i",[typeList count] + plusRow);
return [typeList count] + plusRow;
}
I have found a way, to solve my problem. 我找到了解决问题的方法。 I just changed "Sections" to 0 in XCode properties in storyboard TableView.
我只是在情节提要TableView的XCode属性中将“ Sections”更改为0。 And now working ok.
现在工作正常。
Thanks to everyone to response! 感谢大家的回应!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.