UITableView中的点击事件

23

我有一个按钮和表格。现在我想以这样的方式进行单击,即每当我选择tableview中的任何行并按下按钮时,该特定按钮的按下事件都会发生。为此,首先我必须给每一行分配标记,即

- (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"]]; 
}

你可以在IBAction中获取行。查看我在这里的答案:http://stackoverflow.com/a/12594183/1144632 - danielbeard
7个回答

31

在全局范围内声明一个 int 变量 -

int rowNo;
然后在didSelectRowAtIndexPath:方法中给它赋值。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
     rowNo = indexPath.row;
 }

现在你获得了所选行的索引号。

-(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"]];
    }
}

好的,谢谢!如果想对每一行执行相同的操作,我需要使用indexPath.section吗?而在下载中是什么? - Christien
你想在选择任何行后,仅在按钮点击时调用[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://www.google.com"]];吗? - TheTiger
如果您想为每一行都这样做,那么可以简单地使用-(IBAction)doDownload:(id)sender方法...因为无论点击哪一行,您只需要在按钮点击时执行相同的操作...没有必要进行任何比较。 - TheTiger

4
你需要使用tableView的didSelectRowAtIndexPath方法。在这个方法中,你可以保存选中行的标签或其他你想要保存的内容。在按钮动作中,检查已保存实体的值并执行任何操作。

选择一行并按下按钮,我需要使用哪个东西? - Christien
如果您想在选择表格行时执行某些操作,最好检查didiSelectRowAtIndexPath函数中单元格的标记并执行所需操作。但是,如果必须同时使用选择和按钮按下,则两者都要使用。 - tausun

2
-(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"]]; 

}

谢谢,但我想要的是每当我选择一行并单击我单独提供的按钮时。 - Christien
抱歉,我不明白。您是说您想在单击按钮和行时执行相同的操作吗?还是您想做什么? - Tommy Devoy
谢谢。我明白了。请检查我是否使用了正确的代码来标记每一行。 - Christien

2
使用UITableView的数据源函数,即- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{},其中indexPath.row是每一行的索引。

谢谢,但我想实现的是每当我选择一行并单击我单独提供的按钮时。 - Christien
当你选择一行时,你也想选择按钮吗?我说的对吗? - KarenAnne

1
"

'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"]; 
}

0
如果你想从侧边菜单推出视图。顶部部分是设计,switch case语句运行良好,contact显示一个视图在顶部,其他的先打开导航控制器。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = items[indexPath.row]
    cell.textLabel?.textColor = .white
    cell.backgroundColor = .systemTeal
    cell.imageView?.tintColor = .white

    switch indexPath.row {
    case 0:
        cell.imageView?.image = UIImage(systemName: "info.circle")
        //presentViewController(AboutViewController, animated: true, completion: nil)
        //performSegue(withIdentifier: "toLanguageView", sender: nil)
    case 1:
        cell.imageView?.image = UIImage(systemName: "map")
    case 2:
        cell.imageView?.image = UIImage(systemName: "figure.walk")
    case 3:
        cell.imageView?.image = UIImage(systemName: "paperplane")
    default:
        break
    }

    return cell
    }

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    
    switch indexPath.row {
    case 0:
                    
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "AboutViewController") as! AboutViewController
        self.navigationController?.pushViewController(controller, animated: true)
                
    case 1:
        
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "DigMenuViewController") as! DigMenuViewController
        self.navigationController?.pushViewController(controller, animated: true)
        
    case 2:
        
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "VirtualTourViewController") as! VirtualTourViewController
        self.navigationController?.pushViewController(controller, animated: true)
        
    case 3:
        
        let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let newViewController = storyBoard.instantiateViewController(withIdentifier: "ContactViewController") as! ContactViewController
        self.present(newViewController, animated: true, completion: nil)
        
    default:
        break
    }
}

0
//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"]]; 
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接