前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS18适配指南之UITableView

iOS18适配指南之UITableView

原创
作者头像
YungFan
发布2024-09-27 11:03:24
690
发布2024-09-27 11:03:24
举报
文章被收录于专栏:学海无涯

介绍

  • UITableView 中的所有视图均设置了UITraitCollection.listEnvironment特征。
  • 使用特征设置 UITableViewCell 样式时无需知道 UITableView 的具体样式。

案例

代码

代码语言:swift
复制
import UIKit

class ViewController: UIViewController {
    lazy var segmentedControl: UISegmentedControl = {
        let segmentedControl = UISegmentedControl(items: ["Plain", "Grouped"])
        segmentedControl.selectedSegmentIndex = 0
        segmentedControl.addTarget(self, action: #selector(segmentControlValueChanged), for: .valueChanged)
        return segmentedControl
    }()
    lazy var tableView: UITableView = {
        let tableView = UITableView(frame: UIScreen.main.bounds, style: .plain)
        tableView.dataSource = self
        return tableView
    }()
    let content = ["iPhone 4", "iPhone 4s", "iPhone 5", "iPhone 5s", "iPhone 6",
                   "iPhone 6 Plus", "iPhone 6s", "iPhone 6s Plus", "iPhone 7", "iPhone 7 Plus",
                   "iPhone 8", "iPhone 8 Plus", "iPhone X", "iPhone Xs", "iPhone XR",
                   "iPhone Xs Max", "iPhone 11", "iPhone 11 Pro", "iPhone 11 Pro Max",
                   "iPhone 12 mini", "iPhone 12", "iPhone 12 Pro", "iPhone 12 Pro Max",
                   "iPhone 13 mini", "iPhone 13", "iPhone 13 Pro", "iPhone 13 Pro Max",
                   "iPhone 14", "iPhone 14 Plus", "iPhone 14 Pro", "iPhone 14 Pro Max",
                   "iPhone 15", "iPhone 15 Plus", "iPhone 15 Pro", "iPhone 15 Pro Max",
                   "iPhone 16", "iPhone 16 Plus", "iPhone 16 Pro", "iPhone 16 Pro Max"]
    let detailContent = ["iPhone 4-iOS 4", "iPhone 4s-iOS 5", "iPhone 5-iOS 6", "iPhone 5s-iOS 7",
                         "iPhone 6-iOS 8", "iPhone 6 Plus-iOS 8", "iPhone 6s-iOS 9", "iPhone 6s Plus-iOS 9",
                         "iPhone 7-iOS 10", "iPhone 7 Plus-iOS 10", "iPhone 8-iOS 11", "iPhone 8 Plus-iOS 11",
                         "iPhone X-iOS 11", "iPhone Xs-iOS 12", "iPhone XR-iOS 12", "iPhone Xs Max-iOS 12",
                         "iPhone 11-iOS 13", "iPhone 11 Pro-iOS 13", "iPhone 11 Pro Max-iOS 13", "iPhone 12 mini-iOS 14",
                         "iPhone 12-iOS 14", "iPhone 12 Pro-iOS 14", "iPhone 12 Pro Max-iOS 14", "iPhone 13 mini-iOS 15",
                         "iPhone 13-iOS 15", "iPhone 13 Pro-iOS 15", "iPhone 13 Pro Max-iOS 15", "iPhone 14-iOS 16",
                         "iPhone 14 Plus-iOS 16", "iPhone 14 Pro-iOS 16", "iPhone 14 Pro Max-iOS 16",
                         "iPhone 15-iOS 17", "iPhone 15 Plus-iOS 17", "iPhone 15 Pro-iOS 17", "iPhone 15 Pro Max-iOS 17",
                         "iPhone 16-iOS 18", "iPhone 16 Plus-iOS 18", "iPhone 16 Pro-iOS 18", "iPhone 16 Pro Max-iOS 18"]

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.titleView = segmentedControl
        view.addSubview(tableView)
    }

    @objc func segmentControlValueChanged(_ sender: UISegmentedControl) {
        tableView.removeFromSuperview()
        if sender.selectedSegmentIndex == 0 {
            tableView = UITableView(frame: UIScreen.main.bounds, style: .plain)
        } else {
            tableView = UITableView(frame: UIScreen.main.bounds, style: .grouped)
        }
        tableView.dataSource = self
        view.addSubview(tableView)
        tableView.reloadData()
    }
}

// MARK: - UITableViewDataSource
extension ViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return content.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "abc") ?? UITableViewCell(style: .subtitle, reuseIdentifier: "abc")
        // iOS18新增,通过UITraitCollection.listEnvironment设置单元格样式
        cell.backgroundColor = cell.contentView.traitCollection.listEnvironment == .plain ? .green : .orange
        cell.textLabel?.font = cell.textLabel?.traitCollection.listEnvironment == .plain ? .systemFont(ofSize: 25) : .systemFont(ofSize: 21)
        cell.detailTextLabel?.textColor = cell.traitCollection.listEnvironment == .plain ? .red : .cyan
        cell.textLabel?.text = cell.traitCollection.listEnvironment == .plain ? content[indexPath.row] : detailContent[indexPath.row]
        cell.detailTextLabel?.text = cell.traitCollection.listEnvironment == .plain ? detailContent[indexPath.row] : content[indexPath.row]
        cell.imageView?.image = cell.traitCollection.listEnvironment == .plain ? UIImage(systemName: "iphone") : UIImage(systemName: "ipad")
        cell.accessoryType = cell.traitCollection.listEnvironment == .plain ? .disclosureIndicator : .checkmark
        return cell
    }
}

效果

UITableView.gif
UITableView.gif

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
  • 案例
    • 代码
      • 效果
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档