【问题标题】:TableView containing two array of informations包含两个信息数组的 TableView
【发布时间】:2017-10-13 21:37:31
【问题描述】:

我有 Employee.swift,其中包含以下代码:

import Foundation

struct Employee {
    var name: String
    var favoriteLinks: [String]
    var links: [String]

    init(name: String, favoriteLinks: [String], links: [String]) {
        self.name = name
        self.favoriteLinks = favoriteLinks
        self.links = links
    }
}

我有 ViewController.swift,它使用 TableView 和以下代码:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var lists: [Employee] = [People(name: "Employee 1",
                                  favoriteLinks: ["Facebook","Twitter"], 
                                  links: ["www.facebook.com","www.twitter.com"])
                            ]

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
        tableView.delegate = self
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return lists.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        cell.textLabel?.text = lists[indexPath.row].name
        return cell
    }


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "showLinks" {
        if let indexPath = self.tableView.indexPathForSelectedRow {
            let destination = segue.destination as? SecondTableViewController
            destination?.talks = lists[indexPath.row].talk
            destination?.links = lists[indexPath.row].link

        }
    }
}

}

另外一个 TableViewController 包含以下代码:

import UIKit

class SecondTableViewController: UITableViewController {

var favoriteLinks: [String] = []
var links: [String] = []

override func viewDidLoad() {
    super.viewDidLoad()

}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return favoriteLinks.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    cell.textLabel?.text = favoriteLinks[indexPath.row]

    return cell
}
 }

我创建了一个属性,其中包含员工的姓名以及他最喜欢的链接和链接的列表。 ViewController 包含一个应该只包含员工姓名的 tableview,如果单击员工,您将被重定向到另一个 tableview,其中包含他的 favoriteLists 列表。

这就是问题所在。因为 tableview 只显示文本而不是链接。我希望文本也包含链接,如果单击该链接,则会将您定向到连接的链接。例如,如果单击 Facebook,它将显示我到 www.facebook.com。实现这一目标的最佳方法是什么?

我尝试创建两个单独的数组来包含信息,但我不知道如何调用包含链接的数组。任何帮助,将不胜感激。谢谢!

【问题讨论】:

    标签: ios swift uitableview tableview


    【解决方案1】:

    您必须在SecondTableViewController 中添加didSelectRowAt,如下所示:

    如果您的lists 如下所示:

    var lists: [Employee] = [Employee(name: "Employee 1",
                                    favoriteLinks: ["Facebook","Twitter"],
                                    links: ["http://www.facebook.com","http://www.twitter.com"])
    ]
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        UIApplication.shared.open(URL(string: links[indexPath.row])!, options: [:])
    }
    

    如果您的lists 如下所示:

    var lists: [Employee] = [Employee(name: "Employee 1",
                                    favoriteLinks: ["Facebook","Twitter"],
                                    links: ["www.facebook.com","www.twitter.com"])
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        var urlStr: String = links[indexPath.row]
        if !urlStr.hasPrefix("http") && !urlStr.hasPrefix("https") {
            urlStr = "http://\(urlStr)"
        }
        UIApplication.shared.open(URL(string: urlStr)!, options: [:])
    }
    

    【讨论】:

      【解决方案2】:

      更好的是,将整个 Employee 记录传递给您的第二个视图控制器:

      class ViewController: UITableViewController {
          // Add the https protocol for the link
          var lists: [Employee] = [
              Employee(name: "Employee 1",
                      favoriteLinks: ["Facebook","Twitter"],
                      links: ["https://www.facebook.com","https://www.twitter.com"])
          ]
      
          ...
      
          func prepare(for segue: UIStoryboardSegue, sender: Any?) {
              if segue.identifier == "showLinks" {
                  if let indexPath = self.tableView.indexPathForSelectedRow {
                      let destination = segue.destination as! SecondTableViewController
                      // Pass the whole employee record to the second view controller
                      destination.employee = lists[indexPath.row]
                  }
              }
          }
      }
      

      还有第二个视图控制器:

      class SecondTableViewController: UITableViewController {
          var employee: Employee!
      
          override func viewDidLoad() {
              super.viewDidLoad()
          }
      
          // MARK: - Table view data source
          override func numberOfSections(in tableView: UITableView) -> Int {
              return 1
          }
      
          override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
              return employee.favoriteLinks.count
          }
      
          override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
              // Select the prototype cell in IB and change its style to Subtitle if you want a 2-line label
              // Alternatively you can hide the link altogether, it doesn't affect the outcome
              let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
      
              // For the first line, we display the name of the link
              cell.textLabel?.text = employee.favoriteLinks[indexPath.row]
      
              // For the second line, we display the address of the link
              cell.detailTextLabel?.text = employee.links[indexPath.row]
              cell.detailTextLabel?.textColor = .blue
              return cell
          }
      
          override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
              let href = employee.links[indexPath.row]
              let url = URL(string: href)!
      
              // Open the URL in whatever app that registered to open it. That could be
              // the Facebook app or the Twitter app instead of Safari. After it has been
              // opened, de-highlight the cell
              UIApplication.shared.open(url, options: [:], completionHandler: { _ in tableView.selectRow(at: nil, animated: false, scrollPosition: .none) })
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-21
        • 1970-01-01
        • 2021-03-16
        • 2021-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多