我正在嘗試使用單元格創建 UITableview,與 iPhone 設定螢屏截圖相同。
這是我作業的一部分,所以我必須在 UITableview 中完成所有作業。
這就是我對我的代碼所做的,但一切都是紅色的并且充滿了錯誤。我嘗試按照課程中的示例進行操作,但看起來有點不對。
請幫助我了解這件事是如何作業的以及出了什么問題。
import UIKit
struct Lines{
var image: [UIImage] = []
var title: [String] = []
}
class Titles {
static func titles() -> [Lines]{
return [
Lines(image: UIImage[ systemName: "airplane" ,"wifi.square.fill", "bitcoinsign.circle.fill", "iphone.homebutton.radiowaves.left.and.right", "personalhotpot" ], title: ["Авиарежим" , "Wi-fi", "Bluetooth", "Сотовая связь", "Режим модема"]),
Lines(image: UIImage[ systemName: "bell.badge.fill" ,"speaker.wave.3.fill", "moon.fill", "iphone.homebutton.radiowaves.left.and.right", "clock.fill" ], title: ["Уведомления", "Звуки,тактильные сигналы", "Не беспокоить", "Экранное время"]),
Lines(image: UIImage[ systemName: "gear" ,"switch.2", "display" ] , title: ["Общие", " Control Centre", "Экран и яркость"])
]
}
}
class SecondTableViewController: UITableViewController {
var lines = Titles.titles()
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension SecondTableViewController: UITableViewDataSource, UITableViewDelegate{
func numberOfSections(in tableView: UITableView) -> Int {
return titles.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titles[section].title.count
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "SectionCell") as! TableViewCell
let title = titles[section]
cell.image = Lines.image
cell.titleLabel.text = Lines.title
return cell
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SecondTableViewCell") as! TableViewCell
let name = titles[indexPath.section].title[indexPath.row]
cell.image = Lines.image
cell.titleLabel.text = Lines.title
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
}
謝謝!
uj5u.com熱心網友回復:
您使用了錯誤的屬性名稱:
func numberOfSections(in tableView: UITableView) -> Int {
// titles is not defined
return titles.count
}
您必須使用:
func numberOfSections(in tableView: UITableView) -> Int {
return lines.count
}
其他方法也一樣。在 cellForRow 中:
let name = titles[indexPath.section].title[indexPath.row]
Let image = titles[indexPath.section].image[indexPath.row]
應替換為:
let name = lines[indexPath.section].title[indexPath.row]
對于 viewFirHeader,您使用的單元格通常不是已完成的單元格。每個行陣列都沒有標題。您可能需要重新考慮要使用什么。您組織資料的方式也可能會重新考慮,因為您有 2 個不同的影像和標題陣列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/375671.html
上一篇:SWIFT/UIKit頁腳文本顯示在動態tableViewCell上
下一篇:需要使用字典列印輸出
