ios UITableViewCell 以编程方式设置样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9912597/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:36:08  来源:igfitidea点击:

UITableViewCell set style programmatically

iosuitableview

提问by Matrosov Alexander

How to create UITableViewCell with an next style (Right Detailin xib)

如何使用下一个样式创建 UITableViewCell(xib 中的Right Detail

enter image description here

在此处输入图片说明

For example can I use next:

例如,我可以使用下一个:

cell.style = 'Right Detail'(roughly)

cell.style = '正确的细节'(大致)

Thanks!

谢谢!

回答by yuji

What you want is UITableViewCellStyleValue1, but you can't set an existing cell's style: you have to set it when you're creating it. Like this:

你想要的是UITableViewCellStyleValue1,但你不能设置现有单元格的样式:你必须在创建它时设置它。像这样:

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"YourIdentifier"];

回答by csch

While the best answer for this question is correct it doesn't touch on the topic of cell reuse.

虽然这个问题的最佳答案是正确的,但它并未涉及细胞重用的话题。

It turns out you no longer have to use tableView.register. Instead you can use this approach:

事实证明,您不再需要使用tableView.register. 相反,您可以使用这种方法:

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

    // Whenever there's a reusable cell available, dequeue will return it
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier") ??
        UITableViewCell(style: .subtitle, reuseIdentifier: "cellIdentifier")