2013-10-23 54 views
0

我有一個自定義單元格。這樣如何在CustomCell按鈕中檢測indexPath.row單擊?

Cell

有按鈕,就像LIKE,評價,分享。

我想在indexPath.row方法中點擊這些按鈕。

我做了協議這樣

@protocol Cell_StreamCellDelegate 
@required 

-(void)like; 
-(void)comment; 
-(void)share; 


@end 

我打電話給他們時CELL.m方法,比如下面

-(IBAction)likePressed:(id)sender{ 
    [delegate like]; 
} 
-(IBAction)commentPressed:(id)sender{ 
    [delegate comment]; 
} 
-(IBAction)sharePressed:(id)sender{ 
    [delegate share]; 
} 

但我無法獲取indexPath.row。我想獲得一個特定索引的對象。

感謝

回答

0

聲明一個新的整數屬性作爲自定義單元格子類中的行,然後在cellForRowAtIndex方法中將其值指定爲indexpath.row。

customcell.row = indexpath.row. 

使用此行的協議

[delegate like:row] 
+0

我現在做了,在你的文章之前,讓我測試一下,如果它有效,我會感激 – Duaan

0

在您的委託方法,你可以發送調用的方法將細胞(即self

喜歡的東西:

- (void)commentForCell:(UITableViewCell *)cell; 

當你調用委託方法做到像

[delegate commentForCell:self]; 

然後當你執行th Ë委託方法

- (void)commentForCell:(UITableViewCell *)cell 
{ 
    NSIndexPath *path = [self.tableView indexPathForCell:cell]; 
    ... 
} 

這是很常見使用呼叫者的PARAM的委託方法,你可以在任何標準的SDK方案見(集合視圖,表視圖,警報視圖...)

+0

爲什麼downvote? :/ – Marc

0

試試這個簡單的一個,只是轉換touched points into the cell Index

CGPoint touchPoint = [sender convertPoint:CGPointZero toView:self.tableView]; 
NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:touchPoint]; 
相關問題