我有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
}
}
我有使用TableView的ViewController.swift,代码如下:
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。
这就是问题所在。因为tableview只显示文本而不显示链接。我希望文本也包含链接,如果单击该链接将指向您连接的链接。例如,如果点击Facebook,它会显示给www.facebook.com。实现这一目标的最佳方法是什么?
我尝试创建两个单独的数组来包含信息,但我不知道如何调用包含该链接的数组。任何帮助,将不胜感激。谢谢!
答案 0 :(得分:1)
您必须在didSelectRowAt
中添加SecondTableViewController
,如下所示:
如果您的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: [:])
}
答案 1 :(得分:0)
更好的是,将整个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) })
}
}