【问题标题】:UITableView deleteRowsAtIndexPaths doesn't delete rowUITableView deleteRowsAtIndexPaths 不删除行
【发布时间】:2011-04-07 07:04:29
【问题描述】:

我在 IB 中设计了 UIView,并在其上设计了 UITableView。在视图控制器的 viewDidLoad 方法中,我初始化了我的自定义 UITableViewContoller 并设置了 dataSource 和委托:

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

对 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

【问题讨论】:

    标签: iphone objective-c cocoa-touch uitableview


    【解决方案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 以使生活更轻松的包装器。

    【讨论】:

      【解决方案2】:

      问题出在 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];
      }
      

      【讨论】:

        【解决方案3】:

        试试这个

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

        【讨论】:

          猜你喜欢
          • 2014-01-14
          • 2016-03-07
          • 2017-09-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-05
          • 1970-01-01
          相关资源
          最近更新 更多