iOS26适配指南之@Observable Object

介绍

  • UIKit 支持@Observable类型。
  • 修饰的类型必须是类而不能是结构体。
  • 当其中的数据(属性值)发生更改时,相应的 UI 能够自动更新,而无需手动调用setNeedsLayout()setNeedsDisplay()layoutIfNeeded()等方法。
  • 需要将 UI 更新的代码放在 UIView 的layoutSubviews()或者 UIViewController 的viewWillLayoutSubviews()方法中。当@Observable中的数据发生变化时,layoutSubviews()viewWillLayoutSubviews()方法会自动调用。
  • 该功能可以支持到 iOS 18,但需要在 Info.plist 文件中增加字段UIObservationTrackingEnabled,并且将其值设置为YES

案例

swift 复制代码
import UIKit

// MARK: - PhoneModel
@Observable class PhoneModel {
    var name: String
    var osName: String

    init(name: String, osName: String) {
        self.name = name
        self.osName = osName
    }

    static func getPhone() -> [PhoneModel] {
        let phoneNames = ["iPhone 16", "iPhone 16 Plus", "iPhone 16 Pro", "iPhone 16 Pro Max"]
        let osNames = ["iOS 18", "iOS 18", "iOS 18", "iOS 18"]
        var phoneModels = [PhoneModel]()
        for i in 0 ..< phoneNames.count {
            let phoneModel = PhoneModel(name: phoneNames[i], osName: osNames[I])
            phoneModels.append(phoneModel)
        }
        return phoneModels
    }
}

// MARK: - UITableViewCell
class CustomTableViewCell: UITableViewCell {
    lazy var phoneLabel: UILabel = {
        let label = UILabel(frame: CGRect(x: UIScreen.main.bounds.midX - 180, y: 200, width: 360, height: 100))
        label.textColor = .white
        label.font = .systemFont(ofSize: 40, weight: .bold)
        label.textAlignment = .center
        return label
    }()

    lazy var osLabel: UILabel = {
        let label = UILabel(frame: CGRect(x: UIScreen.main.bounds.midX - 75, y: 400, width: 150, height: 50))
        label.textColor = .white
        label.font = .systemFont(ofSize: 25)
        label.textAlignment = .center
        return label
    }()

    var phoneModel: PhoneModel?

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        contentView.addSubview(phoneLabel)
        contentView.addSubview(osLabel)
        contentView.backgroundColor = .black
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        // UI更新
        phoneLabel.text = phoneModel?.name
        osLabel.text = phoneModel?.osName
    }
}

// MARK: - UIViewController
class ViewController: UIViewController {
    lazy var tableView: UITableView = {
        let tableView = UITableView(frame: UIScreen.main.bounds, style: .plain)
        tableView.dataSource = self
        tableView.rowHeight = UIScreen.main.bounds.height
        tableView.isPagingEnabled = true
        tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: "custom")
        return tableView
    }()

    let phoneModels = PhoneModel.getPhone()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(tableView)
        DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
            let phoneModel = self.phoneModels[0]
            phoneModel.name = "iPhone 17"
            phoneModel.osName = "iOS 26"
        }
    }
}

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "custom", for: indexPath) as! CustomTableViewCell
        cell.phoneModel = phoneModels[indexPath.row]
        return cell
    }
}

效果

相关推荐
Magnetic_h6 小时前
【iOS】方法与消息底层分析
笔记·学习·macos·ios·objective-c·cocoa
MaoJiu13 小时前
Flutter与原生端的通信
flutter·ios
iReaShare15 小时前
如何将 iPhone 备份到云端:完整指南
ios
小赵小赵福星高照~16 小时前
iOS UI视图面试相关
ui·ios·面试
杂雾无尘19 小时前
苹果高管揭示苹果背后秘密:苹果为何不涉足搜索引擎领域?
ios·apple
二流小码农19 小时前
鸿蒙开发:一键更新,让应用无需提交应用市场即可下载安装
android·ios·harmonyos
2501_915921431 天前
没有Mac如何完成iOS 上架:iOS App 上架App Store流程
android·ios·小程序·https·uni-app·iphone·webview
瓜子三百克2 天前
值类型大小与内存分配
swift·内存分配
初级代码游戏2 天前
Maui劝退:用windows直接真机调试iOS,无须和Mac配对
macos·ios·配置·maui·热重载
fundroid2 天前
Swift 进军 Android,Kotlin 该如何应对?
android·ios