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

7 回答 7

31

全局声明一个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"]];
    }
}
于 2012-09-26T05:23:35.700 回答
4

您必须使用 tableView 的函数 didSelectRowAtIndexPath 方法。

在该方法中,您保存选定的行标记或您想要保存的任何内容。并在按钮操作中检查已保存实体的值并执行任何操作。

于 2012-09-26T04:34:11.250 回答
2

使用 UITableView 的数据源功能,即

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{}

其中 indexPath.row 是每一行的索引。

于 2012-09-26T04:40:26.997 回答
1
-(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"]]; 

}
于 2012-09-26T04:40:04.907 回答
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"]; 
}
于 2015-02-02T13:41:58.957 回答
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"]]; 
        }
于 2012-09-26T04:48:35.413 回答
0

如果您想从侧面菜单推送视图。顶部是 switch case 语句运行良好的设计,联系人在顶部显示一个视图,其他人首先打开导航控制器。

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
    }
}
于 2021-10-01T21:17:51.697 回答