如何为表格视图部分中标题的高度变化设置动画? [英] How to animate the height changing of a header in section of a tableview?
本文介绍了如何为表格视图部分中标题的高度变化设置动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 Xcode 9.3 版、Swift 开发 iOS 应用程序.
Developing an iOS application with Xcode ver 9.3, Swift.
你能告诉我如何为 tableview 部分中标题的高度变化设置动画吗?
Could you tell me how to animate the height changing of a header in section of a tableview?
- 搜索栏放置在部分的标题中.
- 点击导航栏按钮切换搜索栏的显示和隐藏.(显示:标题高度= 44,隐藏:标题高度= 0)
- 我想在切换搜索栏显示和隐藏时设置动画.
代码如下.节中标题的高度立即更改,无需动画...
The code is as follows. The height of a header in section is immediately changed without animation...
import UIKit
class TableViewController: UITableViewController, UISearchBarDelegate {
let array = ["apple", "orange", "melon", "banana", "peach"]
let searchBar = UISearchBar()
var searchButtonTapped = false // Search bar is displayed or not
override func viewDidLoad() {
super.viewDidLoad()
searchButtonTapped = false
let searchButton = UIBarButtonItem(title: "Search", style: UIBarButtonItemStyle.plain, target: self, action: #selector(searchTapped))
self.navigationItem.leftBarButtonItem = searchButton
}
@objc func searchTapped() {
if searchButtonTapped == true {
searchButtonTapped = false
searchBar.text = nil
searchBar.resignFirstResponder()
} else {
searchButtonTapped = true
}
self.tableView.reloadData()
}
// change the height of header in section
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if searchButtonTapped == true {
return 44
} else {
return 0.1
}
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchBar.setShowsCancelButton(true, animated: true)
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.text = nil
searchBar.setShowsCancelButton(false, animated: true)
searchBar.endEditing(true)
self.tableView.reloadData()
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
searchBar.delegate = self
searchBar.placeholder = "Search..."
return searchBar
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = array[indexPath.row]
return cell
}
}
截图
推荐答案
self.tableView.reloadData()
没有动画.
你可以试试
reloadRows(at: [IndexPath], with: UITableViewRowAnimation)
或reloadSections(sections: IndexSet, with: UITableViewRowAnimation)
这篇关于如何为表格视图部分中标题的高度变化设置动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文