ios 如何在 Swift 中设置 UITableViewCellStyleSubtitle 和 dequeueReusableCell?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24062285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Set UITableViewCellStyleSubtitle and dequeueReusableCell in Swift?
提问by user3127576
I'd like a UITableView
with subtitle
-style cells that use dequeueReusableCellWithIdentifier
.
我想要一个UITableView
有subtitle
风格的细胞使用dequeueReusableCellWithIdentifier
。
My original Objective-C code was:
我原来的 Objective-C 代码是:
static NSString* reuseIdentifier = @"Cell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
}
After searching the few UITableView
questions here already on SO, I thought to write it in Swift like so:
在UITableView
SO 上搜索了几个问题之后,我想像这样用 Swift 编写它:
tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "Cell")
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
But that doesn't let me say I want a subtitle
style. So I tried this:
但这并不能让我说我想要一种subtitle
风格。所以我试过这个:
var cell :UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
Which gives me a subtitle
cell, but it doesn't let me dequeueReusableCellWithIdentifier
.
这给了我一个subtitle
细胞,但它不让我dequeueReusableCellWithIdentifier
。
I've researched some more and looked at this video tutorial, but he creates a separate subclass
of UITableViewCell
which I assume is unnecessary as I accomplished this same effect previously in Obj-C.
我已经研究了更多并查看了这个视频教程,但他创建了一个单独subclass
的UITableViewCell
,我认为是不必要的,因为我之前在 Obj-C 中完成了同样的效果。
Any ideas? Thanks.
有任何想法吗?谢谢。
回答by memmons
Keep in mind that UITableView
is defined as an optional in the function, which means your initial cell declaration needs to check for the optional in the property. Also, the returned queued cell is also optional, so ensure you make an optional cast to UITableViewCell
. Afterwards, we can force unwrap because we know we have a cell.
请记住,UITableView
它在函数中定义为可选,这意味着您的初始单元格声明需要检查属性中的可选。此外,返回的排队单元也是可选的,因此请确保对 进行可选的强制转换UITableViewCell
。之后,我们可以强制展开,因为我们知道我们有一个单元格。
var cell:UITableViewCell? =
tableView?.dequeueReusableCellWithIdentifier(reuseIdentifier) as? UITableViewCell
if (cell == nil)
{
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,
reuseIdentifier: reuseIdentifier)
}
// At this point, we definitely have a cell -- either dequeued or newly created,
// so let's force unwrap the optional into a UITableViewCell
cell!.detailTextLabel.text = "some text"
return cell
回答by Jeremy Massel
If you'd rather avoid optionality, you can make a subclass of UITableViewCell that looks something like this:
如果你宁愿避免可选性,你可以创建一个 UITableViewCell 的子类,看起来像这样:
class SubtitleTableViewCell: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Then register it using:
然后使用以下方法注册它:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(SubtitleTableViewCell.self, forCellReuseIdentifier: reuseIdentifier)
}
This allows your cell customization code to be really nice:
这使您的单元格自定义代码非常好:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath)
cell.textLabel?.text = "foo"
cell.detailTextLabel?.text = "bar"
return cell
}
回答by Nicolas Miari
Basically the same as other answers, but I get around dealing with nasty optionals (you can't return nil
from -tableView:cellForRow:atIndexPath:
in Swift) by using a computed variable:
基本上同其他的答案,但我得到周围讨厌自选(你不能返回处理nil
来自-tableView:cellForRow:atIndexPath:
斯威夫特)通过使用计算变量:
Swift 3
斯威夫特 3
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell") else {
// Never fails:
return UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "UITableViewCell")
}
return cell
}()
// (cell is non-optional; no need to use ?. or !)
// Configure your cell:
cell.textLabel?.text = "Key"
cell.detailTextLabel?.text = "Value"
return cell
}
Edit:
编辑:
Actually, it would be better to dequeue the cell using: tableView.dequeueReusableCell(withIdentifier:for:)
instead.
实际上,最好使用:tableView.dequeueReusableCell(withIdentifier:for:)
代替使单元出列。
This later variant of the function automatically instantiates a new cell if no one is available for reusing (exactly what my code does explicitly above), and therefore neverreturns nil
.
如果没有人可以重用(正是我的代码在上面明确执行的操作),该函数的这个后来的变体会自动实例化一个新单元格,因此永远不会返回nil
.
回答by jonlambert
Just building upon memmons' answer by cleaning it up Swift 2 style...
只是通过清理 Swift 2 风格来建立在 memmons 的答案上......
let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) ?? UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: reuseIdentifier)
cell.detailTextLabel?.text = "some text"
return cell
Swift 3:
斯威夫特 3:
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) ?? UITableViewCell(style: .subtitle, reuseIdentifier: cellIdentifier)
cell.detailTextLabel?.text = ""
return cell
回答by Meilbn
Since tableView.dequeueReusableCell(withIdentifier:, for:)
return a non-nil cell, the if cell == nil
check is always be false.
But I found a solution, to makes default style cell become what style(value1, value2 or subtitle) you want, because default style cell's detailTextLabel
is nil, so check the detailTextLabel
if it's nil, then create new style cell, and give it to dequeue cell, like:
由于tableView.dequeueReusableCell(withIdentifier:, for:)
返回非零单元格,因此if cell == nil
检查始终为假。但是我找到了一个解决方案,使默认样式单元格成为您想要的样式(值1,值2或副标题),因为默认样式单元格detailTextLabel
为nil,因此请检查它detailTextLabel
是否为nil,然后创建新样式单元格,并将其提供给出列单元格, 喜欢:
Swift 3:
斯威夫特 3:
var cell = tableView.dequeueReusableCell(withIdentifier: yourCellReuseIdentifier, for: indexPath)
if cell.detailTextLabel == nil {
cell = UITableViewCell(style: .value1, reuseIdentifier: repeatCellReuseIdentifier)
}
cell.textLabel?.text = "Title"
cell.detailTextLabel?.text = "Detail"
return cell
That's works for me.
这对我有用。
Hope it's help.
希望它有帮助。
回答by Geoffrey
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let reuseIdentifier = "cell"
var cell:UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as UITableViewCell?
if (cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: reuseIdentifier)
}
cell!.textLabel?.text = self.items[indexPath.row]
cell!.detailTextLabel?.text = self.items[indexPath.row]
return cell!
}
回答by lammert
You can use a slightly different syntax than the one from memmons to prevent the forced unwrapping:
您可以使用与 memmons 略有不同的语法来防止强制解包:
let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as? UITableViewCell ?? UITableViewCell(style: .Subtitle,
reuseIdentifier: reuseIdentifier)
cell.detailTextLabel?.text = "some text"
return cell
This is using XCode 6.17, Swift 1.22.0 syntax where UITableView
is no longer an optional.
这是使用 XCode 6.17, Swift 1.22.0 语法,其中UITableView
不再是可选的。
回答by Sudhi 9135
If you are not using your own Custom Cell. Simply register UITableViewCell through code. Then you can prefer code.
如果您没有使用自己的自定义单元。只需通过代码注册 UITableViewCell 即可。然后你可以更喜欢代码。
Else select storyboard, select your TableViewCell -> Goto Attribute Inspector and choose the desired style as shown below.
否则选择 storyboard,选择 TableViewCell -> Goto Attribute Inspector 并选择所需的样式,如下所示。
回答by iOS Lifee
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
var cell:UITableViewCell? =
tableView.dequeueReusableCell(withIdentifier: "cell")
if (cell != nil)
{
cell = UITableViewCell(style: UITableViewCellStyle.subtitle,
reuseIdentifier: "cell")
}
cell!.textLabel?.text = "ABC"
cell!.detailTextLabel?.text = "XYZ"
return cell!
}
回答by Murali
Make sure that you are not registering any cell to the tableview.
确保您没有将任何单元格注册到 tableview。
If you did so, dequeueReusableCellWithIdentifier will always give a non optional cell so UITableViewCellStyle.Subtitle will never initiate.
如果这样做, dequeueReusableCellWithIdentifier 将始终提供一个非可选单元格,因此 UITableViewCellStyle.Subtitle 永远不会启动。