是否可以创建一个可以在多个 Table Controller 中使用的 tableViewCell? [英] Is it possible to create one tableViewCell that can be used in multiple Table Controllers?

查看:20
本文介绍了是否可以创建一个可以在多个 Table Controller 中使用的 tableViewCell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约 3 或 4 个表控制器,它们都将使用相同的 tableview 单元格.我继续回到第四个可能的逻辑.假设两者之间的信息相同,我可以在多个 tableView 控制器中使用相同的 tableViewCell 吗?还是我必须为每个控制器创建一个新单元?

I have about 3 or 4 table controllers that will all use the same tableview cell. I keep going back on fourth of the possible logic. Can I use the same tableViewCell in multiple tableView Controllers assuming the information is between the two is the same? Or will I have to create a new cell for each controller?

推荐答案

是的,你可以.

我假设您使用的是 Swift.

I am assuming that you are using Swift.

转到文件 -> 新建并选择 cocoaTouch 类,如下所示.

Goto File -> New and select cocoaTouch class as follows.

现在为自定义单元命名您的类,并使其成为 UITableViewCell 的子类.还要选中也创建 Xib 文件"框

Now name Your class for custom cell and make it subClass of UITableViewCell. also check the box which says "Also create Xib file"

现在在这个 Xib 中设计你的单元并在它的 .Swift 文件中创建出口.假设您有一个自定义 tableView 单元格,它看起来像这样

Now design your cell in this Xib and create outlets in its .Swift file. Lets say you have a custom tableView cell which looks something like this

其中包含标签或 ImageView 或您单元格中的任何内容.现在在自定义单元格的 swift 文件中,您可以编写这样的方法

Which contains a label or ImageView or anyThing that you have in your cell. Now in your swift file of custom cell you can write a method like so

class func cellForTableView(tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> YourCustomTableViewCell {
let kYourCustomTableViewCellIdentifier = "kYourCustomTableViewCellIdentifier"
tableView.registerNib(UINib(nibName: "YourCustomTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: kYourCustomTableViewCellIdentifier)

let cell = tableView.dequeueReusableCellWithIdentifier(kYourCustomTableViewCellIdentifier, forIndexPath: indexPath) as! YourCustomTableViewCell

return cell
}

现在你可以在你的应用程序的任何 tableView 中使用这个单元格,就像下面一样

Now you can use this cell in any tableView in your application just like below

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

let cell = YourCustomTableViewCell.cellForTableView(tableView, atIndexPath: indexPath)
cell.backgroundColor = UIColor.clearColor()
// do something with your cell

}

希望能帮到你.

Swift 3 和 Swift 4 的更新:

class func cellForTableView(tableView: UITableView, atIndexPath indexPath: IndexPath) -> YourCustomTableViewCell {
    let kYourCustomTableViewCellIdentifier = "kYourCustomTableViewCellIdentifier"
    tableView.register(UINib(nibName: "YourCustomTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: kYourCustomTableViewCellIdentifier)

    let cell = tableView.dequeueReusableCell(withIdentifier: kYourCustomTableViewCellIdentifier, for: indexPath) as! YourCustomTableViewCell

    return cell
}

这篇关于是否可以创建一个可以在多个 Table Controller 中使用的 tableViewCell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