У меня есть редактируемый UITableView
(с добавлением и удалением элементов) в моем приложении. Это работает странно.
Пока у меня есть только одна или две строки, все работает отлично. Но если я добавлю больше предметов, у меня есть исключение '*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'
Странное поведение UITableView при редактировании
Мне не удалось установить контрольные точки и обработать его. Может, кто-нибудь может мне помочь?
#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];
}
}
Какой тип 'typeList'? Я думал, что это 'NSArray', но вызов' [typeList getObject: [indexPath row]] 'вызывает у меня сомнения. Где вызов 'objectAtIndex:', во всяком случае? – dasblinkenlight
Это не NSArray, это пользовательский класс, основанный на NSObject с методом getObject ... это работает нормально. – Andrey