【问题标题】:UITableView creating an iPhone settings screen with SwiftUITableView 使用 Swift 创建 iPhone 设置屏幕
【发布时间】:2022-01-04 18:17:43
【问题描述】:


我正在尝试使用单元格创建 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)
    }
}

谢谢!

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    您使用了错误的属性名称:

    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 个不同的图像和标题数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-17
      • 2022-10-14
      • 2012-02-28
      • 1970-01-01
      • 2014-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多