J'ai essayé d'utiliser des cellules statiques à l'aide de XIB, mais dans la cellule pour la ligne au chemin d'index, j'obtiens une erreur du type "Impossible de convertir l'expression de retour de type 'UITableViewCell.Type' en type de retour 'UITableViewCell'" à la ligne de "return UITableViewCell ". Quelqu'un peut-il m'aider à le faire, merci d'avance.
extension TriipDetailsViewController: UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0
{
let cell : TripDriverDetailsCell = tripDetailsTableView.dequeueReusableCell(withIdentifier: "TripDriverDetailsCell", for: indexPath) as! TripDriverDetailsCell
return cell
}else if indexPath.row == 1 {
let cell : TripFareDetailsCell = tripDetailsTableView.dequeueReusableCell(withIdentifier: "TripFareDetailsCell", for: indexPath) as! TripFareDetailsCell
return cell
}else if indexPath.row == 2 {
let cell : TripPaymentModeCell = tripDetailsTableView.dequeueReusableCell(withIdentifier: "TripPaymentModeCell", for: indexPath) as! TripPaymentModeCell
return cell
}
return UITableViewCell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0{
return 230
}else if indexPath.row == 1{
return 200
}else if indexPath.row == 2{
return 120
}
}
}
3 réponses
let cell : UITableViewCell
if indexPath.row == 0
{
cell = tripDetailsTableView.dequeueReusableCell(withIdentifier: "TripDriverDetailsCell", for: indexPath) as! TripDriverDetailsCell
}else if indexPath.row == 1 {
cell = tripDetailsTableView.dequeueReusableCell(withIdentifier: "TripFareDetailsCell", for: indexPath) as! TripFareDetailsCell
}else if indexPath.row == 2 {
cell = tripDetailsTableView.dequeueReusableCell(withIdentifier: "TripPaymentModeCell", for: indexPath) as! TripPaymentModeCell
}else {
cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
}
return cell
}
Éliminez le return UITableViewCell
.
Certaines personnes pourraient dire return UITableViewCell()
, mais je préfère traiter ce cas comme l'erreur réelle, par exemple
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.row {
case 0:
return tableView.dequeueReusableCell(withIdentifier: "TripDriverDetailsCell", for: indexPath)
case 1:
return tableView.dequeueReusableCell(withIdentifier: "TripFareDetailsCell", for: indexPath)
case 2:
return tableView.dequeueReusableCell(withIdentifier: "TripPaymentModeCell", for: indexPath)
default:
fatalError("Unexpected row number \(indexPath.row)")
}
}
Le heightForRowAt
doit gérer de la même manière le scénario où le indexPath.row
n'est pas 0, 1 ou 2, par exemple:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch indexPath.row {
case 0: return 230
case 1: return 200
case 2: return 120
default: fatalError("Unexpected row number \(indexPath.row)")
}
}
Tu as
Impossible de convertir l'expression de retour de type 'UITableViewCell.Type' en type de retour 'UITableViewCell
Parce que vous renvoyez un cell
comme return UITableViewCell
, cela signifie que vous renvoyez une UITableViewCell
classe entière au lieu de single UITableViewCell()
donc dans le dernier vous devez retourner cell
comme return UITableViewCell()
- par exemple.
extension TriipDetailsViewController: UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0
{
let cell : TripDriverDetailsCell = tripDetailsTableView.dequeueReusableCell(withIdentifier: "TripDriverDetailsCell", for: indexPath) as! TripDriverDetailsCell
return cell
}else if indexPath.row == 1 {
let cell : TripFareDetailsCell = tripDetailsTableView.dequeueReusableCell(withIdentifier: "TripFareDetailsCell", for: indexPath) as! TripFareDetailsCell
return cell
}else if indexPath.row == 2 {
let cell : TripPaymentModeCell = tripDetailsTableView.dequeueReusableCell(withIdentifier: "TripPaymentModeCell", for: indexPath) as! TripPaymentModeCell
return cell
}
return UITableViewCell() //Here you've to change
}
Questions connexes
De nouvelles questions
ios
iOS est le système d'exploitation mobile fonctionnant sur Apple iPhone, iPod touch et iPad. Utilisez cette balise [ios] pour les questions liées à la programmation sur la plate-forme iOS. Utilisez les balises associées [objective-c] et [swift] pour les problèmes spécifiques à ces langages de programmation.