37

不显示详细信息(副标题)文本。但是,数据是可用的,因为当添加了 println() 调用时,它会将 Optional("data") 与预期的数据一起打印到控制台。在故事板中,UITableViewController 设置为适当的类,Table View Cell Style 设置为“Subtitle”,重用标识符设置为“cell”。如何获取要显示的字幕信息?

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

    var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

    dispatch_async(dispatch_get_main_queue(), { () -> Void in

        cell.textLabel.text = self.myArray[indexPath.row]["title"] as? String
        cell.detailTextLabel?.text = self.myArray[indexPath.row]["subtitle"] as? String

        println(self.myArray[indexPath.row]["subtitle"] as? String)
        // The expected data appear in the console, but not in the iOS simulator's table view cell.

    })
    return cell
}
4

13 回答 13

48

你的代码看起来不错。只需转到情节提要并选择表格视图的单元格-> 现在转到属性检查器并选择字幕样式。

根据以下屏幕截图进行操作。

更改此选项

希望它有帮助..

于 2015-09-17T07:33:53.767 回答
39

这里有同样的问题(从我读过的内容来看,可能是 iOS 8 中的一个错误?),这就是我们解决它的方法:

  1. 从情节提要中删除原型单元

  2. 删除这一行:

var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

  1. 替换为这些代码行:
让 cellIdentifier = "细胞"
            
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as?UITableViewCell
如果单元格 == nil {
    cell = UITableViewCell(样式:UITableViewCellStyle.Value2,reuseIdentifier:cellIdentifier)
}

Swift 3.1 更新

let cellIdentifier = "Cell"
    
var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
if cell == nil {
    cell = UITableViewCell(style: UITableViewCellStyle.value2, reuseIdentifier: cellIdentifier)
}

Swift 4.2 更新 - 简化

let cell = UITableViewCell(style: UITableViewCell.CellStyle.value2, reuseIdentifier: "cellId")

Swift 5 更新 - 简化

let cell = UITableViewCell(style: .value2, reuseIdentifier: "cellId")
于 2014-12-01T03:04:19.677 回答
16

如果您仍想使用情节提要中的原型单元格,请选择 TableViewcell 样式作为字幕。它会起作用的。

于 2014-12-01T03:08:37.420 回答
6

在此处输入图像描述

试试这个它对我有用(swift 5)

let cell = UITableViewCell(style: .value1, reuseIdentifier: "cellId")
cell.textLabel.text = "Déconnexion"
cell.imageView.image = UIImage(named: "imageName")

目标c:

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellId"];
于 2019-01-24T09:59:11.330 回答
5

如果在尝试将文本设置为非零值时将文本设置为 nil,则包含文本的实际视图将丢失。这是在 iOS8 中引入的。尝试设置为空白@" "字符。

看到这个:UITableViewCell 的字幕不会更新

于 2015-03-02T17:40:37.893 回答
5

如果在界面构建器中没有单元格的情况下以编程方式执行此操作,则此代码在 Swift 2.0+ 中就像一个魅力

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    yourTableView.delegate = self
    yourTableView.dataSource = self
    yourTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "subtitleCell")

}

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

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    let cell: UITableViewCell = yourTableView.dequeueReusableCellWithIdentifier("subtitleCell", forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = "the text you want on main title"
    cell.detailTextLabel?.text = "the text you want on subtitle"

    return cell
}
于 2015-11-05T21:04:17.137 回答
4

对于它的价值:我没有出现细节问题。那是因为我已经注册了 tableView 单元格,我不应该这样做,因为单元格原型是直接在情节提要中定义的。

于 2018-08-13T16:05:35.200 回答
2

在 Xcode11 和 Swift5 中,我们必须像下面那样做。如果我们通过检查条件 cell == nil 然后使用 cellStyle 创建 UITableViewCell 来做到这一点,则它不起作用。以下解决方案对我有用。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
           let cellIdentifier = "Cell"
            let cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: cellIdentifier)
    cell?.textLabel?.text = "Title"
    cell?.detailTextLabel?.text = "Sub-Title"

   return cell!
    }
于 2020-05-13T17:10:23.860 回答
1

这是 swift 5 的工作原理,使用 detailtextlabel 获取字幕,使用视图控制器中的 UITableView 对象,如果您缺少其中任何一个,它将无法工作并且可能会崩溃。

class ViewController: UIViewController, UITableViewDelegate,  UITableViewDataSource

在 viewDidLoad 中:

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "subtitleCell")

代理功能:

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

    // Fetch a cell of the appropriate type.
    let cell = UITableViewCell(style: .subtitle , reuseIdentifier: "subtitleCell")

    // Configure the cell’s contents.
    cell.textLabel!.text = "Main Cell Text"
    cell.detailTextLabel?.text = "Detail Cell Text"

    return cell
}
于 2019-10-13T15:33:51.997 回答
1

代码 11

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    //        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
    let cell = UITableViewCell(style: UITableViewCell.CellStyle.value1, reuseIdentifier: "reuseIdentifier")
    cell.detailTextLabel?.text = "Detail text"
    cell.textLabel?.text = "Label text"

    // Configure the cell...

    return cell
}
于 2019-12-17T11:44:07.650 回答
1

上面的一些解决方案并不完全正确。由于单元格应该被重用,而不是重新创建。您可以更改初始化方法。

final class CustomViewCell: UITableViewCell {
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: .value1, reuseIdentifier: reuseIdentifier)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
于 2020-05-21T12:19:09.290 回答
0

未来版本中将弃用三个属性:imageView textLabeldetailTextLabel

您可以使用UIListContentConfiguration配置单元格

dataSource = UITableViewDiffableDataSource(tableView: tableview, cellProvider: { tableview, indexPath, menu in
        let cell = tableview.dequeueReusableCell(withIdentifier: self.profileCellIdentifier, for: indexPath)
        var content = cell.defaultContentConfiguration()

        content.text = menu.title
        if indexPath.section == MenuSection.info.rawValue {
            content.image = UIImage(systemName: "person.circle.fill")
            content.imageProperties.tintColor = AppColor.secondary
        }
        if let subtitle = menu.subTitle {
            content.secondaryText = subtitle
        }
        cell.contentConfiguration = content
        return cell
    })
于 2021-08-01T09:10:45.257 回答
0

带有字幕文本的 Swift 5,这里无需在 viewDidLoad 中注册您的单元格:

    var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
       if cell == nil {
            cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell")
       }
    
    cell?.textLabel?.text = "title"
    cell?.textLabel?.numberOfLines = 0
    cell?.detailTextLabel?.text = "Lorem ipsum"
    cell?.detailTextLabel?.numberOfLines = 0

    
    return cell ?? UITableViewCell()
于 2021-05-28T08:44:09.007 回答