Trong ứng dụng của tôi, tôi lại TableView tôi ([tablView reloadData];
) sau hàng xóa từ TableView sau đó canEditRowAtIndexPath
Phương pháp nhất luôn gọi cho (dể qua) tổng số hàng.Tại phương thức canEditRowAtIndexPath, tải lạiData của UITableView không hoạt động đúng?
Ví dụ:
Nếu tôi có Rows trên TableView của tôi, sau đó tôi xóa hàng từ tableView. Sau khi xóa, tôi reload TableView tôi ([tablView reloadData]
) nhưng canEditRowAtIndexPath
Phương pháp gọi 5 lần thay vì 4 lần ??
Vì vậy, tôi luôn nhận được sau Lỗi:
Terminating app due to uncaught exception 'NSRangeException', reason: '* **-[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4]'
Tôi cũng đã cố gắng để tải lại bảng sau khi một số chậm trễ (sử dụng NSTimer
) nhưng nó cũng không làm việc cho tôi.
tôi đặt một số mã ở đây:
tôi áp dụng canEditRowAtIndexPath
trên hàng cụ thể mà @"status" isEqualToString:@"directory"
ví dụ như,
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%d", self.listOfSounds.count);
// Return NO if you do not want the specified item to be editable.
if([[[self.listOfSounds objectAtIndex:indexPath.row] objectForKey:@"status"] isEqualToString:@"directory"])
return YES;
else
return NO;
}
Mã của hàng xóa:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[self.sql_ deleteSoundFileAudioTableWhereMainID:[[self.listOfSounds objectAtIndex:indexPath.row] objectForKey:@"main_id"]]; /// delete record from DB
[self.listOfSounds removeObjectAtIndex:indexPath.row]; /// delete record from Array
[self updateListofSoundsFile]; /// Custom method
}
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO; // i also tried to return YES;
}
đây updateListofSoundsFile
là của tôi mã phương thức tùy chỉnh là:
-(void)updateListofSoundsFile
{
if(self.listOfSounds.count > 0)
[self.listOfSounds removeAllObjects];
self.listOfSounds = [self.sql_ getAllDataFromAudioTable]; // get all record from DB
NSLog(@"%d",self.listOfSounds.count);
[self.tblView reloadData];
}
Vui lòng đưa ra bất kỳ đề xuất nào, Làm cách nào để giải quyết vấn đề này?
Cảm ơn :)
Đăng phương thức 'numberOfRowsInSection' của bạn. Câu hỏi - Trong phương thức 'commitEditingStyle' của bạn, tại sao bạn gọi' updateListOfSoundsFile'? Bạn đã cập nhật 'listOfSounds' bằng cách xóa một đối tượng. Sau khi loại bỏ đối tượng khỏi mảng, chỉ cần gọi 'deleteRowsAtIndexPaths' để xóa một hàng khỏi bảng.Ngoài ra, hãy xác minh rằng lệnh gọi của bạn tới 'deleteSoundFileAudioTableWhereMainID' thực sự đang xóa một hàng khỏi cơ sở dữ liệu. – rmaddy