TableView의 셀을 탭하면 셀에 UIActivityIndicator 표시 (.accessoryView)
16301 단어 Swift
이번 내용
기능 설명
코드와 간략한 설명
셀 탭시 코드
cell?.accessoryView
에 indicatorView(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
}()
다른 셀 탭시 코드
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의 애니메이션을 종료합니다. didSelectRowAt
에서 애니메이션 시작 ➡️ 다른 셀을 선택 ➡️ didDeselectRowAt
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
}()
}
전체 코드
ViewControllerimport 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
}()
}
}
끝
지적, 질문 등 있으면, 코멘트까지 부탁드립니다.
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
}()
}
}
지적, 질문 등 있으면, 코멘트까지 부탁드립니다.
Reference
이 문제에 관하여(TableView의 셀을 탭하면 셀에 UIActivityIndicator 표시 (.accessoryView)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/HiroUrata/items/c823ab0cfd8768810100텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)