【发布时间】: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