2017-06-28 15 views
0

これはバグです。これは私が解決したばかりのバグで、検索時にオンラインで回答が見つからなかったので、 "hmmm interesting ... "しかし何が起こっていたのか分かりません。UITableViewCellはタップされるまでコンテンツを表示しません/同じ行に2つのセルがある

一見したところ、私はいくつかのセルが表示されていたテーブルビューと、空白だったセルがありました。空のセルをタップすると、コンテンツが表示されます(ただし、タップするまでは表示されません)。

最初の考え:それはデータの問題で、セルをタップすると再読み込み中です。 tableViewを作成して読み込んだ後にデータやさらに個々の行をリロードしようとしましたが、これで問題は解決されませんでした。私はまた、メインのキューにこの呼び出しをディスパッチしようとしましたが、空白/空白のindexPathsにsetNeedsLayoutを呼び出すことを試みました

問題:私はビューデバッガを見て、2つの空白のセルが実際に存在し、彼らの上に空のセルを持っていた。そうです、ある行に2つのセルがあり、もう一方は上にあります。それがタップされたときに示す電池について興味がそれらのために

enter image description here

、このような理由で、私はまだわかりませんが、私はセルをタップした場合、空白のセルは、背後に行くだろう。まだ2つのセルがありますが、ブランクセルがその背後にあるため、カスタムセルが表示されるようになりました。

  • numberOfCellsInRowが正しい数を返した、4
  • ビューデバッガビューデバッガのメモリ・グラフ階層を表示6(2つの行の2個の細胞を含む4行)
  • この上に6個の細胞が存在した確認を示しましたテーブル

私はcellForRowAtIndexPathを見ましたが、私はUITableViewCell(ジェネリックタイプ)が使用されていることに気付いていました。私にとって不思議なことは、これは問題がなかった別のビューコントローラ(​​)で使用されていたのと同じ設定であるということです。しばらくして私は違いに気づいた。

これはfirstVCにあった一般的なパターンです。これは、空白のUITableViewCellで覆われていたという「customCell」だった、と「genericCellは」ストーリーボード

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let genericCell = tableView.dequeReusableCell(withIdentifier: "genericCell") 
    if indexPath = IndexPath(row:0, section:0) { 
     if let customCell = self.dequeueReusableCell(withIdentifier: "myCustomCell") as? MyCustomCell { 
     return customCell 
     } 
    } else if indexPath = IndexPath(row:1, section:0) { 
     genericCell.titleLabel.text = "cell1" 
    } else if indexPath = IndexPath(row:2, section: 0) { 
     genericCell.titleLabel.text = "cell2" 
    } 

    return genericCell 
} 

に標準のUITableViewCellた今​​上の異なる何をした最初の行です:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let genericCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "genericCell") 
    if indexPath = IndexPath(row:0, section:0) {...... 

IこれがiOS/Xcodeのバグかどうか、または論理的な説明があるのか​​どうかは分かりませんが、解決策はcustomCellを作成して返すだけで、genericCellを作成することです。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if indexPath = IndexPath(row:0, section:0) { 
     if let customCell = self.dequeueReusableCell(withIdentifier: "myCustomCell") as? MyCustomCell { 
     return customCell 
     } 
    } else { 
     let genericCell = tableView.dequeReusableCell(withIdentifier: "genericCell") 
     if indexPath = IndexPath(row:1, section:0) { 
      genericCell.titleLabel.text = "cell1" 
     } else if indexPath = IndexPath(row:2, section: 0) { 
      genericCell.titleLabel.text = "cell2" 
     } 
     return genericCell 
    } 
} 

のいずれかを使用するか、ストーリーボードからデキューするのではなくinitメソッドを使用してUITableViewCellを作成します(​​にバグがなくても機能します)。

答えて

0

そうのように、これはiOSの/ Xcodeのバグである場合は、このための論理的な説明があるかどうかが、解決策はcustomCellsが作成された後にのみ「genericCell」を作成することで、返された、私はまだわからない。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if indexPath = IndexPath(row:0, section:0) { 
     if let customCell = self.dequeueReusableCell(withIdentifier: "myCustomCell") as? MyCustomCell { 
     return customCell 
     } 
    } else { 
     let genericCell = tableView.dequeReusableCell(withIdentifier: "genericCell") 
     if indexPath = IndexPath(row:1, section:0) { 
      genericCell.titleLabel.text = "cell1" 
     } else if indexPath = IndexPath(row:2, section: 0) { 
      genericCell.titleLabel.text = "cell2" 
     } 
     return genericCell 
    } 
} 

​​にバグがなくても、ストーリーボードからデキューするのではなく、initメソッドを使用してUITableViewCellを作成することができます。

関連する問題