1
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.

TableViewのCellをタップしたらCellにUIActivityIndicatorを表示(.accessoryView)

Posted at

今回の内容

72A0C612-BF6F-4A59-98F0-EBB221ABC952_1_105_c.jpeg

機能説明

  • tableViewのCellをタップ時にCellの右側にindicatorをアニメーションさせます。

  • 別のCellをタップすると表示済みのindicatorのアニメーションを終了します。

コードと簡単解説

Cellタップ時のコード

  • cell?.accessoryViewindicatorView(UIActivityIndicatorView())を追加

  • .startAnimating()はindicatorが、くるくるアニメーションします。

tableView
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let cell = tableView.cellForRow(at: indexPath)
        
        cell?.accessoryView = {() -> UIActivityIndicatorView in

            let indicatorView = UIActivityIndicatorView(frame: CGRect(x: (cell?.frame.maxX)! - ((cell?.frame.maxX)! / 4), y: (cell?.frame.minY)! , width: (cell?.frame.size.width)! / 4, height: (cell?.frame.size.height)!))

            indicatorView.color = .black
            indicatorView.startAnimating()

            return indicatorView
        }()

別のCellタップ時のコード

  • func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {}は、Cellの選択状態が終了したら実行されます。

  • 流れとしては、func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {}で選択されたらindicatorをアニメーションさせて、別のCellを選択した時にindicatorが表示されていたCellの選択状態が終了するので、func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {}でindicatorのアニメーションを終了します。

  • 簡単に書くと、cellを選択didSelectRowAtでアニメーション開始 ➡️ 別のCellを選択 ➡️ didDeselectRowAtでアニメーション終了 ➡️ didSelectRowAtでアニメーション開始

tableView
    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        
        let cell = tableView.cellForRow(at: indexPath)
        
        cell?.accessoryView = {() -> UIActivityIndicatorView in

            let indicatorView = UIActivityIndicatorView()

            indicatorView.stopAnimating()

            return indicatorView
        }()  
    }

全体コード

ViewController
import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!
    
    let tableViewContentsArray = ["1","2","3","4","5","6","7","8","9","10"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.delegate = self
        tableView.dataSource = self
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 63
        
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        
        return 1
        
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return tableViewContentsArray.count
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
               
        let cellLabel = cell.contentView.viewWithTag(1) as! UILabel
        
        cellLabel.text = tableViewContentsArray[indexPath.row]
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let cell = tableView.cellForRow(at: indexPath)
        
        cell?.accessoryView = {() -> UIActivityIndicatorView in

            let indicatorView = UIActivityIndicatorView(frame: CGRect(x: (cell?.frame.maxX)! - ((cell?.frame.maxX)! / 4), y: (cell?.frame.minY)! , width: (cell?.frame.size.width)! / 4, height: (cell?.frame.size.height)!))

            indicatorView.color = .black
            indicatorView.startAnimating()

            return indicatorView
        }()
        
    }
    
    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        
        let cell = tableView.cellForRow(at: indexPath)
        
        cell?.accessoryView = {() -> UIActivityIndicatorView in

            let indicatorView = UIActivityIndicatorView()

            indicatorView.stopAnimating()

            return indicatorView
        }()  
    }

}

終わり

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

1
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
1
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?