Post

Replies

Boosts

Views

Activity

UITableView programmatically
struct SettingsList { var image: UIImage! var settingsName: String var settingsButton: String } class SettingsTableViewCell: UITableViewCell { static let reuseID = "note_cell" private lazy var settingImageView: UIImageView = { let view = UIImageView() return view }() private lazy var settingTitleLabel: UILabel = { let view = UILabel() view.font = UIFont.preferredFont(forTextStyle: .body) view.textColor = UIColor.label return view }() var button: UIButton = { let view = UIButton(type: .system) view.setImage(UIImage(named: "Chevron"), for: .normal) view.setTitle("Русский", for: .normal) view.setTitleColor(.secondaryLabel, for: .normal) view.tintColor = .black view.semanticContentAttribute = .forceRightToLeft view.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -15) return view }() var switchButton: UISwitch = { let view = UISwitch() return view }() override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) contentView.backgroundColor = .secondarySystemBackground contentView.addSubview(settingImageView) contentView.addSubview(settingTitleLabel) contentView.addSubview(button) contentView.addSubview(switchButton) setupLayout() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func prepareForReuse() { super.prepareForReuse() settingImageView.image = nil settingTitleLabel.text = nil } func setup(title: String) { settingTitleLabel.text = title } func setup(image: UIImage) { settingImageView.image = image } private func setupLayout() { settingImageView.snp.makeConstraints { make in make.centerY.equalTo(contentView) make.leading.equalTo(contentView).offset(16) make.width.height.equalTo(24) } settingTitleLabel.snp.makeConstraints { make in make.centerY.equalTo(contentView) make.leading.equalTo(settingImageView.snp.trailing).offset(13) } button.snp.makeConstraints { make in make.centerY.equalTo(contentView) make.trailing.equalTo(contentView).offset(-25) } switchButton.snp.makeConstraints { make in make.centerY.equalTo(contentView) make.trailing.equalTo(contentView).offset(-25) } } } import UIKit class SettingsVC: UIViewController { private let settingsTableView = UITableView() let settingsList: [SettingsList] = [ SettingsList(image: UIImage(named: "language"), settingsName: "Язык", settingsButton: "chevron.right"), SettingsList(image: UIImage(named: "moon"), settingsName: "Темная тема", settingsButton: "switch.2"), SettingsList(image: UIImage(named: "trash"), settingsName: "Очистить данные", settingsButton: "") ] override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .systemBackground setupUI() } private func setupUI(){ setupNavigationItem() settingsTableView.register(SettingsTableViewCell.self, forCellReuseIdentifier: SettingsTableViewCell.reuseID) } private func setupNavigationItem() { navigationItem.title = "Settings" let image = UIImage(named: "settings") let resizedImage = image?.resized(to: CGSize(width: 25, height: 25)) let rightBarButtonItem = UIBarButtonItem(image: resizedImage, style: .plain, target: self, action: #selector(settingsButtonTapped)) rightBarButtonItem.tintColor = .black navigationItem.rightBarButtonItem = rightBarButtonItem } @objc func settingsButtonTapped(_ sender: UIButton){ } } extension SettingsVC: UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { settingsList.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: SettingsTableViewCell.reuseID, for: indexPath) as! SettingsTableViewCell print("Configuring cell for row \(indexPath.row)") if indexPath.row == 0 { cell.contentView.addSubview(cell.button) } else { cell.button.removeFromSuperview() } if indexPath.row == 1 { cell.contentView.addSubview(cell.switchButton) } else { cell.switchButton.removeFromSuperview() } cell.setup(title: settingsList[indexPath.row].settingsName) cell.setup(image: settingsList[indexPath.row].image) return cell } } It doesn't work when I click on this page(Settings.VC), It gives an error Thread 1: breakpoint 1.1 4.1 (1) and I don't know how to fix this;((( What I want to develop is this one:
0
0
250
Mar ’24