今回の内容

機能説明
-
tableViewのCellをタップ時にCellの右側にindicatorをアニメーションさせます。
-
別のCellをタップすると表示済みのindicatorのアニメーションを終了します。
コードと簡単解説
Cellタップ時のコード
-
cell?.accessoryView
にindicatorView(UIActivityIndicatorView())
を追加 -
.startAnimating()
はindicatorが、くるくるアニメーションします。
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
でアニメーション開始
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
cell?.accessoryView = {() -> UIActivityIndicatorView in
let indicatorView = UIActivityIndicatorView()
indicatorView.stopAnimating()
return indicatorView
}()
}
全体コード
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
}()
}
}
終わり
ご指摘、ご質問などありましたら、コメントまでお願い致します。