2017-01-01 68 views
0

我試圖構建一個相當複雜的表。此表格的一個單元格導入另一個表格。在這個導入的表格中,我根據情況顯示不同的行。導入的表格和所有可導入的表格單元格被組織到自己的具有自己的控制器的nib文件中,用於每個單元格。作爲一名優秀的程序員,我試圖在整個項目中使用依賴注入。現在的問題是,當我使用viewDidLoad中()註冊碎粒uitableviewcell筆尖中的依賴注入

let cellNib = UINib(nibName: "BatchConsumptionCell", bundle: nil) 
tableView.register(cellNib, forCellReuseIdentifier: "BatchConsumptionCell") 

,然後把它們作爲dequeueReusableCell(withIdentifier的常用方法:在的tableView(爲的tableView :):cellForRowAt :)

let batchConsumptionCell = tableView.dequeueReusableCell(withIdentifier: "BatchConsumptionCell", for: indexPath) as! BatchConsumptionCell 
batchConsumptionCell.setConsumption(consumption: consumption) 
return batchConsumptionCell 

我無法及時注入依賴關係。

在BatchConsumptionCell中,我在tableView(tableView:cellForRowAt :)中工作正常。該函數在dequeueReusableCell(withIdentifier:for :)被執行後調用。但只要我試圖使tableView(tableView:numberOfRowsInSection :)動態我遇到問題。這個函數似乎在dequeueReusableCell(withIdentifier:for :)之前調用,所以在這一點上不會注入依賴關係。

我試圖覆蓋我的BatchConsumptionCell中的init(nibName:bundle :)初始值設定項,但這是一個UITableViewCell,所以我無法覆蓋它。

怎樣運用呢?當筆尖和其控制器初始化時,有什麼辦法可以注入依賴關係嗎?還是我整理我的細胞都錯了?任何想法將不勝感激。

因爲在這裏更清晰是我的代碼:

ConsumptionDetailViewController

import UIKit 

class ConsumptionDetailViewController: UITableViewController { 

// MARK: - Properties 

var moc: NSManagedObjectContext! 

var consumption: Consumption! 


// MARK: - Outlet Properties 

@IBOutlet weak var labelDate: UILabel! 
@IBOutlet weak var labelTime: UILabel! 
... 


// MARK: - Default Methods 

override func viewDidLoad() { 
    super.viewDidLoad() 

    let cellNib = UINib(nibName: "BatchConsumptionCell", bundle: nil) 
    tableView.register(cellNib, forCellReuseIdentifier: "BatchConsumptionCell") 

    updateTableFields(selectedConsumption: consumption) 
} 


// MARK: - UI Update Methods 

func updateTableFields(selectedConsumption: Consumption) { 

    labelId.text = selectedConsumption.wtId 
    ... 
} 

// MARK: - BatchAddEditDelegate Methods 

func didFinishEditing(consumption: Consumption) { 
    updateTableFields(selectedConsumption: consumption) 
} 

// MARK: - TableView Methods 

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    ... 
} 

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    ... 
} 

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { 
    ... 
} 

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    ... 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if indexPath.section == 1 && indexPath.row == 0 { 
     let batchConsumptionCell = tableView.dequeueReusableCell(withIdentifier: "BatchConsumptionCell", for: indexPath) as! BatchConsumptionCell 
     batchConsumptionCell.setConsumption(consumption: consumption) 
     return batchConsumptionCell 
    } 
    return super.tableView(tableView, cellForRowAt: indexPath) 
} 


// MARK: - Navigation 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    ... 
} 

} 

BatchConsumptionCell

import UIKit 

class BatchConsumptionCell: UITableViewCell, UITableViewDataSource, UITableViewDelegate { 

var consumption: Consumption! 
var batchConsumptionCount: Int! 

@IBOutlet weak var tableView: UITableView! 

override func awakeFromNib() { 
    super.awakeFromNib() 

    var cellNib = UINib(nibName: "BatchConsumptionBasicCell", bundle: nil) 
    tableView.register(cellNib, forCellReuseIdentifier: "BatchConsumptionBasicCell") 

    cellNib = UINib(nibName: "BatchConsumptionMultiCell", bundle: nil) 
    tableView.register(cellNib, forCellReuseIdentifier: "BatchConsumptionMultiCell") 

    self.tableView.delegate = self 
    self.tableView.dataSource = self 
} 

func setConsumption(consumption: Consumption) { 
    self.consumption = consumption 
    self.batchConsumptionCount = consumption.batchConsumptions?.count 
} 

func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if batchConsumptionCount == 1 { // <-- This does not work 
     return 3 
    } else { 
     return batchConsumptionCount 
    } 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if batchConsumptionCount == 1 { // <-- This works fine 
     let batchConsumptionBasicCell = tableView.dequeueReusableCell(withIdentifier: "BatchConsumptionBasicCell", for: indexPath) as! BatchConsumptionBasicCell 
     let bc = consumption.batchConsumptions?.allObjects[0] as! BatchConsumption 
     if indexPath.row == 0 { 
      batchConsumptionBasicCell.configure(title: "Batch", detail: (bc.batch?.wtId)!) 
     } else if indexPath.row == 1 { 
      batchConsumptionBasicCell.configure(title: "Weight", detail: String(describing: bc.weight!)) 
     } else if indexPath.row == 2 { 
      batchConsumptionBasicCell.configure(title: "Price", detail: String(format:"%.2f", bc.price)) 
     } 
    } else if batchConsumptionCount >= 2 { 
     let batchConsumptionMultiCell = tableView.dequeueReusableCell(withIdentifier: "BatchConsumptionMultiCell", for: indexPath) as! BatchConsumptionMultiCell 
     return batchConsumptionMultiCell 
    } 
    return UITableViewCell() 
} 

} 

