import UIKit
class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var messages = [Int]()
override func viewDidLoad() {
super.viewDidLoad()
for x in 0...25 {
messages.append(x)
}
// Start with the tableview scrolled down by 44
// so the search bar doesn't show up only until you scroll back up
// Like in the iMessage App.
let height = tableView.tableHeaderView?.frame.size.height
let pointXY = CGPoint(x: 0, y: height!)
tableView.setContentOffset(pointXY, animated: false)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messages.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! TableViewCell
cell.messageLabel.text = "Message # \(indexPath.row)"
return cell
}
}
extension TableViewController: UISearchBarDelegate {
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
// Hide the navigation bar when they press on search
navigationController?.setNavigationBarHidden(true, animated: true)
searchBar.setShowsCancelButton(true, animated: true)
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
navigationController?.setNavigationBarHidden(false, animated: true)
searchBar.setShowsCancelButton(false, animated: false)
searchBar.resignFirstResponder()
}
}
1 回答
编辑:我没有登上iOS10并且不了解iMessage应用程序 . 你的问题现在更有意义 . 无论如何,我会将原来的答案留在这里 .
这个项目包含来自apple的原始iMessages应用程序的 basics . 当然还有 a lot more 来调整,但它应该让你开始 .
显示带有消息的表格视图,除非您向上滚动查找它,然后单击导航栏隐藏的搜索栏并且搜索栏也显示取消按钮,否则搜索栏最初是隐藏的 .
如果您想下载整个项目,以便可以看到我如何设置故事板,您可以在此处下载项目 . http://www.filedropper.com/forjeremykelleher