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

时间:2016-12-22 02:48:58

标签: ios swift uitableview

嗨!我在UITableView上遇到了问题。我将PickerView添加到我的textField部分的某些行中,如下面的代码所示。实际上,pickerView将在indexPath.row添加到单元格,但不会为每个部分添加。所以这让我今天早上卡住了。对不起,我的英语不好。特别感谢您的解决方案。

这是我的代码:

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!
}

1 个答案:

答案 0 :(得分:0)

经过一番思考,我不妨给出正式答案。

您可以将textField添加到原型中,该单元格包含具有storyboard或单元子类的init函数的单元格。

然后在视图控制器中有一个indexPath属性,以便知道出现时要填充的选择器 - 我通常将其命名为currentIndexPath,并将其传递到单元格中。

将委托设置为拥有该选择器的textField的单元格。此外,在单元子类中创建一个数组来保存选择器的数据。

在cellForRow期间,您的代码看起来像

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

}