Home > OS >  UITableView creating an iPhone settings screen with Swift
UITableView creating an iPhone settings screen with Swift

Time:12-04


I am trying to create UITableview with cells, identical to the iPhone settings screenshot.
It is part of my homework so i have to do it all in UITableview.
this is what I did with my code, but everything is red and full with errors. I tried to do it following the samples from lessons but it kinda looks all wrong.
Please, help me understand how this thing works and what is wrong.

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)
    }
}

Thank you!

CodePudding user response:

You use the wrong property name :

func numberOfSections(in tableView: UITableView) -> Int {
    // titles is not defined 
    return titles.count
}

You must use :

func numberOfSections(in tableView: UITableView) -> Int {
    return lines.count
}

Same for the other methods. In cellForRow :

let name = titles[indexPath.section].title[indexPath.row]
Let image = titles[indexPath.section].image[indexPath.row]

Should be replaced by :

let name = lines[indexPath.section].title[indexPath.row]

For viewFirHeader you use a cell which is usually not what is done. You have no title for each lines array. You may have to think again what you want to use. The way you organise your data may also be rethink as you have 2 different array for images and titles.

  • Related