Нажатие события в UITableView
У меня есть кнопка и таблица. Теперь я хочу щелкнуть таким образом, что всякий раз, когда я выбираю любую строку в tableview
и нажимаю кнопку, эта конкретная кнопка нажимает событие. Для этого, во-первых, я даю тег каждой строке i.e
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *LabelCellIdentifier = @"cell";
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:LabelCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:LabelCellIdentifier];
}
if (indexPath.row <= [_arrayMp3Link count]) {
cell.textLabel.text = [_arrayMp3Link objectAtIndex:indexPath.row];
}
// now tag each row
NSInteger count = 1;
for (NSInteger i = 0; i < indexPath.section; i++) {
count += [[tableView dataSource] tableView:tableView numberOfRowsInSection:i];
}
count += indexPath.row;
// dequeue, create and configure...
cell.tag = count;
return cell;
}
и теперь, когда я устанавливаю событие в кнопке, когда я выбираю строку и нажимаю кнопку. Но не получить правильные вещи.
(IBAction)doDownload:(id)sender {
// to select first row
if(cell.tag==1)
{
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://www.google.com"]];
}
Ответы
Ответ 1
Объявить переменную int
глобально -
int rowNo;
Затем присвойте ему значение в методе didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
rowNo = indexPath.row;
}
Теперь у вас есть indexNo. выбранной строки.
-(IBAction)doDownload:(id)sender
{
//Now compare this variable with 0 because first row index is 0.
if(rowNo == 0)
{
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://www.google.com"]];
}
}
Ответ 2
Вам нужно использовать метод didSelectRowAtIndexPath функции tableView.
в этом методе вы сохраняете выбранный тег строки или все, что хотите сохранить. И в действии кнопки проверьте значение сохраненного объекта и сделайте что-нибудь.
Ответ 3
Используйте функцию источника данных UITableView, которая
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{}
где indexPath.row - это каждый индекс строки.
Ответ 4
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==i)
{//Perform whatever action you would like for whichever row number..i'm just using i as an int placeholder
[[UIApplication sharedApplication]openURL:[NSURLWithString:@"http://www.google.com"]];
}
//Or you could perform the same action for all rows in a section
if(indexPath.section==i)
{
[[UIApplication sharedApplication]openURL:[NSURLURLWithString:@"http://www.google.com"]];
}
Ответ 5
'the-interview-2.jpg' - это имя файла изображения
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ViewController *vc = [sb instantiateViewControllerWithIdentifier:@"viewc"];
[self.navigationController pushViewController:vc animated:YES];
[vc setImageName:@"the-interview-2.jpg"];
}
Ответ 6
//use selectedrow to check the condition
-(void)tableView:(UITableView *)tableview didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedrow=indexPath.row;
}
-(IBAction)doDownload:(id)sender {
// to select first row
if(selectedrow==1)
{
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://www.google.com"]];
}