TableView中的每个单元格上的数据重复


嗨!我在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!

}

回答:

第二个想法我还不如给一个正式的答案后。

您可以将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

}

以上是 TableView中的每个单元格上的数据重复 的全部内容, 来源链接: utcz.com/qa/261645.html

回到顶部