【问题标题】:Make each item in the tabel view clickable to perform action - Swift使表格视图中的每个项目可单击以执行操作 - Swift
【发布时间】:2016-10-19 00:42:41
【问题描述】:

我对 ios 开发很陌生。我有一个包含 2 个部分的表格视图

表格视图:

import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{
var people = ["ABC","PQR","LMN"]
var languages = ["Android","Java","C++",".Net"]


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 2
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section==0
    {return people.count}
    else
    {return languages.count
    }
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
      let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
    if indexPath.section==0{
        cell.textLabel?.text = people[indexPath.row]
    }
    else{
        cell.textLabel?.text = languages[indexPath.row]

    }
    return cell
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if section==0{
        title="Names"
    }
    else{
        title="Languages"
    }
    return title
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
 }

我想让表格视图中的每个项目都可以点击。也就是说,当单击表视图中的项目时,将打开一个新的相应表视图。有人可以帮我吗?

【问题讨论】:

  • 您的意思是点击表格视图中的单元格进行导航吗?
  • 只需根据 indexPath 在didSelectRowAtIndexPath 函数中合并您需要的内容...
  • 您想要在点击Language 时显示语言列表。我说的对吗?
  • @NiravDoctorwala 是的,这就是我的意思
  • @JigarTarsariya 是的,非常正确

标签: ios xcode swift uitableview


【解决方案1】:

如果您的 didSelectRowAtIndexPath 部分中有 if/switch 语句,您所需要的一切。例如:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    if (indexPath.row == 0) {
        //do something
    }
    else if (indexPath.row == 0) {
        //do something else
    }
}

根据您在单击后想要一个新的表格视图的事实,我认为您应该执行一个 segue 来呈现一个新的视图控制器。见这里:How to segue programmatically using swift

【讨论】:

  • 谢谢。但是我在 if 语句中写了什么。比如我如何导航到新的表格视图?
  • 这就是我为您添加有关如何执行 segues 的链接的原因。您需要在情节提要中有其他表视图,给它们一个唯一标识符,然后在 if 语句中使用具有不同标识符的 performSegueWithIdentifier。
【解决方案2】:

你可以像这样在其他控制器上实现导航,

首先,您需要在视图中嵌入导航控制器,以便轻松导航。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
{
    if (indexPath.row == 0) 
    {

         let vc = self.storyboard?.instantiateViewControllerWithIdentifier("AddVC") as! addRecordViewController 
         self.navigationController?.pushViewController(vc, animated: true)
    }
    else
    {
       // Navigate on other view
    }
}

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-07
    • 1970-01-01
    相关资源
    最近更新 更多