【问题标题】:Can I call reloadRowsAtIndexPaths... inside moveRowAtIndexPath...?我可以在 micRowAve IndexPath... 中调用 reloadRows At IndexPath... 吗?
【发布时间】:2015-06-26 22:38:28
【问题描述】:

在我的UITableView 中,我有一个特定的单元格 (A),其外观取决于同一部分中所有其他单元格的顺序。因此,每当用户将单元格拖动到表格视图中的新位置时,我都需要更新单元格 (A) 的内容。

在我看来,最好的方法是在表格视图完成对单元格重新排序后调用的方法中调用reloadRowsAtIndexPath:@[indexPathOfCellA],例如didMoveRowFromIndexPath:toIndexPath:。可惜没有这样的方法。

因此我尝试在moveRowFromIndexPath:toIndexPath: 方法的末尾调用reloadRowsAtIndexPath:@[indexPathOfCellA]

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    NSString *element = [self.dataSource objectAtIndex:sourceIndexPath.row];
    [self.dataSource removeObjectAtIndex:sourceIndexPath.row];
    [self.dataSource insertObject:element atIndex:destinationIndexPath.row];
    [self updateInfoCellInTableView:tableView];
}

用相应的方法:

- (void)updateExampleCellInTableView:(UITableView *)tableView {
    UInt32 infoCellRow = [tableView numberOfRowsInSection:0] - 1;
    NSIndexPath *indexPathForInfoCell = [NSIndexPath indexPathForRow:infoCellRow inSection:0];
    [tableView reloadRowsAtIndexPaths:@[indexPathForInfoCell] withRowAnimation:UITableViewRowAnimationAutomatic];
}

这不能正常工作。

当将单元格 (A) 拖动到新位置 (B) 时,该位置 (B) 的旧单元格首先向上或向下移动,以便为单元格 (A) 腾出空间。但是当我抬起手指确认重新排序的单元格 (B) 移回其原始位置 (B) 时,导致两个单元格重叠。 (我可以看到,因为单元格的背景是半透明的。)

我的假设是不允许从moveRowAtIndexPath:toIndexPath: 内部调用reloadRowsAtIndexPath:。但是,我在 Apple 的开发者文档中找不到这样的规则。

这个假设正确吗?重新排序表格视图后如何更新特定单元格?

【问题讨论】:

    标签: ios uitableview reload


    【解决方案1】:

    我已经解决了这个问题,这里是一个简单的例子。请过一遍。它可能会帮助你。

    - (void)viewDidLoad
    {
       [super viewDidLoad];
       // Do any additional setup after loading the view, typically from a nib.
    
       tblData=[[UITableView alloc]init];
       [tblData setDataSource:self];
       [tblData setDelegate:self];
       [tblData setFrame:CGRectMake(0, 75, 320, 430)];
       [self.view addSubview:tblData];
       [tblData setEditing:YES animated:NO];
    
       mutArr=[[NSMutableArray alloc]init];
       [mutArr addObject:@"Object 1"];
       [mutArr addObject:@"Object 2"];
       [mutArr addObject:@"Object 3"];
       [mutArr addObject:@"Object 4"];
       [mutArr addObject:@"Object 5"];
       [mutArr addObject:@"Object 6"];
       [mutArr addObject:@"Object 7"];
       [mutArr addObject:@"Object 8"];
       [mutArr addObject:@"Object 9"];
       [mutArr addObject:@"Object 10"];
    
    }
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
       return mutArr.count;
    }
    
    -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
       UITableViewCell *cell=[[UITableViewCell alloc]init];
       cell.textLabel.text=[mutArr objectAtIndex:indexPath.row];
    
       return cell;
    
    }
    
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    {
       return YES;
    }
    
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
    {
    
       NSString *element = [mutArr objectAtIndex:sourceIndexPath.row];
       [mutArr removeObjectAtIndex:sourceIndexPath.row];
       [mutArr insertObject:element atIndex:destinationIndexPath.row];
    
    }
    
    - (void)tableView:(UITableView *)tableView willBeginReorderingRowAtIndexPath:(NSIndexPath *)indexPath
    {
       previousPosition=(int)indexPath.row;
    }
    
    
    - (void)tableView:(UITableView *)tableView didEndReorderingRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
       UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
       cell.textLabel.text=[NSString stringWithFormat:@"This cell moved from :%d to :%ld position",previousPosition,(long)indexPath.row];
    
    }
    

    这里的“previousPosition”是一个整数,声明为全局变量。

    【讨论】:

    • tableView:didEndReorderingRowAtIndexPath: 看起来与我正在寻找的方法一模一样。但是,它是undocumented,因此我会避免使用它,因为该应用程序可能会被拒绝,并且当 Apple 决定删除该方法时它会中断。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    • 2011-06-27
    • 2019-01-12
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    相关资源
    最近更新 更多