2016-12-22 69 views
0

嗨!我在UITableView上遇到問題。我將PickerView添加到我的textField的部分行中,如下面的代碼所示。實際上,pickerView將添加到indexPath.row中的單元格,但不是每個部分。所以這讓我今天早上堅持下去。對不起,我的英語不好。特別感謝您的解決方案。TableView中的每個單元格上的數據重複

這裏是我的代碼:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let data = (self.tableItems[indexPath.section].value?[indexPath.row])! 
    let identifier : String = data["CELL_IDENTIFIER"]!! 
    let cell = tableView.dequeueReusableCell(withIdentifier: identifier) 
    if cell is QuotationHeaderInSectionCell{ 
     let headerCell = cell as! QuotationHeaderInSectionCell 
     headerCell.lblHeaderInSectionTitle.text = data["TEXT_LABEL"]!! 
    }else if cell is QuotationItemCell{ 
     let itemCell = cell as! QuotationItemCell 
     itemCell.lblCellTitle.text = data["TEXT_LABEL"]!! 
     switch indexPath.section { 
     case 0: 
      switch indexPath.row { 
      case 1: 
       self.initInputViewForTextField(sender: itemCell.textFieldCell, inputView: titlePickerView) 
       break 
      case 4: 
       self.initInputViewForTextField(sender: itemCell.textFieldCell, inputView: genderPickerView) 
       break 
      case 10: 
       self.initInputViewForTextField(sender: itemCell.textFieldCell, inputView: provincePickerView) 
       break 
      case 11: 
       self.initInputViewForTextField(sender: itemCell.textFieldCell, inputView: districtPickerView) 
       break 
      case 12: 
       self.initInputViewForTextField(sender: itemCell.textFieldCell, inputView: communePickerView) 
       break 
      case 13: 
       self.initInputViewForTextField(sender: itemCell.textFieldCell, inputView: villagePickerView) 
       break 
      default: 
       break 
      } 
      break 
     case 1: 
      switch indexPath.row { 
      case 0: 
       self.initInputViewForTextField(sender: itemCell.textFieldCell, inputView: assetMakePickerView) 
       break 
      case 1: 
       self.initInputViewForTextField(sender: itemCell.textFieldCell, inputView: assetRangePickerView) 
       break 
      case 2: 
       self.initInputViewForTextField(sender: itemCell.textFieldCell, inputView: assetModelPickerView) 
       break 
      case 3: 
       self.initInputViewForTextField(sender: itemCell.textFieldCell, inputView: assetColorPickerView) 
       break 
      default: 
       break 
      } 
     case 2: 
      if indexPath.row == 0 { 
       self.initInputViewForTextField(sender: itemCell.textFieldCell, inputView: dealerPickerView) 
      } 
      break 
     case 3: 
      switch indexPath.row { 
      case 0: 
       self.initInputViewForTextField(sender: itemCell.textFieldCell, inputView: financeProductPickerView) 
       break 
      case 1: 
       self.initInputViewForTextField(sender: itemCell.textFieldCell, inputView: advPaymentPercentPickerView) 
       break 
      case 3: 
       self.initInputViewForTextField(sender: itemCell.textFieldCell, inputView: assetColorPickerView) 
       break 
      default: 
       break 
      } 
      break 
     default: 
      break 
     } 

    }else if cell is QuotationCommentCell{ 
     let commentCell = cell as! QuotationCommentCell 
     commentCell.textViewComment.text = data["TEXT_LABEL"]!! 
    }else if cell is DocumentTableViewCell{ 
     let documentCell = cell as! DocumentTableViewCell 
    } 
    return cell! 
} 
+0

由於的tableView重用細胞,你可能要考慮具有選擇器視圖作爲細胞的一部分,由故事板原型或通過細胞亞類在裏面。然後有選擇地只顯示它在假設有一個選擇器的單元上。至於選擇器的值,你可以將每個值設置爲一個數組,作爲這個視圖控制器或全局常量的常量。這樣,您無需爲出現的每個單元格添加和刪除視圖。 –

+0

什麼類型的數組爲常量?更多詳細信息plz – Socheat

回答

0

第二個想法我還不如給一個正式的答案後。

您可以將textField添加到具有storyboard或單元格子類的init函數的單元格的原型中。

然後在視圖控制器中有一個indexPath屬性來知道當它出現時你正在填充哪個選擇器 - 我通常將它命名爲currentIndexPath,並將它傳遞給單元格。

將委託設置爲擁有該選取器的textField的單元格。另外,在單元格子類中創建一個數組來保存選擇器的數據。

在cellForRowAt你的代碼看起來像

if cell is QuotationItemCell { 
    let itemCell = cell as! QuotationItemCell 
    switch indexPath.section { 
    case 0: 
     switch indexPath.row { 
     case 1,4,10,11,12,13: 
      itemCell.textField.alpha = 1 
      currentIndexPath = indexPath 
      itemCell.currentIndexPath = indexPath //Property of the cell subclass 
      itemCell.dataArray = dataDict[indexPath.row] 
      itemCell.pickerView.reloadAllComponent //If the pickerView cannot be accessed here create a method in the cell subclass to reload it. 
     default: 
      itemCell.textField.alpha = 0 
     } 
    } 
} 

在UIPickerDelegate方法titleForRow可以從電池的陣列剛讀。

你問實際上可能是一個字典更好的陣列

class yourViewController, UIViewController { 
    let dataDict = [4:["Male","Female"], 
        10:["Something","Else"]] //Make sure the key correspond to the row the data is in 
    var currentIndexPath:IndexPath = IndexPath() //This is suppose to be here too 

} 
+0

pickerView不在QUotationItemCell – Socheat

+0

我添加到textField,以便當我專注於文本字段時,它加載pickerView – Socheat

+0

給我一下,我會嘗試調整它的使用 –

相關問題