0
我有UITableView上的UITapGestureRecognizer。在我稱之爲scrollToRowAtIndexPath:atScrollPosition:animated:
的方法之前,它很好用。一旦被調用,它就不起作用。一旦我打電話給setContentOffset:
,它也不起作用。我必須用手指滾動tableView,然後點擊手勢才能工作。如何在設置contentOffset或滾動到一行之後讓它工作?點擊手勢不工作setContentOffset後
-(void)viewDidLoad {
// The gesture that doesn't work after 'setContentOffset:'
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(hideShowEditing:)];
[tap setCancelsTouchesInView: YES];
[tap setDelegate: self];
[self setEditTap: tap];
[[self tableView] addGestureRecognizer: tap];
}
-(BOOL)hideShowEditing:(UIGestureRecognizer *)gesture
{
CGPoint location = [gesture locationInView: [self tableView]];
NSIndexPath *ip = [[self tableView] indexPathForRowAtPoint: location];
TaskCell *cell = (TaskCell *)[[self tableView] cellForRowAtIndexPath: ip];
// No cells are expanded, so expand this one
if (editingCellRow == -1 && editingCellSection == -1 && ![gesture isKindOfClass: [UISwipeGestureRecognizer class]]) {
editingCellRow = [ip row]; // if it's a swipe gesture, then it's from the crossOut method
editingCellSection = [ip section];
[[self tableView] beginUpdates];
[[self tableView] endUpdates];
[cell addViewsForEditing];
// Tap gesture only doesn't work if it has to scroll the tableView
[[self tableView] scrollToRowAtIndexPath: ip atScrollPosition: UITableViewScrollPositionNone animated: YES];
return YES;
}
// Another cell is expanded, so unexpand the other cell
// and expand this cell
else if ((editingCellRow != [ip row] || editingCellSection != [ip section]) && ![gesture isKindOfClass: [UISwipeGestureRecognizer class]]) {
// Index path of the expanded cell
NSIndexPath *expandedIp = [NSIndexPath indexPathForRow: editingCellRow inSection: editingCellSection];
TaskCell *expandedCell = (TaskCell *)[[self tableView] cellForRowAtIndexPath: expandedIp];
[expandedCell setAnimateExpansion: YES];
[cell setAnimateExpansion: YES];
// Store the index of the new expanded cell
editingCellRow = [ip row];
editingCellSection = [ip section];
[[self tableView] beginUpdates];
[[self tableView] endUpdates];
[expandedCell removeViewsForEditing];
[[self tableView] scrollToRowAtIndexPath: ip atScrollPosition: UITableViewScrollPositionNone animated: YES];
[cell addViewsForEditing];
return YES;
}
// Tapped the expanded cell, so unexpand it
else if (editingCellRow == [ip row] && editingCellSection == [ip section]) {
[cell setAnimateExpansion: YES];
editingCellRow = -1;
editingCellSection = -1;
[[self tableView] beginUpdates];
[[self tableView] endUpdates];
[cell removeViewsForEditing];
return YES;
}
return NO;
}
我真的不知道爲什麼它不工作的權利。我的代碼看起來和你的一樣。我要嘗試使用按鈕而不是輕按手勢。 – bbraunj
正如您所做的那樣,我嘗試在方法中配置輕擊手勢(editTap)。它也可以工作。你可以給我們hideShowEditing:方法嗎? – Franck
我爲你添加了方法 – bbraunj