2011-04-07 68 views
2

我有在IB设计的UIView和UITableView。鉴于位指示的viewDidLoad方法我初始化我的自定义UITableViewContoller并设置DataSource和委托:UITableView deleteRowsAtIndexPaths不会删除行

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    ... 
    GoodsTableController *goodsController = [[GoodsTableController alloc] init]; 
    goodsController.data = [[orderData objectForKey:@"Result"] objectForKey:@"Items"]; 
    self.gtc = goodsController; 
    [goodsController release]; 

    goodsTable.delegate = self.gtc; 
    goodsTable.dataSource = self.gtc; 
    [goodsTable reloadData];} 

Intresting在GoodsTableController:

//in the header file 
@interface GoodsTableController : UITableViewController { 
     NSMutableArray *data; 
} 

@property (nonatomic, retain) NSMutableArray *data; 

//implementation 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
     NSLog(@"Current data size %d", [data count]); 
     return [data count]; 
} 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     if (editingStyle == UITableViewCellEditingStyleDelete) { 
       NSLog(@"Deleting row at index %d", indexPath.row); 
       [self.tableView beginUpdates]; 

       [self.data removeObjectAtIndex:indexPath.row]; 
       [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

       [self.tableView endUpdates]; 
       [self.tableView reloadData]; 
     } 
} 

对于调试我使用的NSLog,我看到这个数组被改变,其大小发生了变化,tableView:numberOfRowsInSection被调用,但表不更新。行不隐藏。也就是说登录:

2011-04-07 13:08:18.784 Test[32300:207] Current data size 2 //first call tableView:numberOfRowsInSection: by reloadData in View Controller after set data 
2011-04-07 13:08:23.486 Test[32300:207] Current data size 2 //call after slide on row 
2011-04-07 13:08:26.091 Test[32300:207] Deleting row at index 0 
2011-04-07 13:08:26.093 Test[32300:207] Current data size 1 //call after endUpdate 
2011-04-07 13:08:26.096 Test[32300:207] Current data size 1 //call after reloadData 

回答

0

的问题是在GoodsTableController实例未定义的tableView。 固定码:

- (void)viewDidLoad { 
     [super viewDidLoad]; 
     ... 
     GoodsTableController *goodsController = [[GoodsTableController alloc] init]; 
     goodsController.data = [[orderData objectForKey:@"Result"] objectForKey:@"Items"]; 
     self.gtc = goodsController; 
     [goodsController release]; 

     goodsTable.delegate = self.gtc; 
     goodsTable.dataSource = self.gtc; 

     //Fix there: 
     self.gtc.tableView = goodsTable; 
     [goodsTable reloadData]; 
} 
0

试试这个

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    int count = [NSMutableArray count]; 
    return count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    cell.selectionStyle = UITableViewCellSelectionStyleBlue; 
    cell.textLabel.text = [NSMutableArray objectAtIndex:indexPath.row]; 
    return cell; 
} 

- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (!indexPath) return 
    { 
     UITableViewCellEditingStyleNone; 
    } 

    if (indexPath.row == ([NSMutableArray count])) 
    { 
     return UITableViewCellEditingStyleInsert; 
    } 
    else 
    { 
     return UITableViewCellEditingStyleDelete; 
    } 

    return UITableViewCellEditingStyleNone; 
} 

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Code for do anything after select row in table view 
} 

- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     [recordedFileList removeObjectAtIndex:indexPath.row]; 
     [recordTableView reloadData]; 
    } 
} 

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return YES; 
} 

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
{ 
    NSString *item = [[NSMutableArray objectAtIndex:fromIndexPath.row] retain]; 
    [NSMutableArray removeObject:item]; 
    [NSMutableArray insertObject:item atIndex:toIndexPath.row]; 
    [item release]; 
} 
+0

与我的方式有什么不同? – Kirill 2011-04-07 08:04:56

1

我研究的UITableView的删除机制,插入&重装。我正在Github中维护我的项目来解决这个问题。

https://github.com/VansonLeung/VanTableController

它包括在其一个工作单元测试。通常情况下,以动画的UITableView的单元格动画,我们必须做这样的事情:

[tableView_ beginUpdates]; 
[tableView_ deleteRowsAtIndexPaths: indexPathDelete withRowAnimation:UITableViewRowAnimationAutomatic]; 
[tableView_ insertRowsAtIndexPaths: indexPathInsert withRowAnimation:UITableViewRowAnimationAutomatic]; 
[tableView_ reloadRowsAtIndexPaths: indexPathReload withRowAnimation:UITableViewRowAnimationAutomatic]; 
[tableView_ endUpdates]; 

我们必须确定删除行,插入行&重装行indexPath阵列,这是一个非常讨厌的工作!我的项目工作基本上是生成这些indexPaths以使生活更轻松的包装。