proposedDestinationIndexPath由于标题错误

proposedDestinationIndexPath由于标题错误

问题描述:

我想设置一个表格视图,我可以上下移动行。 当移动一个单元格时,tableView中为单元格制作空间的位置显示为大约2行太高。proposedDestinationIndexPath由于标题错误

当移动单元格,我通过登录所提出的indexPath:

- (NSIndexPath*) tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath { 
    NSLog(@"%ld", proposedDestinationIndexPath.row); 
} 

和登录行确实是关于我拿我的手机上减2

因为它出现在一行中,问题是由具有标题的表视图引起的,这似乎混淆了所提出的索引路径的计算。头在viewDidLoad加入一个固定的框架,但我不能在那个时候计算它的高度,因此其大小在viewWillLayoutSubviews设置:

- (void)viewWillLayoutSubviews { 

    [super viewWillLayoutSubviews]; 
    //.. 
    [myHeader setNeedsLayout]; 
    [myHeader layoutIfNeeded]; 
    CGRect newFrame = myHeader.frame; 
    newFrame.size.height = [self dynamicallyComputeHeight]; 
    myHeader.frame = newFrame; 
} 

所以它看起来像的tableView还没有完全更新有关如何的信息从视觉位置计算indexPath,但这不会在其他委托调用中弹出。

尝试重新加载您的tableView:

- (void)viewWillLayoutSubviews { 

    [super viewWillLayoutSubviews]; 
    // Add a flag to cehck if it has already been updated 
    if (_hasUpdatedHeaderFrame) { return } 
    _hasUpdatedHeaderFrame = YES; 

    [myHeader setNeedsLayout]; 
    [myHeader layoutIfNeeded]; 
    CGRect newFrame = myHeader.frame; 
    newFrame.size.height = [self dynamicallyComputeHeight]; 
    myHeader.frame = newFrame; 

    [_tableView reloadData]; 
} 
+0

这将创建一个无限循环 – vib

+0

@vib呀,不好意思来晚了,当我写到这,也许你应该有一个标志,这样做一次,我觉得你并不需要每次更新标题帧viewWillLayoutSubviews被反复调用?我编辑了答案 – 2017-02-12 14:09:40

+1

我本可以自己想出来的,对不起。你的答案解决了它,它是完美的。非常感谢 – vib