UITableView doesn't show content of the custom cell?
UITableView doesn't show content of the custom cell?
I'm beginner in iOS, so bear with me. I have several problems. First:
When app is launched, search bar should be hidden, and when user swipes down the search bar should appear like this: when the app is launched
Second: When user taps on search bar, the table view should show cells like this: when user taps search bar but for some reason table view won't show the content of said cell, it shows the name of the store in search bar: shows only name of the store(I connected delegate and dataSource). This is my code:
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
}
}
}
This is array of Stores and how I set table view.
//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)
}
}
This two labels are shown upon launching the app below the search bar.
private func setupLabels() {
dataLabel.append(Labels(nameLabel: "Top liked"))
dataLabel.append(Labels(nameLabel: "Top followed"))
currentDataLabel = dataLabel
}
Search text:
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()
// }
}
Classes for stores and labels:
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()
}
}
}
I know, there is a lot of code here, but this is a big app... and this is just part of it. I would really appreciate if someone would take time to help me. Thanks in advance.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Comments
Post a Comment