ios - 使用 Nib 的多个 customTableViewCell

标签 ios uitableview swift

我正在尝试使用 Swift 创建多个自定义 tableViewCells。我使用了一个 objective-c 项目来转换成 swift 代码,这可能是它不起作用的原因。我用 xib 创建了 2 个 UITableViewCell 子类。然后我将子类添加到 xib 类。

但是当我注册 Nib 并尝试在 tableView 中显示它们时,tableView 是空的。我添加了将委托(delegate)连接到 uitableView

viewDidLoad

    self.tableView?.registerNib(UINib(nibName: "TextViewCell", bundle: nil), forCellReuseIdentifier: "TextViewCell")
    self.tableView?.registerNib(UINib(nibName: "AttachViewCell", bundle: nil), forCellReuseIdentifier: "AttachViewCell")

cellForRowAtIndexPath

func tableView(tableView:UITableView!, cellForRowAtIndexPath indexPath:NSIndexPath!) -> UITableViewCell! {
    var cell: UITableViewCell? = nil


    if indexPath.row == 0 {


        var  textCell:TextViewCell! = tableView.dequeueReusableCellWithIdentifier("TextViewCell") as? TextViewCell

        textCell.nameTextView.text = "Test"

        cell = TextViewCell()
    }

    if indexPath.row == 1 {
        var  attachCell:AttachViewCell! = tableView.dequeueReusableCellWithIdentifier("AttachViewCell") as? AttachViewCell

        attachCell.attachLabel.text = "Attach image"


        cell = AttachViewCell()
    }




    return cell
}

最佳答案

使用 Xcode 6 beta 7,您的 UITableViewController 类应该如下所示:

import UIKit

class ViewController: UITableViewController {

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.registerNib(UINib(nibName: "TextViewCell", bundle: nil), forCellReuseIdentifier: "TextViewCell")
        tableView.registerNib(UINib(nibName: "AttachViewCell", bundle: nil), forCellReuseIdentifier: "AttachViewCell")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCellWithIdentifier("TextViewCell", forIndexPath: indexPath) as TextViewCell
            cell.nameTextView!.text = "Test" //@IBOutlet weak var nameTextView: UILabel!
            return cell
        } else {
            let cell = tableView.dequeueReusableCellWithIdentifier("AttachViewCell", forIndexPath: indexPath) as AttachViewCell
            cell.attachLabel!.text = "Attach image" //@IBOutlet weak var attachLabel: UILabel!
            return cell
        }
    }

}

请注意,在 Xcode 6 beta 7 中,tableView:cellForRowAtIndexPath: 不再返回可选的 UITableViewCell。所以你必须使用以前的代码。如果您使用带有 tableView:cellForRowAtIndexPath: 的早期版本的 Xcode 6 beta 返回隐式展开的可选 UITableViewCell,您可以编写以下代码:

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCellWithIdentifier("TextViewCell", forIndexPath: indexPath) as TextViewCell
        cell.nameTextView!.text = "Test" //@IBOutlet weak var nameTextView: UILabel!
        return cell
    }

    if indexPath.row == 1 {
        let cell = tableView.dequeueReusableCellWithIdentifier("AttachViewCell", forIndexPath: indexPath) as AttachViewCell
        cell.attachLabel!.text = "Attach image" //@IBOutlet weak var attachLabel: UILabel!
        return cell
    }

    return nil
}

关于ios - 使用 Nib 的多个 customTableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25654006/

相关文章:

xcode - 动态设置表格 View 单元格行高?

javascript - 删除特定行中的先前单元格并在 Java 脚本中创建新单元格

ios - 如何以编程方式将图像保存在自定义位置?

iphone - 如何获取动态 JSON

iphone - iOS Storyboard如何在没有导航 Controller 的情况下使用 "push"样式从右侧呈现 Controller ?

ios - TableView 复制上的复选框,Swift,iOS

ios - 单元格更新后更新 TableView 节标题

ios - Realm 数据库对象似乎是空的,但事实并非如此

swift - 根据参数调用不同的函数

swift - RealityKit – Material 的 Alpha 透明度