首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

iOS 自定義 Cell 多控制器共用:透過 reuseIdentifier 區分配置

     編輯:程式設計綜合問答

核心做法:不同控制器註冊不同 reuseIdentifier

在 UITableView 註冊 Cell 時,使用**唯一識別字串**對應不同顯示邏輯;dequeueReusableCellWithIdentifier: 會依識別字串回傳對應樣式的 Cell。

實作範例

// 共用 Cell 類別
class MyCell: UITableViewCell {
    static let idControllerA = "MyCell_ControllerA"
    static let idControllerB = "MyCell_ControllerB"

    // 共用 UI 元件
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var detailLabel: UILabel!
    @IBOutlet weak var iconImageView: UIImageView!

    // 依 identifier 配置不同內容
    func configure(for identifier: String, data: Any) {
        switch identifier {
        case MyCell.idControllerA:
            // Controller A 風格
            titleLabel.font = .boldSystemFont(ofSize: 18)
            detailLabel.textColor = .gray
            iconImageView.image = UIImage(named: "icon_a")
            if let model = data as? ModelA { titleLabel.text = model.title }
        case MyCell.idControllerB:
            // Controller B 風格
            titleLabel.font = .systemFont(ofSize: 16)
            detailLabel.textColor = .blue
            iconImageView.image = UIImage(named: "icon_b")
            if let model = data as? ModelB { titleLabel.text = model.name }
        default: break
        }
    }
}

// Controller A
class ControllerA: UIViewController, UITableViewDataSource {
    let tableView = UITableView()
    let dataA = [ModelA(title: "A1"), ModelA(title: "A2")]

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(MyCell.self, forCellReuseIdentifier: MyCell.idControllerA)
        tableView.dataSource = self
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: MyCell.idControllerA, for: indexPath) as! MyCell
        cell.configure(for: MyCell.idControllerA, data: dataA[indexPath.row])
        return cell
    }
}

// Controller B
class ControllerB: UIViewController, UITableViewDataSource {
    let tableView = UITableView()
    let dataB = [ModelB(name: "B1"), ModelB(name: "B2")]

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(MyCell.self, forCellReuseIdentifier: MyCell.idControllerB)
        tableView.dataSource = self
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: MyCell.idControllerB, for: indexPath) as! MyCell
        cell.configure(for: MyCell.idControllerB, data: dataB[indexPath.row])
        return cell
    }
}

進階:Storyboard / XIB 方式

  1. 在 Storyboard 中拖入 Prototype Cell,設定 **Identifier** 為 MyCell_ControllerA。
  2. 同一 Cell 類別可關聯多個 XIB,分別命名 MyCell_A.xib、MyCell_B.xib,註冊時 tableView.register(UINib(nibName: "MyCell_A", bundle: nil), forCellReuseIdentifier: "MyCell_ControllerA")。

優勢

  • 零耦合:Cell 類別不知曉控制器,僅依 identifier 切換外觀。
  • 效能最佳:系統自動管理重用佇列,不同 identifier 獨立佇列,不會混用錯誤樣式。
  • 易擴充:新增控制器只需定義新 identifier 並在 configure 加入 case。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved