2013-07-04 28 views
0

我有單元格內的UIButton tableview。獲取tableview單元格內的按鈕來運行相同的didSelectCell

我在didSelectRowAtIndexPath中創建了一些功能。我想要的是當用戶點擊按鈕時也可以運行相同的功能/方法。

如何使buttonPressed方法運行didSelectRowAtIndexPath?

如果我不能做到這一點,我可以將功能移到一個新的方法,並調用這個新的方法。但是,如何從按鈕按下方法獲取單元格indexPath?

回答

0
-(IBAction)playButtonPressed:(id)sender { 
    NSLog(@"button pressed"); 
    UIButton *button = (UIButton *)sender; 
    UITableViewCell *cell = (UITableViewCell *)button.superview.superview; 
    UITableView *curTableView = (UITableView *)cell.superview; 
    NSIndexPath *indexPath = [curTableView indexPathForCell:cell]; 

    [self didSelectRowOrButtonAtIndexPath:indexPath]; 
} 

我做了一個新的方法didSelectRowOrButtonAtIndexPath,並呼籲這無論從buttonPressed和didSelectRowAtIndexPath方法,並在indexpath掠過我的方法中使用。

0
  1. 找到包含該按鈕的UITableViewCell對象(必須是其容器視圖之一,只需沿着超視圖鏈)。
  2. [UITableView indexPathForCell:]查找indexPath。
0

在你cellForRowAtIndexPath,分配indexPath.row你的按鈕的tag(假設你只需要該行 - 的想法是將indexPath附以某種方式按鈕,tag只是其中的一個辦法做到這一點)。

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { 

    // Init/Reuse the cell ... 

    cell.button.tag = indexPath.row; 

    return cell; 
} 

-(void)buttonPressed:(UIButton*)button { 
    [self method:button.tag]; 
} 
相關問題