Swift:来自XIB文件的多个自定义TableViewCells

时间:2016-12-30 11:11:50

标签: ios swift tableview xib tableviewcell

我正在关注此tutorial from Jared Davidson以在我的应用中使用XIB文件实现多个CustomTableViewCells。我在Xcode项目中有这些文件:Image Link Xcode Files

我有一个TextElement:Image Link和 我有一个ImageElement:Image Link

我希望使用离线数据对此进行测试,以便在此工作之后实施Firebase。这是我的Home.swift数据结构:

import Foundation
import FirebaseDatabase

struct Home {

var key:String!
let itemRef:FIRDatabaseReference?
var userUID:String!
var user:String!

// Home Element Cell Content
var elementSortNumber:Int!
var elementCellType:String!
var referenceElementID:String!

var databaseVersion:String!

init (key:String = "",
      uid:String,
      user:String,

      elementSortNumber:Int,
      elementCellType:String,
      referenceElementID:String,
      databaseVersion:String) {

    // General (Security tracking)
    self.key = key
    self.itemRef = nil
    self.userUID = uid
    self.user = user

    // Home Element Cell Content
    self.elementSortNumber = elementSortNumber
    self.elementCellType = elementCellType
    self.referenceElementID = referenceElementID

}

init (snapshot:FIRDataSnapshot) {

    // General (Security tracking)
    key = snapshot.key

    itemRef = snapshot.ref

    if let addedByUser = snapshot.value as? NSDictionary, let _temp = addedByUser["User"] as? String {
        user = _temp
    } else {
        user = ""
    }

    // Home Element Cell Content
    if let homeElementSortNumber = snapshot.value as? NSDictionary, let _temp = homeElementSortNumber["Title"] as? Int {
        elementSortNumber = _temp
    } else {
        elementSortNumber = 50
    }

    if let homeElementCellType = snapshot.value as? NSDictionary, let _temp = homeElementCellType["Content"] as? String {
        elementCellType = _temp
    } else {
        elementCellType = ""
    }

    if let homeElementID = snapshot.value as? NSDictionary, let _temp = homeElementID["Ref Element ID"] as? String {
        referenceElementID = _temp
    } else {
        referenceElementID = ""
    }

    if let textDatabaseVersion = snapshot.value as? NSDictionary, let _temp = textDatabaseVersion["DB Version"] as? String {
        databaseVersion = _temp
    } else {
        databaseVersion = ""
    }

}

}

这是我的TableViewController的代码:

import UIKit

class HomeTableViewController: UITableViewController {

    var arrayOfCellData = [Home]()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        arrayOfCellData =
            [Home(key: "",
                  uid:"",
                  user:"",

                  elementSortNumber:1,
                  elementCellType:"TextElement",
                  referenceElementID:"123ABC",
                  databaseVersion:"1"),

             Home(key: "",
                  uid:"",
                  user:"",

                  elementSortNumber:1,
                  elementCellType:"ImageElement",
                  referenceElementID:"QWERTZ",
                  databaseVersion:"1"),

             Home(key: "",
                  uid:"",
                  user:"",

                  elementSortNumber:1,
                  elementCellType:"TextElement",
                  referenceElementID:"XYZ789",
                  databaseVersion:"1")]

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // If I return 1 the app crashes and if I comment this function it also crashes.
        return 0
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrayOfCellData.count
    }

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

        if arrayOfCellData[indexPath.row].elementCellType == "TextElement" {

            let textElementCell = Bundle.main.loadNibNamed("TextElementTableViewCell", owner: self, options: nil) as! TextElementTableViewCell

            textElementCell.textElementTitleLabel.text = arrayOfCellData[indexPath.row].referenceElementID

            return textElementCell

        }

        else if arrayOfCellData[indexPath.row].elementCellType == "ImageElement" {

            let imageElementCell = Bundle.main.loadNibNamed("ImageElementTableViewCell", owner: self, options: nil) as! ImageElementTableViewCell

            imageElementCell.imageElementImageView.image = UIImage(named: "placeholder")

            return imageElementCell
        }

        else {

            let textElementDefaultCell = Bundle.main.loadNibNamed("TextElementTableViewCell", owner: self, options: nil) as! TextElementTableViewCell

            textElementDefaultCell.textElementTitleLabel.text = arrayOfCellData[indexPath.row].referenceElementID

            return textElementDefaultCell
        }

    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        if arrayOfCellData[indexPath.row].elementCellType == "TextElement" {
            return 116
        }
        else if arrayOfCellData[indexPath.row].elementCellType == "ImageElement" {
            return 275
        }
        else {
            return 116
        }
    }

}

这就是问题:模拟器是空的,你可以在这张图片中看到Image Link为什么?我该如何解决这个问题?

我真的很感激一些帮助。谢谢。

1 个答案:

答案 0 :(得分:3)

在viewdidload中注册xib文件,如下所示:

tableView.register(UINib(nibName: "TextElementTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "TextElementTableViewCellIdentifier")

然后在cellForRowIndex路径中:使用其标识符

访问单元格
let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "TextElementTableViewCellIdentifier", for: indexPath) as! TextElementTableViewCell