【问题标题】:Programmatically 'Touch' UITableViewCell?以编程方式“触摸”UITableViewCell?
【发布时间】:2012-02-23 20:47:35
【问题描述】:

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

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

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

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

【问题讨论】:

标签: iphone objective-c ipad uitableview


【解决方案1】:

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

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

【讨论】:

  • 你已经知道要选择的索引路径,所以试试这个:[table.delegate tableView:table didSelectRowAtIndexPath:selectedCellIndexPath];
【解决方案2】:

如果想要按下它,我猜你只是想在 didSelectRow 中运行代码?

无论你的 didSelectRow 方法中有什么,你不能把它放在一个单独的方法中然后调用它吗?

【讨论】:

    【解决方案3】:

    如果您将 UITableView 子类化,则可以自定义在每个 TableViewCells 上发生的 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!");
            }
        }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-26
      • 1970-01-01
      • 1970-01-01
      • 2014-07-17
      • 2011-01-01
      • 2016-07-06
      • 2011-11-14
      相关资源
      最近更新 更多