Why my button in cell (TableView) doesn't work? - uitableview

I don't know why my button in cell doesn't work. Where can be the problem ? It doesn't printing something to console like it isn't tapped.
extension ProfileViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Reservations.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: K.reusableCellReservations, for: indexPath)
as! ReservationCell
cell.restaurantName.text = Reservations[indexPath.row].restaurantName
cell.reseravationTime.text = Reservations[indexPath.row].time
cell.reservationDate.text = Reservations[indexPath.row].date
cell.reservationGuests.text = Reservations[indexPath.row].guests
cell.reservationStatus.text = Reservations[indexPath.row].status
cell.cancelReservation.addTarget(self, action: #selector(btnCellTapped(sender: )), for: .touchUpInside)
return cell
}
#objc func btnCellTapped(sender: UIButton) {
print("pressed")
}
And this is reservation Cell code.
class ReservationCell: UITableViewCell {
#IBOutlet weak var restaurantName: UILabel!
#IBOutlet weak var reseravationTime: UILabel!
#IBOutlet weak var reservationDate: UILabel!
#IBOutlet weak var reservationGuests: UILabel!
#IBOutlet weak var reservationStatus: UILabel!
#IBOutlet weak var cancelReservation: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool {
super.setSelected(selected, animated: animated)
}
#IBAction func btnCellTapped(_ sender: UIButton) {
}
}

Because your code looks fine, we have to start with debugging.
Launch the app and go to the view controller in question
Execute action in Xcode menu: Debug -> View Debugging -> Capture View Hierarchy
Select the button in question
Select 'Object Inspector' in the Inspector Pane (to the right)
Below 'shadow color' you should see the list of target-action information
Check if the list has correct 'target' and 'action' values.
Screenshot for View Debug Hierarchy tool

Related

Swift 5 Collapsible table header

