以编程方式“触摸” UITableViewCell?

发布于 2025-01-08 20:17:31 字数 400 浏览 0 评论 0原文

我需要以编程方式选择一个 UITableViewCell,我已经尝试过:

NSIndexPath *selectedCellIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [table selectRowAtIndexPath:selectedCellIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone];

但是,它仅在视觉上选择单元格,并且不会在 didSelectRowAtIndexPath 中选择该选择,除非您实际点击该单元格。

任何想法,有没有人经历过这个或有想法来解决这个问题或以不同的方式处理它?

I need to programmatically select a UITableViewCell, I have tried this:

NSIndexPath *selectedCellIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [table selectRowAtIndexPath:selectedCellIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone];

However, it only selects the cell visually and doesn't pick up on that selection in didSelectRowAtIndexPath unless you actually tap the cell.

Any ideas, has anyone experienced this or have ideas to get around this or approach it differently?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终遇你 2025-01-15 20:17:31

为什么在向 selectRowAtIndexPath 发送消息后不向 didSelectRowAtIndexPath 发送消息?代码应该如下所示:

NSIndexPath *selectedCellIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[table selectRowAtIndexPath:selectedCellIndexPath 
                   animated:YES 
             scrollPosition:UITableViewScrollPositionNone];
[table.delegate tableView:table didSelectRowAtIndexPath:selectedCellIndexPath];

Why not send a message to didSelectRowAtIndexPath after you've sent a message to selectRowAtIndexPath? The code should look like this:

NSIndexPath *selectedCellIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[table selectRowAtIndexPath:selectedCellIndexPath 
                   animated:YES 
             scrollPosition:UITableViewScrollPositionNone];
[table.delegate tableView:table didSelectRowAtIndexPath:selectedCellIndexPath];
一紙繁鸢 2025-01-15 20:17:31

通过想要按下它,我猜您只想运行 didSelectRow 中的代码?

无论您的 didSelectRow 方法中有什么,您都不能将其放入其自己的单独方法中并直接调用它吗?

By wanting it pressed, I'm guessing you just want to run the code in didSelectRow?

Whatever you have in your didSelectRow method, can't you put it in a seperate method of its own and just call it?

何以畏孤独 2025-01-15 20:17:31

如果您对 UITableView 进行子类化,则可以自定义每个 TableViewCell 上发生的 UIEvent。
您的控制器将响应触摸事件:

  • TouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  • TouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  • TouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  • TouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

对于每个事件,您都有一个 NSSET 触摸,您可以找到您的 UitableViewCell
示例:

 - touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    for(UITouch *aTouch in touches){
        if(aTouch.view IsYourUiTableViewCell){
           NSLOG(@"TableViewCell press!");
        }
    }
  }

If you subclass your UITableView it's possible to customize UIEvent that occurs on each TableViewCells.
Your controller will respond to Touch Events:

  • touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  • touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  • touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  • touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

For each event you have a NSSET of touches and you can find your UitableViewCell
Exemple :

 - touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    for(UITouch *aTouch in touches){
        if(aTouch.view IsYourUiTableViewCell){
           NSLOG(@"TableViewCell press!");
        }
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文