长按(触摸并等待)tableView 中的单元格
Posted
技术标签:
【中文标题】长按(触摸并等待)tableView 中的单元格【英文标题】:Long touch (touch and wait) on the cells in tableView 【发布时间】:2014-08-25 16:11:44 【问题描述】:我有一个表格视图,当我触摸要导航到 editViewController 的单元格时,以及当我长按(触摸并等待)要导航到 DetailsViewController 的单元格时,我想这样做
【问题讨论】:
那么,有什么问题呢?您的帖子非常模棱两可,范围太广,无法得到任何答案。 【参考方案1】:1) 将UILongPressGestureRecognizer
添加到您的单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//add longPressGestureRecognizer to your cell
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];
//how long the press is for in seconds
lpgr.minimumPressDuration = 1.0; //seconds
[cell addGestureRecognizer:lpgr];
return cell;
2) 处理长按并推送给您editViewController
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
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
if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
NSLog(@"long press on table view at row %ld", (long)indexPath.row);
editViewController *editView = [self.storyboard instantiateViewControllerWithIdentifier:@"editView"]; //dont forget to set storyboard ID of you editViewController in storyboard
[self.navigationController pushViewController:editView animated:YES];
3) 正常按推送到您的DetailsViewController
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
DetailsViewController *detailView = [self.storyboard instantiateViewControllerWithIdentifier:@"detailView"]; //dont forget to set storyboard ID of you editViewController in storyboard
[self.navigationController pushViewController:detailView animated:YES];
【讨论】:
以上是关于长按(触摸并等待)tableView 中的单元格的主要内容,如果未能解决你的问题,请参考以下文章