2012-02-23 124 views
1

我需要以编程方式选择一个UITableViewCell,我已经试过这样:以编程方式'触摸'UITableViewCell?

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

然而,仅在视觉上选择该小区并没有挑上所选取的didSelectRowAtIndexPath除非你真正挖掘细胞。

任何想法,有没有人遇到过这个问题,或有想法来解决这个问题或采用不同的方法?

谢谢。

+1

的可能重复的[选择的tableview行编程(http://stackoverflow.com/questions/2035061/select-tableview-row-programmatically) – 2012-02-23 20:55:25

回答

3

想拿它按下,我猜你只是想运行didSelectRow的代码?

无论你在didSelectRow方法中有什么,你不能把它放在它自己的独立方法中,只是调用它?

+1

当然有那么一天!工作感谢。 – 2012-02-23 21:05:04

9

在向selectRowAtIndexPath发送消息后,为什么不发送消息到didSelectRowAtIndexPath?该代码应该是这样的:

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

对不起,诸如? – 2012-02-23 20:55:09

+1

你已经知道你想选择的索引路径,所以试试这个:[table.delegate tableView:table didSelectRowAtIndexPath:selectedCellIndexPath]; – 2012-02-23 20:56:03

0

如果您继承了UITableView,那么可以自定义每个TableViewCells上发生的UIEvent。 你的控制器会回应触摸事件:

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

对于每一个事件你有接触的NSSet中,你可以找到你UitableViewC ELL 例:

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