【问题标题】:Clicking Event in UITableViewUITableView 中的点击事件
【发布时间】:2012-09-17 15:42:01
【问题描述】:

我有一个按钮和表格。现在我想以这样的方式单击,每当我选择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"]]; 
}

【问题讨论】:

标签: iphone objective-c ios uitableview tableview


【解决方案1】:

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

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

【讨论】:

  • k 但要选择一行并按下按钮我需要使用哪个东西
  • 如果你想要选择表格行时的动作,哪个更好,那么你可以检查didiSelectRowAtIndexPath func中的单元格标签并做你想做的事。但如果你必须使用选择和按钮同时按下,然后同时使用。
【解决方案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"]]; 

}

【讨论】:

  • 谢谢你!但每当我选择一行并单击我单独给出的按钮时,我都想要。
  • 对不起,我不明白。您是说如果单击按钮和行,您希望能够做同样的事情吗?或者你想做什么?
  • thanx.I got it..请检查我是否使用了正确的代码来标记每一行。
【解决方案3】:

使用UITableView的数据源函数

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

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

【讨论】:

  • 谢谢妈妈!但是每当我选择一行并单击我单独给出的按钮时,我都想要
  • 当你选择一行时,你想让按钮也被选中吗?我说的对吗?
【解决方案4】:
//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"]]; 
        }

【讨论】:

    【解决方案5】:

    全局声明一个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"]];
        }
    }
    

    【讨论】:

    • 好的,谢谢!如果想对每一行进行相同的操作..我需要使用索引 path.section 吗?下载什么?
    • 而您只想调用 [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"google.com"]]; 在选择任何行后单击按钮?
    • 如果你想对每一行都这样做,那么简单地想你可以这样做 -(IBAction)doDownload:(id)sender 方法......因为无论点击哪一行你只需要点击按钮做同样的事情......没有必要进行任何比较
    【解决方案6】:

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

    【讨论】:

      【解决方案7】:

      如果您想从侧面菜单推送视图。顶部是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
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-17
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        • 1970-01-01
        • 2017-04-27
        • 1970-01-01
        相关资源
        最近更新 更多