I have to do this implementation, I need a list of cells representing month periods, where each one is collapsed, and when clicked it shows its content, I used two cells prototypes based on some tutorials I found but I'm really new into swift programming, I can't get the expected result, I share some screens and actual code. Hope someone could help me.
class BillingListCell: UITableViewCell{
#IBOutlet weak var billWrapper: UIView!
#IBOutlet weak var billTotal: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
class BillingListHeaderCell: UITableViewCell{
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var numberLabel: UILabel!
#IBOutlet weak var statusButton: UIButton!
func setExpanded() {
statusButton.setImage(UIImage(systemName: "chevron.up"), for: .normal)
}
func setCollapsed() {
statusButton.setImage(UIImage(systemName: "chevron.down"), for: .normal)
}
}
class BillingListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var billingListTableView: UITableView!
var paymentArray: [String] = ["data","data2", "data3"]
private let numberOfActualRowsForSection = 1
func numberOfSections(in tableView: UITableView) -> Int {
return paymentArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// First will always be header
return false ? (1+numberOfActualRowsForSection) : 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if(indexPath.row == 0){
let cell = tableView.dequeueReusableCell(withIdentifier: "BillingListHeaderCell", for: indexPath) as! BillingListHeaderCell
cell.setCollapsed()
return cell
}else{
let cell = tableView.dequeueReusableCell(withIdentifier: "BillingListCell", for: indexPath) as! BillingListCell
cell.billWrapper.layer.cornerRadius = 15
cell.billWrapper.layer.borderWidth = 1
cell.billWrapper.layer.borderColor = UIColor.blue.cgColor
cell.billTotal.text = "1234"
return cell
}
}

Having trouble connecting my CustomCell.xib to my tableView in the storyboard

I Have to do an app for recipes and it shows me different recipes in my tableView, and i just want to implement my CustomCell (from a xib file) to my storyboard and I don't know how to connect it to show my data (I already checked my identifier) here's the code of my controller :
class SearchRecipe: UIViewController, ShowAlert {
var recipeData = RecipeDataModel()
var recipe = [String]()
#IBOutlet weak var tableViewSearch: UITableView!
override func viewDidLoad() {
self.tableViewSearch.rowHeight = 130
}
func updateRecipeData(json: JSON){
if let ingredients = json["hits"][0]["recipe"]["ingredientLines"].arrayObject{
recipeData.ingredientsOfRecipe = ingredients[0] as! String
recipeData.cookingTime = json["hits"][0]["recipe"]["totalTime"].stringValue
recipeData.recipe = json["hits"][0]["recipe"]["label"].stringValue
recipeData.recipeImage = json["hits"][0]["recipe"]["image"].stringValue
}
else {
print("Problem")
}
//self.tableViewSearch.reloadData()
}
}
extension SearchRecipe: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return recipe.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customRecipeCell", for: indexPath) as! CustomRecipeCell
getRecipesDisplay(in: cell, from: recipeData , at: indexPath)
return cell
}
func getRecipesDisplay(in cell: CustomRecipeCell, from recipeModel: RecipeDataModel, at indexPath: IndexPath){
cell.recipeTitle.text = recipeData.recipe
cell.recipeInfos.text = recipeData.ingredientsOfRecipe
cell.timerLabel.text = recipeData.cookingTime
}
}
and this is the code my xib file :
class CustomRecipeCell: UITableViewCell {
#IBOutlet weak var recipeTitle: UILabel!
#IBOutlet weak var recipeInfos: UILabel!
#IBOutlet weak var cellImageBackground: UIImageView!
#IBOutlet weak var likeAndTimerView: UIView!
#IBOutlet weak var likeImage: UIImageView!
#IBOutlet weak var timerImage: UIImageView!
#IBOutlet weak var likeLabel: UILabel!
#IBOutlet weak var timerLabel: UILabel!
#IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func awakeFromNib() {
super.awakeFromNib()
activityIndicator.isHidden = true
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
In ViewDidLoad, you must register your cell this way:
override func viewDidLoad() {
tableViewSearch.register(UINib(nibName:" /* NAME OF YOUR XIB FILE */ ", bundle: nil), forCellReuseIdentifier: "customRecipeCell")
}
You also have to edit the size of your cell in the attribute inspector of your custom cell and of your table view

UITableView with CustomCell not show data Swift4 on Xcode9

I read the other topics, but not solves my problem, I create a tableview and your cells with same value to make tests, but when I execute my project nothing data is showed. I put 5 size of rows to only to test. I don't know why my code not works.
My storyboard:
TableViewCellVenda.swift
class TableViewCellVenda: UITableViewCell {
#IBOutlet weak var cellView: UIView!
#IBOutlet weak var imageCloud: UIImageView!
#IBOutlet weak var labelValorTotal: UILabel!
#IBOutlet weak var labelQtdItens: UILabel!
#IBOutlet weak var labelCliente: UILabel!
#IBOutlet weak var labelDataVenda: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
ViewControllerVendas.swift
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
// UI View
#IBOutlet weak var tableViewVendas: UITableView!
#IBOutlet weak var waitView: UIActivityIndicatorView!
// DB
var db: OpaquePointer?
var count : Int = 0
// Data
var saleOrders : [SaleOrder] = []
override func viewDidLoad() {
super.viewDidLoad()
self.waitView.startAnimating()
self.waitView.hidesWhenStopped = true
tableViewVendas.delegate = self
tableViewVendas.dataSource = self
}
// TableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellVenda") as! TableViewCellVenda
print("Cell for row")
cell.labelCliente.text = "Nome do Cliente"
cell.labelDataVenda.text = "14/02/1991"
cell.labelQtdItens.text = "20"
cell.labelValorTotal.text = "R$ 542,22"
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 75
}
}
Emulator:
Try adding a new swift UIKit file with a class of type UITableView and do all the tableview setup in there. And of coarse set the tables’s class as this UITableView class.

Reset all buttons after pressing the next button

