Ich habe einen UiTableViewController mit vier Abschnitten und einer Reihe von Zeilen in jedem erstellt. Ich habe auch eine Reihe von URLs implementiert. Ich bin neu in der Programmierung und dies ist eine Kombination aus zwei früheren TableViewControllern, aber das Problem ist, dass das URL-Array für jeden Abschnitt gilt. Wenn Sie in Zeile 1 auf Zeile 1 klicken, wird der erste Link geöffnet, aber beim Klicken auf Zeile 1 in Abschnitt 2 wird auch der erste Link geöffnet.URL Links ist UiTableView mit mehreren Abschnitten
Wie beschränke ich das URL-Array auf nur einen Abschnitt?
Ich verstehe, warum es nicht funktioniert, und habe viele Dinge ausprobiert, aber es ist noch nicht so weit.
struct Objects {
var sectionName : String!
var sectionObjects :[String]!
}
var objectsArray = [Objects]()
let urlArray1 = ["http://www.apple.co.uk","http://www.google.co.uk","https://www.dropbox.com/","tel://123456789",""]
override func viewDidLoad() {
super.viewDidLoad()
objectsArray = [Objects(sectionName: "Section 1", sectionObjects: ["one", "two", "three", "four","four A"]),
Objects(sectionName: "Section 2", sectionObjects: ["five", "six", "seven", "eight"]),
Objects(sectionName: "Section 3", sectionObjects: ["nine", "ten", "eleven", "twelve"]),
Objects(sectionName: "Section 4", sectionObjects: ["thirteen", "fourteen", "fifteen", "sixteen"])]
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as UITableViewCell!
cell?.textLabel?.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]
return cell!
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objectsArray[section].sectionObjects.count
}
override func numberOfSections(in tableView: UITableView) -> Int {
return objectsArray.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return objectsArray[section].sectionName
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let urlString = self.urlArray1[indexPath.row]
if let url = URL(string:urlString)
{
UIApplication.shared.open(url, options: [:])
}