3 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊
首先將長(zhǎng)按手勢(shì)識(shí)別器添加到表格視圖中:
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 2.0; //seconds
lpgr.delegate = self;
[self.myTableView addGestureRecognizer:lpgr];
[lpgr release];
然后在手勢(shì)處理程序中:
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
CGPoint p = [gestureRecognizer locationInView:self.myTableView];
NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:p];
if (indexPath == nil) {
NSLog(@"long press on table view but not on a row");
} else if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
NSLog(@"long press on table view at row %ld", indexPath.row);
} else {
NSLog(@"gestureRecognizer.state = %ld", gestureRecognizer.state);
}
}
您必須注意這一點(diǎn),以免干擾用戶對(duì)單元格的正常輕敲,并注意handleLongPress可能會(huì)觸發(fā)多次(這是由于手勢(shì)識(shí)別器狀態(tài)更改)。

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超2個(gè)贊
我已經(jīng)使用了安娜·卡列尼娜(Anna-Karenina)的答案,并且在出現(xiàn)嚴(yán)重錯(cuò)誤的情況下效果很好。
如果您使用的是節(jié),則長(zhǎng)按節(jié)標(biāo)題將導(dǎo)致您在按該節(jié)的第一行時(shí)得到錯(cuò)誤的結(jié)果,我在下面添加了一個(gè)固定版本(包括根據(jù)手勢(shì)狀態(tài)過(guò)濾虛擬呼叫, Anna-Karenina的建議)。
- (IBAction)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
CGPoint p = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];
if (indexPath == nil) {
NSLog(@"long press on table view but not on a row");
} else {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
if (cell.isHighlighted) {
NSLog(@"long press on table view at section %d row %d", indexPath.section, indexPath.row);
}
}
}
}
- 3 回答
- 0 關(guān)注
- 953 瀏覽
添加回答
舉報(bào)