2012-07-01 2 views
0

Я использовал следующий код для добавления кнопок в мой tableviewcell, когда нажата кнопка. Мне нужно знать, в какой строке она была. поэтому я отметил теги (playButton viewWithTag: indexPath.row) Проблема в том, что если я определяю метод целевого действия (воспроизведения), чтобы получить отправителя, он вылетает с «нераспознанным селектором», любой из них знает, по какой строке кнопка была нажата или почему это происходит сбой, как это БлагодаряДобавление кнопок в tableview - невозможно получить доступ к отправителю

-(void)configureCell: (UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { 
UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom] ; 
[playButton setFrame:CGRectMake(150,5,40,40)]; 
[playButton viewWithTag:indexPath.row] ; //Tagging the button 
[playButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal]; 
[playButton addTarget:self action:@selector(play) forControlEvents: UIControlEventTouchUpInside]; 
[cell addSubview:playButton]; 

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
UITableViewCell *beatCell = nil; 
beatCell = [tableView dequeueReusableCellWithIdentifier:@"beatCell"]; 
if (beatCell == nil){ 
    beatCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"beatCell"]; 
} 
[self configureCell:beatCell atIndexPath:indexPath]; 
return beatCell; 

}

-(void) play:(id) sender{ 
UIButton *play = sender; 
NSLog(@"Play %i" , play.tag); 

}

ответ

1

Вместо

[playButton viewWithTag:indexPath.row] ; 

Где вы пытаетесь получить подвид из UIButton (я не знаю, почему), вы должны установить метку с помощью сеттер метода:

[playButton setTag:indexPath.row]; 

Вы также должны отдать свой отправителя до UIButton type

-(void) play:(id) sender{ 
UIButton *play = (UIButton *)sender; 
NSLog(@"Play %i" , play.tag); 
} 
+0

Спасибо, вы добавили, было так им portant, поскольку я получал только нули, теперь он работает !!!! – Papito

+0

Как это сделать? – Papito

1

Изменить один символ этой строки:

[playButton addTarget:self action:@selector(play) forControlEvents: UIControlEventTouchUpInside]; 

в

[playButton addTarget:self action:@selector(play:) forControlEvents: UIControlEventTouchUpInside]; 

При включении параметров на селектор, двоеточие на самом деле имеет значение для цели выполнения C, чтобы иметь возможность найдите этот селектор объекта, на который настроен таргетинг.

+0

Спасибо, Майкл, это было очень полезно – Papito

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