2

我是一个可滑动的 tableView 单元格,在该单元格中有一个用于删除该行的按钮。我的方法中有这段代码来删除该行。该行的内容已从 DataSource 中正确删除,但我在隐藏和重新加载我的 tableView 时遇到了麻烦。这是我的代码:

- (void)deleteElementAtIndexPath:(NSIndexPath *)indexPath {
    [dateTableView beginUpdates];

    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];

    listTitles = [[NSMutableArray alloc] init];

    listTitles = [[user objectForKey:@"listTitles"] mutableCopy];

    [listTitles removeObjectAtIndex:indexPath.row];

    [user setObject:listTitles forKey:@"listTitles"];
    [user synchronize];

    [dateTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [dateTableView endUpdates];
    [dateTableView reloadData];
}

我的班级有委托和数据源协议。这也是我的委托方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSLog(@"reloading...numberofsection");
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"reloading...numberofrowsinsection");

    return [listTitles count];
}
4

3 回答 3

2

一些东西:

- (void)deleteElementAtIndexPath:(NSIndexPath *)indexPath {

    // [dateTableView beginUpdates]; This shouldn't be placed here

    NSLog(@"This method was called: %@", NSStringFromSelector(_cmd)); // Check if this method is even called       

    // NSUserDefaults *user = [NSUserDefaults standardUserDefaults]; don't bother creating a new object

    // listTitles = [[NSMutableArray alloc] init]; Since you are reinitialising down below this is not needed

    // listTitles = [[user objectForKey:@"listTitles"] mutableCopy]; Don't bother creating a new object

    [[[[NSUserDefaults standardUserDefaults] objectForKey:@"listTitles"] mutableCopy] removeObjectAtIndex:indexPath.row];
    [[NSUserDefaults standardUserDefaults] synchronize];

    [dateTableView beginUpdates];
    [dateTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [dateTableView endUpdates];

    // Now try this - THIS IS THE MOST IMPORTANT CHANGE: calling -setNeedsDisplay**
    [dateTableView reloadData];
    [dateTableView setNeedsDisplay];
}

其他几件事:

我的班级有委托和数据源协议。这也是我的委托方法:

和都是数据源方法-numberOfSectionsInTableView:-numberOfRowsInSection:请确保您已经实现了另一个强制-cellForRowAtIndexPath:方法,您的委托方法如下-commitEditingStyle::和-didSelectRowAtIndexPath:

这也是一个很好的开源单元刷卡项目

于 2013-08-09T01:30:17.063 回答
1

取出这些线 -

listTitles = [[NSMutableArray alloc] init];

listTitles = [[user objectForKey:@"listTitles"] mutableCopy];

[dateTableView reloadData];

然后绝对完全确保您的 IBOulet 已连接。我看到您正在使用一个名为的 tableView 实例dateTableView,这表明您正在使用自定义 UIViewController 类。如果您为此使用了 Interface Builder,则必须确保已将 Outlet 连接到表。如果没有,在某处调用 dateTableView 就像向 nil 发送消息一样。

于 2013-08-08T19:40:08.983 回答
0

NSMutableArray 的分配是无用的,因为您要覆盖下一行中的变量。如果调用 beginUpdates 和 endUpdates,也不需要 reloadData。

确保您作为参数传递的 indexPath 中包含正确的 Section,也许 tableview 会尝试删除错误部分的行。如果该部分是正确的,您应该检查您的 UITableViewDataSource 的实现。

验证

listTitles = [[user objectForKey:@"listTitles"] mutableCopy];

不返回零。否则,您在此对象上调用的每个新方法都将无效。您还应该记录从 numberOfRowsInSection: 返回的单元格计数。顺便说一句,更清洁的实现将是:

@property (strong, nonatomic) NSMutableArray * listTitles;
@synthesize listTitles;

- (void)deleteElementAtIndexPath:(NSIndexPath *)indexPath {
    [dateTableView beginUpdates];

    [self.listTitles removeObjectAtIndex:indexPath.row];

    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:self.listTitles forKey:@"listTitles"];
    [user synchronize];

    [dateTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [dateTableView endUpdates];
}

在构造函数(或控制器的 viewDidLoad)中,执行以下操作

self.listTitles = [[[NSUserDefaults standardUserDefaults] objectForKey:@"listTitles"] mutableCopy];
if (self.listTitles == nil) {
    self.listTitles = [[NSMutableArray alloc] init];
}
于 2013-08-08T19:37:43.913 回答