繁体   English   中英

最后一行删除后如何重新加载自定义表格视图单元格?

[英]How to reload custom table view cell after last row deletion?

我有一个带有简单数组作为数据源的UITableView 我为特殊的UITableViewCell设计了一个自定义笔尖,该UITableViewCell在数组为空(即应用程序首次启动时)时显示。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([self.wallet count] == 0)
        return 1;
    else return [self.wallet count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // If there isn't any coupon yet, then show the custom "No Coupon Yet" cell
    if ([self.wallet count] == 0)
        return [tableView dequeueReusableCellWithIdentifier:@"NoCouponCell" forIndexPath:indexPath];
    else {
        CouponCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CouponCell" forIndexPath:indexPath];
        Coupon *coupon = [self.wallet objectAtIndex:indexPath.row];

        [cell configureForCoupon:coupon];
        return cell;
    }
}

由于我想添加滑动删除功能,因此我为视图控制器提供了以下方法。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.wallet removeObjectAtIndex:indexPath.row];
    // Creates a new, temporary array holding just the one index-path item
    NSMutableArray *tmp = [[NSMutableArray alloc] initWithObjects:indexPath, nil];
    // Tells the table view to delete the row with a nice animation
    [tableView deleteRowsAtIndexPaths:tmp withRowAnimation:UITableViewRowAnimationAutomatic];
}

但是,当表视图只有一行并且我尝试删除它时,我的应用程序崩溃了。 为什么?

编辑:调试信息告诉您引发了NSInternalInconsistencyException

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) 
    {
        // Delete the row from the data source
        [self.wallet removeObjectAtIndex:indexPath.row];

        [tableView reloadData]; // reload your table to see updates

        // or if you what some animation use|

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath] 
                         withRowAnimation:UITableViewRowAnimationAutomatic];  

...


这是您的cellForRowAtIndexPath的修复程序:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CouponCell *cell = [tableView dequeueReusableCellWithIdentifier:[self.wallet count] == 0 ? @"NoCouponCell" : @"CouponCell" forIndexPath:indexPath];

    if ([self.wallet count] > 0)
    {
        Coupon *coupon = [self.wallet objectAtIndex:indexPath.row];

        [cell configureForCoupon:coupon];
    }
    return cell;
}

在您的代码中尝试以下操作:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [self.wallet count];
}

在您的numberOfRowsInSection函数中使用此函数:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

      return [self.wallet count];
}

然后在commitEditingStyle函数的末尾使用

[tableView reloadData]; 

尝试这段代码:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.wallet removeObjectAtIndex:indexPath.row];
    // Creates a new, temporary array holding just the one index-path item
    NSMutableArray *tmp = [[NSMutableArray alloc] initWithObjects:indexPath, nil];
    // Tells the table view to delete the row with a nice animation
    [tableView beginUpdates];
    [tableView deleteRowsAtIndexPaths:tmp withRowAnimation:UITableViewRowAnimationAutomatic];
    [tableView endUpdates];
}

如果数据数组wallet没有元素,则返回的行数为1:

 if ([self.wallet count] == 0)
        return 1;
    else return [self.wallet count];

但是在commitEditingStyle方法中,您无法处理这种情况:这意味着可以删除NoCouponCell 而且我认为发生崩溃是因为您要从数据数组中删除一个不存在的对象。

[self.wallet removeObjectAtIndex:indexPath.row];

解决方案 :使用以下委托方法来确定何时可以编辑单元格:这可以防止调用委​​托方法commitEditingStyle

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return [self.wallet count] > 0
}

您可以在tableview数据源上的UITableview tableFooterView中添加视图,可以在此视图中放置客户单元格的内容

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (dataArray.count == 0) {
        UIView *view = [[UITableView alloc] initWithFrame:self.view.bounds];
        view.backgroundColor = [UIColor blueColor];
        self.tableView.tableFooterView = view;
    }
    return dataArray.count;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM