当用户的手指超出屏幕时,UITableView 返回错误的 indexPath
UITableView returning wrong indexPath when user's finger goes outside the screen
我正在开发重新排序单元格 class 以允许我的用户重新排序 table 中的单元格。
过程很简单,我每超过一个就换一个cell的位置
过程 运行 非常顺利,直到我的手指滑出 iPhone 屏幕。在那种情况下,UIGestureRecognizer 或 UITableView 给我一个错误的 indexPath。它指的是我的 tableView 的第一个位置。
这是我的代码:
初始化手势识别
...
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(longPressGestureRecognized:)];
[_tableView addGestureRecognizer:longPress];
...
控制手势状态的方法
- (IBAction)longPressGestureRecognized:(id)sender {
UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
UIGestureRecognizerState state = longPress.state;
CGPoint location = [longPress locationInView:_tableView];
NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:location];
static UIView *snapshot = nil; ///< A snapshot of the row user is moving.
static NSIndexPath *sourceIndexPath = nil; ///< Initial index path, where gesture begins.
NSLog(@"State: %ld", (long)state);
switch (state) {
case UIGestureRecognizerStateBegan: {
NSLog(@"UIGestureRecognizerStateBegan");
if (indexPath) {
sourceIndexPath = indexPath;
Task *sourceTask = [_elements objectAtIndex:sourceIndexPath.row];
NSLog(@"superTask: state[%ld], %@, indexpath.row: %@, actualPosition: %@", (long)state, sourceTask.subtask, @(indexPath.row), sourceTask.position);
if (_collapseSubElements){
if ([sourceTask.subtask isEqualToString:@"0"]){
[self collapseAllTaskWithSubtasks];
sourceIndexPath = indexPath = [NSIndexPath indexPathForRow:[_elements indexOfObjectIdenticalTo:sourceTask] inSection:0];
}
UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
// Take a snapshot of the selected row using helper method.
snapshot = [self customSnapshoFromView:cell];
// Add the snapshot as subview, centered at cell's center...
__block CGPoint center = cell.center;
snapshot.center = center;
snapshot.alpha = 0.0;
[_tableView addSubview:snapshot];
[UIView animateWithDuration:0.25 animations:^{
// Offset for gesture location.
center.y = location.y;
snapshot.center = center;
snapshot.transform = CGAffineTransformMakeScale(1.05, 1.05);
snapshot.alpha = 0.98;
cell.alpha = 0.0;
} completion:^(BOOL finished) {
cell.hidden = YES;
}];
}
}
break;
}
case UIGestureRecognizerStateChanged: {
CGPoint center = snapshot.center;
center.y = location.y;
snapshot.center = center;
Task *sourceTask = [_elements objectAtIndex:sourceIndexPath.row];
Task *targetTask = [_elements objectAtIndex:indexPath.row];
NSLog(@"UIGestureRecognizerStateChanged: %ld - %ld - %@", (long)indexPath.row, (long)sourceIndexPath.row, sourceTask.position);
// Is destination valid and is it different from source?
if (indexPath && ![indexPath isEqual:sourceIndexPath]) {
if (indexPath.row - sourceIndexPath.row <= 1){
UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
UIButton *subtasksButton = (UIButton *)[cell viewWithTag:108];
// Solamente se pueden mover tareas dentro del mismo nivel
if ([sourceTask isBrotherOfTask:targetTask]){
if (_collapseSubElements){
if (subtasksButton.selected){
//[self expandCollapseSubtasks:subtasksButton];
_collapseCellsBlock(subtasksButton);
}
// ... update element position
NSNumber *sourceTaskPosition = sourceTask.position;
NSNumber *targetTaskPosition = targetTask.position;
_temporalValues = [NSMutableDictionary dictionaryWithObjectsAndKeys:
sourceTaskPosition, @"position", nil];
[targetTask updateWithValues: _temporalValues];
_temporalValues = [NSMutableDictionary dictionaryWithObjectsAndKeys:
targetTaskPosition, @"position", nil];
[sourceTask updateWithValues: _temporalValues];
// ... update data source.
[_elements exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row];
// ... move the rows.
[_tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath];
// ... and update source so it is in sync with UI changes.
sourceIndexPath = indexPath;
} else {
}
}
}
}
break;
}
case UIGestureRecognizerStateEnded:
{
NSLog(@"UIGestureRecognizerStateEnded");
Task *task = [_elements objectAtIndex:indexPath.row];
//if (indexPath && ![indexPath isEqual:sourceIndexPath]) {
NSLog(@"httpClient indexpath.row: %@, editedposition: %@", @(indexPath.row), task.position);
/*[_httpClient createUpdateRequestForObject:task withPath:@"task/" withRegeneration:NO];
[_httpClient update:nil];*/
_completionBlock(task);
}
default: {
// Clean up.
UITableViewCell *cell = [_tableView cellForRowAtIndexPath:sourceIndexPath];
cell.hidden = NO;
cell.alpha = 0.0;
[UIView animateWithDuration:0.25 animations:^{
snapshot.center = cell.center;
snapshot.transform = CGAffineTransformIdentity;
snapshot.alpha = 0.0;
cell.alpha = 1.0;
} completion:^(BOOL finished) {
sourceIndexPath = nil;
[snapshot removeFromSuperview];
snapshot = nil;
}];
break;
}
}
}
从所选单元格创建快照的方法
- (UIView *)customSnapshoFromView:(UIView *)inputView {
// Make an image from the input view.
UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, NO, 0);
[inputView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Create an image view.
UIView *snapshot = [[UIImageView alloc] initWithImage:image];
snapshot.layer.masksToBounds = NO;
snapshot.layer.cornerRadius = 0.0;
snapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0);
snapshot.layer.shadowRadius = 5.0;
snapshot.layer.shadowOpacity = 0.4;
return snapshot;
}
细胞运动
当我移动 8 号单元格时,一切正常,但是当我将 6 号单元格移到底部时,界面将单元格保持在正确的位置,但返回的 indexPath 为 0
我认为原因是 _tableView 的内容大小小于您指向的位置。
你可以检查这个:
if (_tableView.contentSize.height<location.y) indexPath = [NSIndexPath indexPathForRow:[_tableView numberOfRowsInSection:0]-1 inSection:0];
我正在开发重新排序单元格 class 以允许我的用户重新排序 table 中的单元格。
过程很简单,我每超过一个就换一个cell的位置
过程 运行 非常顺利,直到我的手指滑出 iPhone 屏幕。在那种情况下,UIGestureRecognizer 或 UITableView 给我一个错误的 indexPath。它指的是我的 tableView 的第一个位置。
这是我的代码:
初始化手势识别
...
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(longPressGestureRecognized:)];
[_tableView addGestureRecognizer:longPress];
...
控制手势状态的方法
- (IBAction)longPressGestureRecognized:(id)sender {
UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
UIGestureRecognizerState state = longPress.state;
CGPoint location = [longPress locationInView:_tableView];
NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:location];
static UIView *snapshot = nil; ///< A snapshot of the row user is moving.
static NSIndexPath *sourceIndexPath = nil; ///< Initial index path, where gesture begins.
NSLog(@"State: %ld", (long)state);
switch (state) {
case UIGestureRecognizerStateBegan: {
NSLog(@"UIGestureRecognizerStateBegan");
if (indexPath) {
sourceIndexPath = indexPath;
Task *sourceTask = [_elements objectAtIndex:sourceIndexPath.row];
NSLog(@"superTask: state[%ld], %@, indexpath.row: %@, actualPosition: %@", (long)state, sourceTask.subtask, @(indexPath.row), sourceTask.position);
if (_collapseSubElements){
if ([sourceTask.subtask isEqualToString:@"0"]){
[self collapseAllTaskWithSubtasks];
sourceIndexPath = indexPath = [NSIndexPath indexPathForRow:[_elements indexOfObjectIdenticalTo:sourceTask] inSection:0];
}
UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
// Take a snapshot of the selected row using helper method.
snapshot = [self customSnapshoFromView:cell];
// Add the snapshot as subview, centered at cell's center...
__block CGPoint center = cell.center;
snapshot.center = center;
snapshot.alpha = 0.0;
[_tableView addSubview:snapshot];
[UIView animateWithDuration:0.25 animations:^{
// Offset for gesture location.
center.y = location.y;
snapshot.center = center;
snapshot.transform = CGAffineTransformMakeScale(1.05, 1.05);
snapshot.alpha = 0.98;
cell.alpha = 0.0;
} completion:^(BOOL finished) {
cell.hidden = YES;
}];
}
}
break;
}
case UIGestureRecognizerStateChanged: {
CGPoint center = snapshot.center;
center.y = location.y;
snapshot.center = center;
Task *sourceTask = [_elements objectAtIndex:sourceIndexPath.row];
Task *targetTask = [_elements objectAtIndex:indexPath.row];
NSLog(@"UIGestureRecognizerStateChanged: %ld - %ld - %@", (long)indexPath.row, (long)sourceIndexPath.row, sourceTask.position);
// Is destination valid and is it different from source?
if (indexPath && ![indexPath isEqual:sourceIndexPath]) {
if (indexPath.row - sourceIndexPath.row <= 1){
UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
UIButton *subtasksButton = (UIButton *)[cell viewWithTag:108];
// Solamente se pueden mover tareas dentro del mismo nivel
if ([sourceTask isBrotherOfTask:targetTask]){
if (_collapseSubElements){
if (subtasksButton.selected){
//[self expandCollapseSubtasks:subtasksButton];
_collapseCellsBlock(subtasksButton);
}
// ... update element position
NSNumber *sourceTaskPosition = sourceTask.position;
NSNumber *targetTaskPosition = targetTask.position;
_temporalValues = [NSMutableDictionary dictionaryWithObjectsAndKeys:
sourceTaskPosition, @"position", nil];
[targetTask updateWithValues: _temporalValues];
_temporalValues = [NSMutableDictionary dictionaryWithObjectsAndKeys:
targetTaskPosition, @"position", nil];
[sourceTask updateWithValues: _temporalValues];
// ... update data source.
[_elements exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row];
// ... move the rows.
[_tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath];
// ... and update source so it is in sync with UI changes.
sourceIndexPath = indexPath;
} else {
}
}
}
}
break;
}
case UIGestureRecognizerStateEnded:
{
NSLog(@"UIGestureRecognizerStateEnded");
Task *task = [_elements objectAtIndex:indexPath.row];
//if (indexPath && ![indexPath isEqual:sourceIndexPath]) {
NSLog(@"httpClient indexpath.row: %@, editedposition: %@", @(indexPath.row), task.position);
/*[_httpClient createUpdateRequestForObject:task withPath:@"task/" withRegeneration:NO];
[_httpClient update:nil];*/
_completionBlock(task);
}
default: {
// Clean up.
UITableViewCell *cell = [_tableView cellForRowAtIndexPath:sourceIndexPath];
cell.hidden = NO;
cell.alpha = 0.0;
[UIView animateWithDuration:0.25 animations:^{
snapshot.center = cell.center;
snapshot.transform = CGAffineTransformIdentity;
snapshot.alpha = 0.0;
cell.alpha = 1.0;
} completion:^(BOOL finished) {
sourceIndexPath = nil;
[snapshot removeFromSuperview];
snapshot = nil;
}];
break;
}
}
}
从所选单元格创建快照的方法
- (UIView *)customSnapshoFromView:(UIView *)inputView {
// Make an image from the input view.
UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, NO, 0);
[inputView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Create an image view.
UIView *snapshot = [[UIImageView alloc] initWithImage:image];
snapshot.layer.masksToBounds = NO;
snapshot.layer.cornerRadius = 0.0;
snapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0);
snapshot.layer.shadowRadius = 5.0;
snapshot.layer.shadowOpacity = 0.4;
return snapshot;
}
细胞运动 当我移动 8 号单元格时,一切正常,但是当我将 6 号单元格移到底部时,界面将单元格保持在正确的位置,但返回的 indexPath 为 0
我认为原因是 _tableView 的内容大小小于您指向的位置。
你可以检查这个:
if (_tableView.contentSize.height<location.y) indexPath = [NSIndexPath indexPathForRow:[_tableView numberOfRowsInSection:0]-1 inSection:0];