查看評論// <-- This works fine
// <-- This does not work在BatchConsumptionCell

+0

顯示你的'setConsumumption' – Paulw11

+0

和也是在更新batchConsumptionCount值碼? – bubuxu

+0

對不起,忘了setConsumption(消費:)在BatchConsumptionCell – Balse

回答

0

您也應該註冊筆尖到BatchConsumptionCell類,並實施必要的表視圖的數據源&委託方法:

class BatchConsumptionCell: UITableViewCell, UITableViewDataSource, UITableViewDelegate { 

    @IBOutlet weak var tableView: UITableView! 

    var consumption: Consumption! 
    var batchConsumptionCount: Int! 

    override func awakeFromNib() { 
     super.awakeFromNib() 

     var cellNib = UINib(nibName: "BatchConsumptionBasicCell", bundle: nil) 
     tableView.register(cellNib, forCellReuseIdentifier: "BatchConsumptionBasicCell") 

     cellNib = UINib(nibName: "BatchConsumptionMultiCell", bundle: nil) 
     tableView.register(cellNib, forCellReuseIdentifier: "BatchConsumptionMultiCell") 

     self.tableView.delegate = self 
     self.tableView.dataSource = self 
    } 

    func setConsumption(consumption: Consumption) { 
     self.consumption = consumption 
     self.batchConsumptionCount = consumption.batchConsumptions?.count 
     tableView.reloadData() 
    } 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return consumption == nil ? 0 : 1 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if batchConsumptionCount == .some(1) { return 3 } 
     return batchConsumptionCount ?? 0 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     switch batchConsumptionCount { 
     case .some(1): 
      let batchConsumptionBasicCell = tableView.dequeueReusableCell(withIdentifier: "BatchConsumptionBasicCell", for: indexPath) as! BatchConsumptionBasicCell 
      let bc = consumption.batchConsumptions?.allObjects[0] as! BatchConsumption 
      if indexPath.row == 0 { 
       batchConsumptionBasicCell.configure(title: "Batch", detail: (bc.batch?.wtId)!) 
      } else if indexPath.row == 1 { 
       batchConsumptionBasicCell.configure(title: "Weight", detail: String(describing: bc.weight!)) 
      } else if indexPath.row == 2 { 
       batchConsumptionBasicCell.configure(title: "Price", detail: String(format:"%.2f", bc.price)) 
      } 
      return batchConsumptionBasicCell 

     default: 
      let batchConsumptionMultiCell = tableView.dequeueReusableCell(withIdentifier: "BatchConsumptionMultiCell", for: indexPath) as! BatchConsumptionMultiCell 
      return batchConsumptionMultiCell 

     } 
    } 

} 
+0

我已經這樣做了。 BatchConsumptionCell看起來非常符合您的建議。頭文件是'class BatchConsumptionCell:UITableViewCell,UITableViewDataSource,UITableViewDelegate',並且我使用'let nib = UINib(...'和'tableView.register(...')來調用單元格,但是我在哪裏注入我的依賴關係進入這個BatchConsumptionCell ??? – Balse

+0

你是什麼意思「注入我的依賴關係」?你是否可以在你的問題上粘貼你的整個BatchConsumptionCell代碼? – bubuxu

+0

在我的BatchConsumptionCell裏面我需要var'consumption'。目前,我通過'在我使用'dequeueReusableCell(withIdentifier:for:)'(見上面)加載它後,使用了batchConsumptionCell.setConsumption(consume:consumption)'但是當我嘗試根據'tableView(tableView:numberOfRowsInSection :)'我得到一個錯誤。'tableView(tableView:numberOfRowsInSection:)'似乎被調用之前,我可以設置我的'消費'變種。 – Balse