0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

UITableViewCell 再利用について(サンプルアプリと簡単解説) 

Posted at

今回の内容

  • UITableViewCellの再利用について調べた時に作成したサンプルアプリがあったので、簡単な解説とコードです。

サンプルアプリ

711DA893-5B13-4E53-8660-61AAE90F3BD9_1_201_a.jpeg
  • 再利用されたUITableViewCellは背景色を.systemGreenにして、cell.detailTextLabel.textに再利用される直前にcell.textLabel.textに表示していた値を、表示してどのcellがどこに再利用されたかを見ることが出来ます。

  • required init?(coder: NSCoder){ super.init(coder: coder) }はCellのインスタンスを生成するタイミングで処理が働きます。

  • prepareForReuse()UITableViewが持っているReuseQueueからCellを再利用するタイミングで処理が働きます。(まだtableViewにCellが表示される訳では無いです)

  • tableViewCellが表示されるタイミングで処理が働くのが、 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {}です。

  • 全てコードで書いています。コピペだけで試せますので宜しければご覧ください。

import UIKit

class ViewController: UIViewController {

    let tableView = UITableView()
        
    var count = Int()
    
    override func viewDidLoad() {
        super.viewDidLoad()
     
        tableView.frame = CGRect(x: view.frame.minX, y: view.frame.minY, width: view.frame.width, height: view.frame.height)
        tableView.register(CustomCell.self, forCellReuseIdentifier: "cell")
        view.addSubview(tableView)
        
        tableView.dataSource = self
    }

}

extension ViewController:UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 100
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
           
        count = count + 1
        print(count)
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
        
        if cell.textLabel?.text != ""{
            
            cell.detailTextLabel?.text = cell.textLabel?.text
        }
        
        cell.textLabel?.text = String(indexPath.row)
        
        return cell
    }
    
}

class CustomCell:UITableViewCell{
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: .value1, reuseIdentifier: reuseIdentifier)
        
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        
        if textLabel?.text != ""{
            
            detailTextLabel?.text = ""
            detailTextLabel?.text = textLabel!.text 
        }
        backgroundColor = .systemGreen
        print("reuse")   
    }

}

終わり

ご指摘、ご質問などありましたら、コメントまでお願い致します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?