ios - 如何在表格 View 单元格中快速加载两个不同的布局

标签 ios uitableview swift

我有两个原型(prototype)单元格,它们都有不同的布局。这样做的原因是一个细胞将装载雄性,一个细胞将装载雌性。我已经为两个单元格提供了一个标识符,现在我正在尝试将相关内容加载到每个单元格中。当我加载代码时,它只会加载到原型(prototype)单元格 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 作为返回值。但现在我明白了:(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
}

关于ios - 如何在表格 View 单元格中快速加载两个不同的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27747554/

相关文章:

objective-c - ARM 汇编 (IOS) 中 Objective-C 选择器的偏移量

uitableview - 嵌入导航栏中时 iOS 7 中奇怪的 UISearchDisplayController View 偏移行为

ios - 使用委托(delegate)从按钮传递信息以突出显示单元格

ios - 使用 NSFetchedResultsController 委托(delegate)方法将多行插入 tableView 时出现问题

调用错误中的 Swift2 额外参数

ios - Google gtl 框架集成在模拟器上出现错误,但在设备上使用

ios - 每个目标多个配置文件

iOS10 UIView模糊效果带阴影

ios - Swift Sprite Kit 再次调用 Function

swift - 覆盖子类中的枚举情况