以编程方式'触摸'UITableViewCell?

时间:2012-02-23 20:47:36

标签: iphone objective-c ipad uitableview

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

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

但是,它只能直观地选择单元格,并且不会在didSelectRowAtIndexPath中选择该选择,除非您实际点击该单元格。

任何想法,有没有人经历过这个或有想法绕过这个或以不同的方式处理它?<​​/ p>

3 个答案:

答案 0 :(得分:9)

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

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

答案 1 :(得分:3)

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

无论你在didSelectRow方法中拥有什么,你不能把它放在一个独立的方法中,只需要调用它吗?

答案 2 :(得分:0)

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

  • touchesBegan :( NSSet *)触及withEvent:(UIEvent *)事件
  • touchesMoved :( NSSet *)触及withEvent:(UIEvent *)事件
  • touchesEnded:(NSSet *)触及withEvent:(UIEvent *)事件
  • touchesCancelled:(NSSet *)触及withEvent:(UIEvent *)事件

对于每个活动,你都有一个NSSET的触摸,你可以找到你的UitableViewCell 例如:

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