2012-02-23 106 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!"); 
     } 
    } 
    } 
相關問題