UITableViewCellのUITableViewCellStyleスタイルプロパティをすばやく正しく設定する方法!
How Set Uitableviewcellstyle Style Property Uitableviewcell Correctly Swift
3つのステップで私のエラーを見てください:
ステップ1ViewControllerのviewDidLoadメソッドで次のメソッドを使用します。
再利用が必要なセルのIDを登録するtableView.register(UITableViewCell.self、forCellReuseIdentifier: 'cellID')メソッド
ステップ2キーコードを貼り付けます
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ var cell: UITableViewCell? = nil cell = tableView.dequeueReusableCell(withIdentifier: 'cellID', for: indexPath) if cell == nil{ cell = UITableViewCell.init(style: UITableViewCellStyle.subtitle, reuseIdentifier: 'cellID') } cell?.textLabel?.text = '123123' cell?.imageView?.image = UIImage.init(named: 'ico_del.png') cell?.detailTextLabel?.text = '987654' cell?.detailTextLabel?.backgroundColor = UIColor.brown cell?.textLabel?.textColor = UIColor.black return cell! }
ステップ3:実行結果を確認する
問題が発生し、明確に設定されましたUITableViewCellStyle。字幕スタイル
cell = UITableViewCell.init(style: UITableViewCellStyle.subtitle, reuseIdentifier: 'cellID')
しかし、結果は予想に反することが判明し、それを示していますUITableViewCellStyle。デフォルトstyle! ! ! Nani ?????
デバッグした後、私は問題を見つけました:
if cell == nil {// Think about why this will not be executed, the style of the cell will be invalid cell = UITableViewCell.init(style: UITableViewCellStyle.subtitle, reuseIdentifier: 'cellID') }
このコードは実行する機会がまったくありません! ! !その理由は、上記の最初のステップでニーズが登録されているためです。セルIDを再利用して実行する
cell = tableView.dequeueReusableCell(withIdentifier: 'cellID', for: indexPath)
予想通り、セルの結果は自然にゼロにはなりません、ハハ!幸せになりすぎないでください
(---------------------あなたの不快感を断ち切らないでください ----------------------------)
次に、それが架空の結果であるかどうかを確認するために試してみてください。
断固として削除ViewControllerのviewDidLoadの登録メソッド
tableView.register (UITableViewCell.self, forCellReuseIdentifier: 'cellID') // Delete delete
登録に行かないでください、そしてコードが実行されるとき
cell = tableView.dequeueReusableCell(withIdentifier: 'cellID', for: indexPath)
セルの結果はnilである必要がありますが、実行結果は直接例外をスローします。 ! !
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier cellID - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
いいやつ、なぜ? ? ? ?ああ、myGod、以前はそうではなかった
長い間投げた後、私は間違った方法を使っていることに気づきました、Ma! ! !
正しい方法:
cell = tableView.dequeueReusableCell (withIdentifier: 'cellID') // Remember! ! !
進入禁止:
cell = tableView.dequeueReusableCell (withIdentifier: 'cellID', for: indexPath) // There is no right or wrong method, just use the wrong place, woo woo
ついに私が望んでいた結果が出ました、最後の写真、天国の精神を慰めます
終わり! ! !