如何在 swift 中的 table 视图单元格中加载两个不同的布局

How do I load two different layouts inside a table view cell in swift

我有两个原型单元格,它们都有不同的布局。这样做的原因是一个细胞将装载雄性,一个细胞将装载雌性。我已经为两个单元格提供了一个标识符,现在我正在尝试将相关内容加载到每个单元格中。当我加载代码时,它只加载到原型单元格 1 中。

这是我在 cellForRowAtIndexPath 中使用的代码:

 let cellFemale : Profiles = tableView.dequeueReusableCellWithIdentifier("cellFemale") as Profiles
 let cellMale : Profiles = tableView.dequeueReusableCellWithIdentifier("cellMale") as Profiles

   if self.profile.genders[indexPath.row] == "female" {
        //Adding the textLabel
       cellFemale.nameLabel?.text = self.profile.names[indexPath.row]

        //Adding the profile picture
        var finalImage = UIImage(data: self.profile.images[indexPath.row])
        cellFemale.imageLabel?.image = finalImage

        //Adding the gender label
        //if self.profile.genders[indexPath.row] == "female"{
        cellFemale.genderLabel.text = "F"
        } else if self.profile.genders[indexPath.row] == "male" {

            //Adding the textLabel
            cellMale.nameLabel?.text = self.profile.names[indexPath.row]

            //Adding the profile picture
            var finalImage = UIImage(data: self.profile.images[indexPath.row])
            cellMale.imageLabel?.image = finalImage

            //Adding the gender label
            //if self.profile.genders[indexPath.row] == "female"{
            cellMale.genderLabel.text = "M"
        }

我确实忘记将 cellMale 添加为 return 值。但现在我明白了:(Profiles, Profiles) 不可转换为 UITableViewCell。完整代码:

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


        let cellFemale : Profiles = tableView.dequeueReusableCellWithIdentifier("cellFemale") as Profiles
        let cellMale : Profiles = tableView.dequeueReusableCellWithIdentifier("cellMale") as Profiles

  if self.profile.genders[indexPath.row] == "female" {
        //Adding the textLabel
       cellFemale.nameLabel?.text = self.profile.names[indexPath.row]

        //Adding the profile picture
        var finalImage = UIImage(data: self.profile.images[indexPath.row])
        cellFemale.imageLabel?.image = finalImage

        //Adding the gender label
        //if self.profile.genders[indexPath.row] == "female"{
        cellFemale.genderLabel.text = "F"
        } else if self.profile.genders[indexPath.row] == "male" {

            //Adding the textLabel
            cellMale.nameLabel?.text = self.profile.names[indexPath.row]

            //Adding the profile picture
            var finalImage = UIImage(data: self.profile.images[indexPath.row])
            cellMale.imageLabel?.image = finalImage

            //Adding the gender label
            //if self.profile.genders[indexPath.row] == "female"{
            cellMale.genderLabel.text = "M"
        }



        return (cellFemale, cellMale)
    }

您只想出列您将用于该行的单元格类型。此外,您的大部分代码在男性和女性案例之间重复。我建议:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let isMale = (self.profile.genders[indexPath.row] == "male")
    let identifier = isMale ? "cellMale" : "cellFemale"

    let cell = tableView.dequeueReusableCellWithIdentifier(identifier) as Profiles

    cell.nameLabel?.text = self.profile.names[indexPath.row]

    //Adding the profile picture
    let finalImage = UIImage(data: self.profile.images[indexPath.row])
    cell.imageLabel?.image = finalImage

    //Adding the gender label
    cell.genderLabel.text = isMale ? "M" : "F"

    return cell
}