在canEditRowAtIndexPath方法中,UITableView的reloadData无法正常工作? [英] At canEditRowAtIndexPath Method, reloadData of UITableView not work properly?

查看:573
本文介绍了在canEditRowAtIndexPath方法中,UITableView的reloadData无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我从TableView中删除行后重新加载我的TableView [tablView reloadData]; 然后 canEditRowAtIndexPath 方法总是要求(以前)总行数。

In my application, I reload my TableView ([tablView reloadData];) after delete row from TableView then canEditRowAtIndexPath Method alway call for (pervious) total number Of Rows.

例如:

如果我在TableView上有 5 行,那么我从tableView中删除 1 行。删除后,我重新加载我的TableView [tablView reloadData] canEditRowAtIndexPath 方法调用 5次而不是 4次

If i have 5 Rows on my TableView, then i delete 1 row from tableView. After deleting, I reload my TableView ([tablView reloadData]) but canEditRowAtIndexPath Method calls 5 time instead of 4 times ??

所以我总是跟随错误:


因未捕获的异常'NSRangeException'终止应用程序,原因:' * ** - [__ NSArrayM objectAtIndex:]:索引5超出边界[0 .. 4]'

我也尝试在延迟一段时间后重新加载表(使用 NSTimer )但它也不适合我。

I also tried to reload table after some delay (using NSTimer) but it also not worked for me.

我在这里放了一些代码:

I在特定行上应用 canEditRowAtIndexPath @statusisEqualToString:@directory如此,

I apply canEditRowAtIndexPath on specific row which @"status" isEqualToString:@"directory" such like,

- (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;
}  

删除行代码:

- (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;
}

这里 updateListofSoundsFile 是我的自定义方法代码是:

Here updateListofSoundsFile is my custom method code is :

-(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];  
}

请给出任何建议,我该如何解决这个问题?

谢谢:)

Please Give any suggestion, How can i solve this issue ?
Thanks :)

推荐答案

你需要从tableview中删除raw也可以从数组中删除项目并使用此行重新加载数据因为从数组中删除项而不是tableview。

you need to remove raw from tableview also befor remove item from array and reload data using this line becouse remove item from array but not tableview.

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

- (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

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [self.listOfSounds removeObjectAtIndex:indexPath.row]; /// delete record from Array

        [self updateListofSoundsFile]; /// Custom method
    }
}

这篇关于在canEditRowAtIndexPath方法中,UITableView的reloadData无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