我是iOS的初学者,请多多包涵。我有几个问题。第一: 启动应用程序时,搜索栏应处于隐藏状态,而当用户向下滑动时,搜索栏应显示为:when the app is launched 第二:当用户点击搜索栏时,表格视图应显示以下单元格:when user taps search bar,但由于某种原因,表格视图将不显示该单元格的内容,它会在搜索栏中显示商店名称: shows only name of the store(我连接了委托和数据源)。这是我的代码:
import UIKit
import Foundation
class LOSocialWallPublicViewController: UIViewController, UISearchBarDelegate {
//MARK: Variables
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
var searchController = UISearchController()
var suggestionsTableView: UITableView?
var hidesSearchBarWhenScrolling = true
var storeTableView: UITableView?
var dataArray = [Stores]()
var currentDataArray = [Stores]()
var dataLabel = [Labels]()
var currentDataLabel = [Labels]()
var filtered:[String] = []
var searchActive = false
var cellSelected = 0
//MARK: - Outlets
@IBOutlet weak var moreInfoButton: UIButton!
@IBOutlet weak var homePageButton: UITabBarItem!
@IBOutlet weak var addButton: UITabBarItem!
@IBOutlet weak var getMoreButton: UITabBarItem!
@IBOutlet weak var fourthItemTabBarButton: UITabBarItem!
@IBOutlet weak var commentButton: UITabBarItem!
@IBOutlet weak var slideView: UIView!
@IBOutlet weak var allPostsButton: UIButton!
@IBOutlet weak var topFollowedButton: UIButton!
@IBOutlet weak var topLikedButton: UIButton!
@IBOutlet weak var leftSlidViewConstraint: NSLayoutConstraint!
@IBOutlet weak var menuView: UIView!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar:UISearchBar!
//MARK: - LifeCycle
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.register(UINib(nibName: "LOCustomSocialWallTableViewCell", bundle: nil), forCellReuseIdentifier: "customCell")
tableView.backgroundColor = UIColor(hex: 0xEFEFEF)
searchBarTextDidBeginEditing(searchBar)
setupLabels()
setupStores()
//MARK: - Search bar
searchBar.delegate = self
searchBar.isTranslucent = true
searchBar.placeholder = "Search by markers"
searchBar.tintColor = UIColor.black
searchBar.barTintColor = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1)
searchBar.isOpaque = false
searchBar.searchTextPositionAdjustment = UIOffset(horizontal: 10.0, vertical: 0.0)
searchBar.setImage(UIImage(named: "search"), for: .search, state: .normal)
if let textField = searchBar.value(forKey: "searchField") as? UITextField {
textField.textColor = UIColor.black
textField.font = UIFont(name: "Montserrat", size: 14)
textField.clipsToBounds = true
textField.frame.size.height = 30
if let backgroundView = textField.subviews.first {
backgroundView.backgroundColor = UIColor.white
backgroundView.layer.cornerRadius = 17
backgroundView.clipsToBounds = true
}
}
}
这是商店数组以及我如何设置表格视图。
//MARK: - Search stores
private func setupStores() {
dataArray.append(Stores(name: "Zara", image: "zara"))
dataArray.append(Stores(name: "Amadeus", image: "amadeus"))
dataArray.append(Stores(name: "Europa 92", image: "europa 92"))
dataArray.append(Stores(name: "Bershka", image: "bershka"))
dataArray.append(Stores(name: "Levi's", image: "levis"))
dataArray.append(Stores(name: "Pull&Bear", image: "pull&bear"))
dataArray.append(Stores(name: "Sir Oliver", image: "s.Oliver"))
currentDataArray = dataArray
}
//MARK: - Suggestions TableView
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
if suggestionsTableView == nil {
suggestionsTableView?.isHidden = false
suggestionsTableView = UITableView(frame: CGRect(x: 0, y: 185, width: UIScreen.main.bounds.width, height: 168), style: .plain)
suggestionsTableView?.dataSource = self
suggestionsTableView?.delegate = self
suggestionsTableView?.isScrollEnabled = false
self.view.insertSubview(suggestionsTableView!, aboveSubview: tableView!)
suggestionsTableView?.register(UINib(nibName: "LOSearchLabelsTableViewCell", bundle: nil), forCellReuseIdentifier: "searchLabel")
suggestionsTableView?.register(UINib(nibName: "LOSocialWallSearchTableViewCell", bundle: nil), forCellReuseIdentifier: "searchCell")
} else if storeTableView == nil {
storeTableView?.reloadData()
suggestionsTableView?.isHidden = true
storeTableView = UITableView(frame: CGRect(x: 0, y: 185, width: UIScreen.main.bounds.width, height: 450), style: .plain)
storeTableView?.dataSource = self
storeTableView?.delegate = self
self.view.insertSubview(storeTableView!, aboveSubview: tableView!)
storeTableView?.register(UINib(nibName: "LOSocialWallSearchTableViewCell", bundle: nil), forCellReuseIdentifier: "searchCell")
storeTableView?.register(UINib(nibName: "LOSearchLabelsTableViewCell", bundle: nil), forCellReuseIdentifier: "searchLabel")
self.searchBar.setImage(UIImage(named: "searchActive"), for: .search, state: .normal)
}
}
启动搜索栏下方的应用程序时会显示这两个标签。
private func setupLabels() {
dataLabel.append(Labels(nameLabel: "Top liked"))
dataLabel.append(Labels(nameLabel: "Top followed"))
currentDataLabel = dataLabel
}
搜索文字:
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
// if tableView == self.storeTableView {
dataArray = currentDataArray.filter({ store -> Bool in
guard let text = searchBar.text else {return false}
return store.name.contains(text)
})
if(dataArray.count != 0) {
searchActive = true
} else {
searchActive = false
}
if searchText.isEmpty {
self.storeTableView?.reloadData()
searchActive = true
storeTableView?.isHidden = false
}
self.storeTableView?.reloadData()
// }
}
商店和标签的类别:
class Stores {
let name: String
let image: String
init(name: String, image: String) {
self.name = name
self.image = image
}
}
class Labels {
let nameLabel: String
init(nameLabel: String) {
self.nameLabel = nameLabel
}
}
}
UITableViewDelegate:
//MARK: - UITableView delegate and datasource
extension LOSocialWallPublicViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.tableView {
return 5
} else if tableView == self.storeTableView {
// if (searchActive) {
// if currentDataArray.isEmpty {
return dataArray.count
// }
// return currentDataArray.count
// } else {
//
// return currentDataLabel.count
//
// }
}
return dataArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == self.tableView {
let socialCell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as? LOCustomSocialWallTableViewCell
return socialCell!
} else {
if tableView == self.storeTableView && (searchActive) {
if let cell = tableView.dequeueReusableCell(withIdentifier: "searchCell", for: indexPath) as? LOSocialWallSearchTableViewCell {
if indexPath.isEmpty {
cell.searchLabel?.text = dataArray[indexPath.row].name
cell.searchImageView.image = UIImage(named: dataArray[indexPath.row].image)
} else {
cell.searchLabel?.text = currentDataArray[indexPath.row].name
cell.searchImageView.image = UIImage(named: currentDataArray[indexPath.row].image)
return cell
}
}
} else if let c = tableView.dequeueReusableCell(withIdentifier: "searchLabel", for: indexPath) as? LOSearchLabelsTableViewCell {
if tableView == self.suggestionsTableView {
if indexPath.row == 0 {
c.topLabel.text = dataLabel[indexPath.row].nameLabel
return c
} else if indexPath.row == 1 {
c.topLabel.text = currentDataLabel[indexPath.row].nameLabel
return c
}
}
}
}
return UITableViewCell()
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView == self.tableView {
LOControllerHelper.showSinglePostViewController()
suggestionsTableView?.isHidden = true
storeTableView?.isHidden = true
} else if tableView == self.storeTableView {
cellSelected = indexPath.row
searchBar.text = currentDataArray[indexPath.row].name
searchBar.resignFirstResponder()
storeTableView?.reloadData()
}
}
}
我知道,这里有很多代码,但这是一个很大的应用程序……而这只是其中的一部分。如果有人愿意花时间帮助我,我将不胜感激。预先感谢。