I need to change the image from the button to another after clicking on the button. I did this with the help of setBackgroundImage(). But how do I change the image of all buttons at once and change the image only to the button that is pressed...
![button set][1]
My viewController
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
self.tableView.separatorStyle = .none
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! UIT
cell.TitleLabel.text=dataMas[indexPath.row]
cell.name.text = titleTrack[indexPath.row]
cell.btns.tag = indexPath.row
cell.btns.addTarget(self, action: #selector(ViewController.PlayTrack(_:)), for: UIControlEvents.touchUpInside )
return cell
}
and cell class
import UIKit
class UIT: UITableViewCell {
#IBOutlet weak var btns: UIButton!
#IBOutlet weak var TitleLabel: UILabel!
#IBOutlet weak var name: UILabel!
override func awakeFromNib() {
}
override func setSelected(_ selected: Bool, animated: Bool) {
}
}
try this inside your ViewController.PlayTrack:
var numberOfButtons: Int = 5
#IBOutlet weak var btns: UIButton!
for i in 1...numberOfButtons {
if btns.tag == i {
// SET IMAGE TO BTN HERE.
}
}

Swift -> my prototype cell (UITableViewCell) doesn't show in my UIViewController with a UITableView

My storyboard looks like this
and my code is the following
UIViewController
class DownLoadSoundsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
// MARK: View Controller Properties
let viewName = "DownLoadSoundsViewController"
#IBOutlet weak var visualEffectView: UIVisualEffectView!
#IBOutlet weak var dismissButton: UIButton!
#IBOutlet weak var downloadTableView: UITableView!
// MARK: Properties
var soundPacks = [SoundPack?]() // structure for downloadable sounds
override func viewDidLoad() {
super.viewDidLoad()
downloadTableView.dataSource = self
downloadTableView.delegate = self
downloadTableView.register(DownLoadTableViewCell.self, forCellReuseIdentifier: "cell")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numberOfSoundPacks
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let method = "tableView.cellForRowAt"
//if (indexPath as NSIndexPath).section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "downloadTableViewCell", for: indexPath) as! DownLoadTableViewCell
cell.backgroundColor = UIColor.green
if soundPacks[(indexPath as NSIndexPath).row]?.price == 0 {
cell.soundPackPriceUILabel.text = "FREE"
} else {
cell.soundPackPriceUILabel.text = String(format: "%.2", (soundPacks[(indexPath as NSIndexPath).row]?.price)!)
}
//cell.textLabel?.text = soundPacks[(indexPath as NSIndexPath).row]?.soundPackTitle
cell.soundPackTitleUILabel.text = soundPacks[(indexPath as NSIndexPath).row]?.soundPackTitle
cell.soundPackAuthorUILabel.text = soundPacks[(indexPath as NSIndexPath).row]?.author
cell.soundPackShortDescription.text = soundPacks[(indexPath as NSIndexPath).row]?.shortDescription
cell.soundPackImage.image = UIImage(named: "Placeholder Icon")
DDLogDebug("\(viewName).\(method): table section \((indexPath as NSIndexPath).section) row \((indexPath as NSIndexPath).row))")
return cell
//}
}
UItableViewCell
class DownLoadTableViewCell: UITableViewCell {
#IBOutlet weak var soundPackImage: UIImageView!
#IBOutlet weak var soundPackTitleUILabel: UILabel!
#IBOutlet weak var soundPackAuthorUILabel: UILabel!
#IBOutlet weak var soundPackShortDescription: UILabel!
#IBOutlet weak var soundPackPriceUILabel: UILabel!
let gradientLayer = CAGradientLayer()
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
But I get the following;
I am sure I am doing something small incorrectly, but as of yet can't figure it out. Looked through many examples included my own code where I have gotten this working before.
Not a single one of my settings for the tableview are getting invoked except the number of cells. But everything in;
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{...}
is not working.
Help is appreciated.
I think you need to reload the tableView after getting data from Firebase
self.saveMixesTableView.reloadData()

Resources