2014-01-17 2 views
0

я есть UITable с UITableCells которые выглядят как:кнопка UITableCell touchUpInside с индексом ячейки кнопки WHOS была нажата

[MyLabel MyButton]

[MyLabel MyButton]

[MyLabel MyButton]

[MyLabel MyButton]

Я хотел бы мой handlerCellButtonClicked также получить ссылку на индекс ячейки. Как я могу получить ссылку на этот индекс, а также при нажатии кнопки?

/** called by table to get the cell needed by the index */ 
-(UITableViewCell*)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    ... 
    //init cell 
    ... 
    //configure the cells custom button handlers 
    [cell.myButton addTarget:self action:@selector(handlerCellButtonClicked) forControlEvents:UIControlEventTouchUpInside]; 

    return cell; 
} 

/** handler when the button in the cell is clicked */ 
- (void)handlerCellButtonClicked{ 
    NSLog(@"cell myButton clicked"); 
} 
+0

возможно дубликат [Детектирование который UIButton прессовали в UITableView] (http://stackoverflow.com/questions/1802707/detecting-which-uibutton-was-pressed-in-a-uitableview) –

ответ

2

Использовать UIButton Tag.

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

    //init cell 
    //configure the cells custom button handlers 

    cell.myButton.tag = indexPath.row; 
    [cell.myButton addTarget:self action:@selector(handlerCellButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 

    return cell; 
} 

/** handler when the button in the cell is clicked */ 
- (void)handlerCellButtonClicked:(UIButton *)sender 
    { 
    NSLog(@"%d cell myButton is clicked", sender.tag); 
    } 
+0

Как вы уверены, что селектор передаст отправителя? – hgwhittle

+0

(handlerCellButtonClicked :) должен передать отправителя цели, не так ли? –

+0

Я не уверен. Мне любопытно узнать, работает ли это. – hgwhittle

Смежные вопросы