iPhone开发:编辑分组的UITableView

时间:2009-07-03 09:12:32

标签: iphone uitableview

我们即将向App商店提交申请,我们在删除表格中的行和部分时遇到了一个非常奇怪的错误。

到目前为止,对于兼容性抖动,我们一直在使用2.0链接(这样任何iPhone都可以运行我们的代码)。

对于那种情况,我们有一个允许删除项目的组表,这就是我们处理项目删除的方式:

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

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        NSMutableArray* sect = (NSMutableArray*)[tableContents objectAtIndex:indexPath.section];
        [sect removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];

        //if the section is empty remove it
        if ([sect count] == 0){
            [tableContents removeObjectAtIndex:indexPath.section];
            [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
        }

        [tableView reloadData];
    }   
}

这个小代码工作得很好,但我们有一个问题惠特部分标题。 当您删除某个部分中的最后一行(最后一行)时,该部分标题仍保留在视图中,漂浮在不知名的中间。

我们缺少什么?

提前致谢

Gonso。

添加beginUpdate / endUpdates后,我在控制台中得到了一个EXC_BAD_ACCES和这个输出:

#0  0x30beae79 in -[UITableViewRowData rectForFooterInSection:] ()
#1  0x30a9f02d in -[UITableView(_UITableViewPrivate) _updateWithItems:withOldRowData:oldRowRange:newRowRange:] ()
#2  0x30a9a541 in -[UITableView(_UITableViewPrivate) _endCellAnimations] ()
#3  0x0000276a in -[RootViewController tableView:commitEditingStyle:forRowAtIndexPath:] (self=0x5248c0, _cmd=0x30c620b0, tableView=0x528c40, editingStyle=UITableViewCellEditingStyleDelete, indexPath=0x52e6e0) at /Users/MacAdmin/Documents/ws/LabTest/TableViewDeletes/Classes/RootViewController.m:135
#4  0x30a9886c in -[UITableView(UITableViewInternal) animateDeletionOfRowWithCell:] ()
#5  0x30b06d40 in -[UIRemoveControl _doRemove:] ()
#6  0x30a4eee6 in -[UIApplication sendAction:to:from:forEvent:] ()
#7  0x30ab0d36 in -[UIControl sendAction:to:forEvent:] ()
#8  0x30ab11fe in -[UIControl(Internal) _sendActionsForEvents:withEvent:] ()
#9  0x30ab0544 in -[UIControl touchesEnded:withEvent:] ()
#10 0x30a7dc73 in -[UIScrollView touchesEnded:withEvent:] ()
#11 0x30a96a9f in -[UITableView touchesEnded:withEvent:] ()
#12 0x30a67917 in -[UIWindow sendEvent:] ()
#13 0x30a56fff in -[UIApplication sendEvent:] ()
#14 0x30a561e0 in _UIApplicationHandleEvent ()
#15 0x31565dea in SendEvent ()
#16 0x3156840c in PurpleEventTimerCallBack ()
#17 0x9200d615 in CFRunLoopRunSpecific ()
#18 0x9200dcf8 in CFRunLoopRunInMode ()
#19 0x31566600 in GSEventRunModal ()
#20 0x315666c5 in GSEventRun ()
#21 0x30a4eca0 in -[UIApplication _run] ()
#22 0x30a5a09c in UIApplicationMain ()
#23 0x00001fb0 in main (argc=1, argv=0xbfffee88) at /Users/MacAdmin/Documents/wsPogo/LabTest/TableViewDeletes/main.m:14

这是最终代码,如果您仅链接2.0,则可以使用。

if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source
        NSMutableArray* sect = (NSMutableArray*)[tableContents objectAtIndex:indexPath.section];
        [sect removeObjectAtIndex:indexPath.row];
        //if its the last row left in the section, just delete the section.
        //if the section is empty remove it
        if ([sect count] == 0){
            [tableContents removeObjectAtIndex:indexPath.section];
            [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
        } else {
            //section is not empty, just remove the row
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
        }
        [tableView reloadData];
}   

谢谢!

2 个答案:

答案 0 :(得分:2)

我不确定这是否适用于2.0,因为我直接使用3.0和3.1 beta版,但您可以尝试如下,围绕beginUpdates / endUpdates块中的行/部分删除。如果这对您在2.0上有用,请务必阅读与这些方法相关的文档,以准确了解您在此类块中允许执行的操作以及严格禁止的操作。请注意,我已经在块之前移动了模型更新。希望这会有所帮助。

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

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        NSMutableArray* sect = (NSMutableArray*)[tableContents objectAtIndex:indexPath.section];
        [sect removeObjectAtIndex:indexPath.row];
        if ([sect count] == 0)
            [tableContents removeObjectAtIndex:indexPath.section];

        [tableView beginUpdates]
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];

        //if the section is empty remove it
        if ([sect count] == 0){
                [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
        }
        [tableView endUpdates]

        [tableView reloadData];
    }   
}

答案 1 :(得分:0)

您无法同时删除行和部分。试试这个:

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

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        NSMutableArray* sect = (NSMutableArray*)[tableContents objectAtIndex:indexPath.section];
        [tableView beginUpdates];
        if ([sect count] == 1)
        {
            [tableContents removeObjectAtIndex:indexPath.section];
            [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
        }
        else{
            [sect removeObjectAtIndex:indexPath.row];
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
        }
        [tableView endUpdates];

    }   
}