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. 次のコード行に置き換えます。
let cellIdentifier = "セル"
            
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell
if セル == nil {
    セル = 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 に答える
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 に答える
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 に答える