I am facing issue with dynamic cell which create multiple Textfield based on selection of my medicationTime .when i entered select medicationTime based on that my textfields are created and if i deselect medicationTime ,textfield should get removed from uiTableview cell which is working fine.Problem i am facing while scrolling my tableview as 1st textfield value get copied to 5 th textfield and vice versa. Also while deleting textfiled the values get copying
UITableViewDelegate ,UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if timeArray.count > 0 {
return timeArray.count
} else {
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: AddMedicationTableViewCell = medicineTimeTableView.dequeueReusableCell(withIdentifier: "AddMedicationTimeCell",for:indexPath as IndexPath) as! AddMedicationTableViewCell
cell.quantityTextField.delegate = self
if timeArray.count > 0 {
cell.timeLabel.text = timeArray[indexPath.row]
cell.quantityTextField.keyboardType = .decimalPad
// cell.quantityTextField.delegate = self
// let subViews = cell.contentView.subviews
// for subview in subViews
// {
// if (subview is UITextField)
// {
// subview.removeFromSuperview()
// }
// }
if arrayofTextField.indices.contains(indexPath.row)
{
cell.quantityTextField = arrayofTextField[indexPath.row ]
} else {
cell.quantityTextField.text = dataList[indexPath.row]
cell.quantityTextField.tag = indexPath.row
arrayofTextField.append(cell.quantityTextField)
}
cell.selectionStyle = .none
return cell
}
//Deleting code
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let deSelectedCell = collectionView.cellForItem(at: indexPath)
label?.textColor = UIColor(rgb:0x58595B)
let label1 = cell.viewWithTag(3000+indexPath.row) as? UILabel
if label1?.tag != nil {
timeSelected = checkTimeSelected((label1?.tag)!)
for eachTime in timeArray {
if eachTime == timeSelected {
if let index = timeArray.index(of: eachTime) {
timeArray.remove(at: index)
timingsArry.remove(at: index)
dataList[index] = ""
arrayofTextField.remove(at: index)
medicineTimeTableView.reloadData()
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…