this code is SettingTableViewCell
enter picture description right here
class SettingTableViewCell: UITableViewCell {
static let identifier = "SettingTableViewCell"
non-public let iconContainer = UIView().then {
$0.clipsToBounds = true
$0.layer.cornerRadius = 10
$0.layer.masksToBounds = true
}
non-public let iconImageView = UIImageView().then {
$0.tintColor = .white
$0.contentMode = .scaleAspectFit
}
non-public let label = UILabel().then {
$0.numberOfLines = 1
}
override init(fashion: UITableViewCell.CellStyle, reuseIdentifier: String?) {
tremendous.init(fashion: fashion, reuseIdentifier: reuseIdentifier)
[label, iconImageView, iconContainer].forEach { contentView.addSubview($0) }
contentView.clipsToBounds = true
accessoryType = .disclosureIndicator
}
required init?(coder: NSCoder) {
fatalError()
}
override func layoutSubviews() {
tremendous.layoutSubviews()
let dimension: CGFloat = contentView.body.dimension.top - 12
iconContainer.body = CGRect(x: 10, y: 6, width: dimension, top: dimension)
let imageSize: CGFloat = dimension/1.5
iconImageView.body = CGRect(x: (size-imageSize)/2, y: (size-imageSize)/2, width: imageSize, top: imageSize)
iconImageView.heart = iconContainer.heart
label.body = CGRect(x: 20 + iconContainer.body.dimension.width, y: 0, width: contentView.body.dimension.width - 20 - iconContainer.body.dimension.width, top: contentView.body.dimension.top)
}
override func prepareForReuse() {
tremendous.prepareForReuse()
iconImageView.picture = nil
label.textual content = nil
iconContainer.backgroundColor = nil
}
public func configure(with mannequin: SettingOption) {
label.textual content = mannequin.title
iconImageView.picture = mannequin.icon
iconContainer.backgroundColor = mannequin.iconBackgroundColor
}
}
this code is SettingViewController
import UIKit
struct Part {
let title: String
let choices: [SettingsOptionType]
}
enum SettingsOptionType {
case staticCell(mannequin: SettingOption)
case switchCell(mannequin: SettingsSwitchOption)
}
struct SettingsSwitchOption {
let title: String
let icon: UIImage?
let iconBackgroundColor: UIColor
let handler: (() -> Void)
var isOn: Bool
}
struct SettingOption {
let title: String
let icon: UIImage?
let iconBackgroundColor: UIColor
let handler: (() -> Void)
}
ultimate class SettingViewController: UIViewController {
non-public lazy var presenter = SettingPresenter(viewController: self)
non-public let tableView: UITableView = {
let desk = UITableView(body: .zero, fashion: .grouped)
desk.register(SettingTableViewCell.self, forCellReuseIdentifier: SettingTableViewCell.identifier)
desk.register(SwitchTableViewCell.self, forCellReuseIdentifier: SwitchTableViewCell.identifier)
return desk
}()
var fashions = [Section]()
override func viewDidLoad() {
tremendous.viewDidLoad()
configure()
view.addSubview(tableView)
tableView.delegate = self
tableView.dataSource = self
tableView.body = view.bounds
}
func configure() {
//스위치 관련
// fashions.append(Part(title: "", choices: [
// .switchCell(model: SettingsSwitchOption(title: "Airplane Mode", icon: UIImage(systemName: "airplane"), iconBackgroundColor: .systemRed, handler: {
//
// }, isOn: true))
// ]))
// fashions.append(Part(title: "개인정보", choices: [
// .staticCell(model: SettingOption(title: "gmail", icon: UIImage(systemName: ""), iconBackgroundColor: .green, handler: {
// print("asdfasdf")
// })),
// .staticCell(model: SettingOption(title: "아이디 정보", icon: UIImage(systemName: ""), iconBackgroundColor: .green, handler: {
// print("sdfgsdfgsdfg")
// }))
// ]))
fashions.append(Part(title: "개인정보", choices: [
.staticCell(model: SettingOption(title: "gmail 정보", icon: UIImage(systemName: ""), iconBackgroundColor: .gray) {
let idVC = IdInformationViewController()
self.navigationController?.pushViewController(idVC, animated: true)
print("클릭함")
}),
.staticCell(model: SettingOption(title: "아이디 정보", icon: UIImage(systemName: ""), iconBackgroundColor: .gray) {
print("하하")
})
]))
//
fashions.append(Part(title: "보안", choices: [
.staticCell(model: SettingOption(title: "앱 비밀번호", icon: UIImage(systemName: ""), iconBackgroundColor: .gray) {
}),
.staticCell(model: SettingOption(title: "침입 시도", icon: UIImage(systemName: ""), iconBackgroundColor: .gray) {
}),
.staticCell(model: SettingOption(title: "앱 열기 추적", icon: UIImage(systemName: ""), iconBackgroundColor: .gray) {
print("asdfasdf r")
})
]))
fashions.append(Part(title: "앱", choices: [
.staticCell(model: SettingOption(title: "앱 아이콘 변경", icon: UIImage(systemName: ""), iconBackgroundColor: .gray) {
}),
.staticCell(model: SettingOption(title: "앱 테마 변경", icon: UIImage(systemName: "bluetooth"), iconBackgroundColor: .gray) {
})
]))
fashions.append(Part(title: "", choices: [
.staticCell(model: SettingOption(title: "지원", icon: UIImage(systemName: ""), iconBackgroundColor: .gray) {
}),
.staticCell(model: SettingOption(title: "버그 보고하기", icon: UIImage(systemName: ""), iconBackgroundColor: .gray) {
})
]))
fashions.append(Part(title: "", choices: [
.staticCell(model: SettingOption(title: "피드백", icon: UIImage(systemName: ""), iconBackgroundColor: .gray) {
})
]))
fashions.append(Part(title: "", choices: [
.staticCell(model: SettingOption(title: "앱 공유", icon: UIImage(systemName: ""), iconBackgroundColor: .gray) {
}),
.staticCell(model: SettingOption(title: "개인정보 처리 방침", icon: UIImage(systemName: ""), iconBackgroundColor: .gray) {
}),
.staticCell(model: SettingOption(title: "이용 약관", icon: UIImage(systemName: ""), iconBackgroundColor: .gray) {
})
]))
fashions.append(Part(title: "", choices: [
.staticCell(model: SettingOption(title: "사용 방법", icon: UIImage(systemName: ""), iconBackgroundColor: .gray) {
}),
.staticCell(model: SettingOption(title: "개발자 정보", icon: UIImage(systemName: ""), iconBackgroundColor: .gray) {
})
]))
}
}
extension SettingViewController: SettingProtocol {
}
extension SettingViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, titleForHeaderInSection part: Int) -> String? {
let part = fashions[section]
return part.title
}
func numberOfSections(in tableView: UITableView) -> Int {
print("numberOfSections (fashions.depend)")
return fashions.depend
}
func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
print(" numberOfRowsInSection (fashions[section].choices.depend)")
return fashions[section].choices.depend
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let mannequin = fashions[indexPath.section].choices[indexPath.row]
print("mannequin: (mannequin)")
swap mannequin.self {
case .staticCell(let mannequin):
guard let cell = tableView.dequeueReusableCell(
withIdentifier: SettingTableViewCell.identifier,
for: indexPath) as? SettingTableViewCell else {
return UITableViewCell()
}
cell.configure(with: mannequin)
return cell
case .switchCell(let mannequin):
guard let cell = tableView.dequeueReusableCell(
withIdentifier: SwitchTableViewCell.identifier,
for: indexPath) as? SwitchTableViewCell else {
return UITableViewCell()
}
cell.configure(with: mannequin)
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let sort = fashions[indexPath.row].choices[indexPath.row]
swap sort.self {
case .staticCell(let mannequin):
mannequin.handler()
case .switchCell(let mannequin):
mannequin.handler()
}
}
}
enter picture description right here
enter picture description right here
enter picture description right here
this code is SwitchTableViewCell
import UIKit
import Then
class SwitchTableViewCell: UITableViewCell {
static let identifier = "SwitchTableViewCell"
non-public let iconContainer = UIView().then {
$0.clipsToBounds = true
$0.layer.cornerRadius = 10
$0.layer.masksToBounds = true
}
non-public let iconImageView = UIImageView().then {
$0.tintColor = .white
$0.contentMode = .scaleAspectFit
}
non-public let label = UILabel().then {
$0.numberOfLines = 1
}
non-public let mySwitch = UISwitch().then { $0.onTintColor = .systemBlue }
override init(fashion: UITableViewCell.CellStyle, reuseIdentifier: String?) {
tremendous.init(fashion: fashion, reuseIdentifier: reuseIdentifier)
[label, iconContainer, mySwitch].forEach { contentView.addSubview($0) }
iconContainer.addSubview(iconImageView)
contentView.clipsToBounds = true
accessoryType = .none
}
required init?(coder: NSCoder) {
fatalError()
}
override func layoutSubviews() {
tremendous.layoutSubviews()
let dimension: CGFloat = contentView.body.dimension.top - 12
iconContainer.body = CGRect(x: 10, y: 6, width: dimension, top: dimension)
let imageSize: CGFloat = dimension/1.5
iconImageView.body = CGRect(x: (size-imageSize)/2, y: (size-imageSize)/2, width: imageSize, top: imageSize)
iconImageView.heart = iconContainer.heart
mySwitch.sizeToFit()
mySwitch.body = CGRect(x: contentView.body.dimension.width - mySwitch.body.dimension.width - 10, y: (contentView.body.dimension.top - mySwitch.body.dimension.top)/2, width: mySwitch.body.dimension.width, top: mySwitch.body.dimension.top)
label.body = CGRect(x: 20 + iconContainer.body.dimension.width, y: 0, width: contentView.body.dimension.width - 20 - iconContainer.body.dimension.width, top: contentView.body.dimension.top)
}
override func prepareForReuse() {
tremendous.prepareForReuse()
iconImageView.picture = nil
label.textual content = nil
iconContainer.backgroundColor = nil
mySwitch.isOn = false
}
public func configure(with mannequin: SettingsSwitchOption) {
label.textual content = mannequin.title
iconImageView.picture = mannequin.icon
iconContainer.backgroundColor = mannequin.iconBackgroundColor
mySwitch.isOn = mannequin.isOn
}
}
enter picture description right here
I am establishing iOS, however I do not know if it is as a result of tableView overlaps, but when indexPath is simply as much as 1, and indexPath.row is 0, the 0th of all cells is identical as the primary declared end result. Please assist me.
To provide you slightly bit extra element, the operate configure routinely offers every mannequin an interval when the mannequin is available in, so it suits the cell worth index.
enter picture description right here
enter picture description right here
I need the index of all cells to be completely different, so if I put the worth within the header and click on on it, the occasions within the header shall be executed. Assist me!