Несколько табличных представлений внутри Collection-view - PullRequest
0 голосов
/ 27 июня 2018

Я работаю над Несколько табличных представлений внутри Collection-view . И я следовал за этой статьей. Но внутри метода cellForRowAt indexPath я получаю следующую ошибку.

Значение типа 'UITableViewCell' не имеет члена 'lblLab'
Значение типа 'UITableViewCell' не имеет члена 'lblLab'
Значение типа 'UITableViewCell' не имеет члена 'lblMedicine'

Я уже создал отдельные классы для всех трех ячеек таблицы, в которых уже упоминались все три метки.

Ниже приведен мой код для того же, который написан внутри класса ячеек с коллекцией.

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell : UITableViewCell;
    if tableView == tableLAB {
        cell = tableView.dequeueReusableCell(withIdentifier: "labCell", for: indexPath) as! LabTableCell;
        cell.lblLab!.text = arr1[indexPath.row];
    } else if tableView == tableDIAGNOSIS {
        cell = tableView.dequeueReusableCell(withIdentifier: "diagnosisCell", for: indexPath) as! DiagnosisTableCell;
        cell.lblDiagnosis!.text = arr1[indexPath.row];
    } else if tableView == tableMEDICINE {
        cell = tableView.dequeueReusableCell(withIdentifier: "medicineCell", for: indexPath) as! MedicineTableCell;
        cell.lblMedicine!.text = arr1[indexPath.row];
    }

    return cell;
}

Вы можете сказать мне, что я делаю неправильно? Заранее спасибо.

Ответы [ 2 ]

0 голосов
/ 27 июня 2018

Проблема в том, что вы пытаетесь принудительно привести объект, инициализированный через родительский класс, в дочерний класс, это невозможно, и вы можете убедиться, что у вас есть условные операторы для всех случаев, т.е.

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

    if(tableview == a){
    //This is a conditional statement
    }
    else if(tableview == b){
    //This is a conditional statement
    }
    else{
    //This is a conditional statement
    }

    //no need to return anything here as your conditional operators are handling all return //cases
    }

и в каждом условном операторе объявите и инициализируйте свой уникальный тип ячейки и верните его, например.

let cell : CellType = tableView.dequeueReusableCell(withIdentifier: "CellType", for: indexPath) as! CellType;
return cell;
0 голосов
/ 27 июня 2018

Вы можете попробовать

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

    if tableView == tableLAB {
        let cell = tableView.dequeueReusableCell(withIdentifier: "labCell", for: indexPath) as! LabTableCell;
        cell.lblLab!.text = arr1[indexPath.row];
         return cell;
    } else if tableView == tableDIAGNOSIS {
       let cell = tableView.dequeueReusableCell(withIdentifier: "diagnosisCell", for: indexPath) as! DiagnosisTableCell;
        cell.lblDiagnosis!.text = arr1[indexPath.row];
         return cell;
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "medicineCell", for: indexPath) as! MedicineTableCell;
        cell.lblMedicine!.text = arr1[indexPath.row];
         return cell;
    }


}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...