iOS Table View click event not working - ios

When i click table view cell click event not working. I am assigned the data to view in custom tableView cell class and passed the value from viewController class. Is there is any problem in assigning data to views in custom table view cell class
ViewController.class
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.ViewAllTableView.dequeueReusableCell(withIdentifier: "ViewAllTableViewCell", for: indexPath) as! ViewAllTableViewCell
let products = self.allProducts[indexPath.row]
cell.setData(products: products)
cell.delegate = self
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "allDetail", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? ProductDetailViewController{
let products = self.allProducts[(ViewAllTableView.indexPathForSelectedRow?.row)!]
destination.productID = products.id
}
}
This is Custom Table View Cell class
ViewAllTableViewCell.class
class ViewAllTableViewCell: UITableViewCell {
#IBOutlet weak var ItemImage: UIImageView!
#IBOutlet weak var ItemName: UILabel!
#IBOutlet weak var ItemOfferPrice: UILabel!
#IBOutlet weak var ItemOriginalPrice: UILabel!
#IBOutlet weak var ItemWeight: UILabel!
#IBOutlet weak var ItemCountLabel: UILabel!
var delegate : ViewAllTableViewCellDelegate?
var allProduct: ViewAllProductsData!
func setData(products: ViewAllProductsData){
self.allProduct = products
self.ItemName.text = allProduct.name
self.ItemWeight.text = "\(allProduct.quantity) \(allProduct.unit)"
self.ItemOfferPrice.text = "\(allProduct.price)"
self.ItemOriginalPrice.text = "\(allProduct.originalPrice)"
self.ItemCountLabel.text = "\(allProduct.count)"
let url: URL = NSURL(string: allProduct.image)! as URL
self.ItemImage.af_setImage(withURL: url)
}
#IBAction func ViewAllMinusButton(_ sender: UIButton) {
delegate?.minusCount(data: allProduct)
}
#IBAction func ViewAllPlusbutton(_ sender: UIButton) {
delegate?.addCount(data: allProduct)
}
}
protocol ViewAllTableViewCellDelegate{
func addCount(data: ViewAllProductsData)
func minusCount(data: ViewAllProductsData)
}

It happens because either you haven't conform properly to UITableViewDelegate or you have UITapGestureRecognizer somewhere in your view controller.

It worked for me after changing tableView attribute selection from no selection to single selection

Related

Manipulating from grandchild view controller - Swift Xcode

This is my first time using swift or Xcode.
I'm trying to make a simple transaction register app
The first view has a table, in which each row represents an account and it's balance.
When you click on a row, it opens up a second view, via segue, which contains a table of all transactions for that account. At the top of this view there is an 'Add Transaction' button, which opens up a third view, that has a form and an 'Add' button. When the 'Add' button is pressed, I use the .reloadData() on the table in the second view and the third view is dismissed.
But, the table, visually, does not have an additional row in it. Which is because after the 3rd view closes, the newly added transaction is no longer in the transactions array.
Am I doing something wrong? My attempt and images are below.
First view
import UIKit
class AccountsViewController: UIViewController {
#IBOutlet weak var newAccountNameUITextField: UITextField!
#IBOutlet weak var newAccountBalanceUITextField: UITextField!
#IBOutlet weak var addNewAccountUIButton: UIButton!
#IBOutlet weak var accountsUITableView: UITableView!
var selectedAccount: Account = Account(name: "", balance: "")
var accounts = [Account(name: "PNC", balance: "45.93")]
override func viewDidLoad() {
super.viewDidLoad()
accountsUITableView.delegate = self
accountsUITableView.dataSource = self
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
if let transactionsViewController = segue.destination as? TransactionsViewController {
transactionsViewController.modalPresentationStyle = .fullScreen
transactionsViewController.account = selectedAccount
}
}
}
extension AccountsViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedAccount = accounts[indexPath.row]
performSegue(withIdentifier: "trasactionsSegue", sender: self)
}
}
extension AccountsViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return accounts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "account", for: indexPath) as! AccountCell
cell.selectionStyle = .none
cell.nameUILabel?.text = accounts[indexPath.row].name
cell.balanceUILabel?.text = accounts[indexPath.row].balance
return cell
}
}
Second View
import UIKit
class TransactionsViewController: UIViewController {
#IBOutlet weak var nameUILabel: UILabel!
#IBOutlet weak var TransactionsUITableView: UITableView!
#IBOutlet weak var balanceUILabel: UILabel!
var account: Account = Account(name: "", balance: "", transactions: [])
override func viewDidLoad() {
super.viewDidLoad()
TransactionsUITableView.dataSource = self
nameUILabel.text = account.name
balanceUILabel.text = account.balance
}
//Pass data to newTransactionViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
if let newTransactionViewController = segue.destination as? NewTransactionViewController {
newTransactionViewController.account = account
}
}
//Dismiss this view when Accounts button is pressed
#IBAction func backToAccountsTouchUpInside(_ sender: UIButton) {
self.dismiss(animated: true, completion: {
self.presentingViewController?.dismiss(animated: true, completion: nil)
})
}
#IBAction func addTransactionTouchUpInside(_ sender: UIButton) {
performSegue(withIdentifier: "addTransactionSegue", sender: self)
}
#IBAction func unwindToViewControllerA(segue: UIStoryboardSegue) {
DispatchQueue.global(qos: .userInitiated).async {
DispatchQueue.main.async {
//At this point the newly added transaction is missing
self.TransactionsUITableView.reloadData()
}
}
}
}
extension TransactionsViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return account.transactions.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "transaction", for: indexPath) as! TransactionCell
cell.selectionStyle = .none
cell.descriptionUILabel.text = account.transactions[indexPath.row].description
cell.amountUILabel.text = account.transactions[indexPath.row].amount
cell.balanceUILabel.text = account.transactions[indexPath.row].balanceAfterAmount
return cell
}
}
Third View
import UIKit
class NewTransactionViewController: UIViewController {
#IBOutlet weak var clearedUISegmentedControl: UISegmentedControl!
#IBOutlet weak var depositingUISegmentedControl: UISegmentedControl!
#IBOutlet weak var descriptionUITextField: UITextField!
#IBOutlet weak var amountUITextField: UITextField!
#IBOutlet weak var addTransactionUIButton: UIButton!
var account: Account? = nil
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func addTransactionTouchUpInside(_ sender: UIButton) {
let depositing = depositingUISegmentedControl.selectedSegmentIndex == 0 ? true : false
let cleared = clearedUISegmentedControl.selectedSegmentIndex == 0 ? true : false
let description = descriptionUITextField.text
let amount = amountUITextField.text
let balanceAfterAmount = operationOnCurrency(depositing: depositing, amount: amount!, balance: account!.balance)
let newTransaction = Transaction(depositing: depositing, amount: amount!, balanceAfterAmount: balanceAfterAmount, description: description!, cleared: cleared)
account?.transactions.append(newTransaction)
self.performSegue(withIdentifier: "backToTransactions", sender: self)
}
}
func operationOnCurrency (depositing: Bool, amount: String, balance: String) -> String {
//Return empty string for now
return ""
}
The problem is that you are appending a new Transaction in the Account instance that was created in your NewTransactionViewController, rather than updating the data in the instance held by the TransactionsViewController or the root data source in the AccountsViewController (assuming that is the root data source). You need to pass the updated data backwards when the add button is pressed. You can create a delegate protocol to take care of this. Using your transition from NewTransactionViewController to TransactionsViewController example, first create the protocol:
protocol NewTransactionDelegate {
func transactionAddedToAccount(account: Account)
}
Then inside of your NewTransactionViewController you will want to create a delegate property:
class NewTransactionViewController: UIViewController {
#IBOutlet weak var clearedUISegmentedControl: UISegmentedControl!
#IBOutlet weak var depositingUISegmentedControl: UISegmentedControl!
#IBOutlet weak var descriptionUITextField: UITextField!
#IBOutlet weak var amountUITextField: UITextField!
#IBOutlet weak var addTransactionUIButton: UIButton!
var account: Account? = nil
**var delegate: NewTransactionDelegate?**
override func viewDidLoad() {
super.viewDidLoad()
}
And inside of your addTransactionTouchUpInside method call the delegate method:
#IBAction func addTransactionTouchUpInside(_ sender: UIButton) {
let depositing = depositingUISegmentedControl.selectedSegmentIndex == 0 ? true : false
let cleared = clearedUISegmentedControl.selectedSegmentIndex == 0 ? true : false
let description = descriptionUITextField.text
let amount = amountUITextField.text
let balanceAfterAmount = operationOnCurrency(depositing: depositing, amount: amount!, balance: account!.balance)
let newTransaction = Transaction(depositing: depositing, amount: amount!, balanceAfterAmount: balanceAfterAmount, description: description!, cleared: cleared)
account?.transactions.append(newTransaction)
**delegate?.transactionAddedToAccount(account: account)**
self.performSegue(withIdentifier: "backToTransactions", sender: self)
}
Now back in your TransactionsViewController you will want to conform to the NewTransactionDelegate protocol and implement the required method declared in the protocol:
class TransactionsViewController: UIViewController, NewTransactionDelegate {
func transactionAddedToAccount(account: Account) {
self.account = account
tableView.reloadData()
}
Then when you perform the segue to transition from TransactionsViewController to NewTransactionViewController you will want to set the destination view controller's delegate property to self:
//Pass data to newTransactionViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
if let newTransactionViewController = segue.destination as? NewTransactionViewController {
**newTransactionViewController.delegate = self**
newTransactionViewController.account = account
}
}
Now when the add button is tapped the delegate method is called and passed the new instance of account, which is then passed back to the previous view controller and updated.
Note that this will only update in account instance in the TransactionsViewController and you will also need to update the data for this account at the source or it will be lost when TransactionsViewController is deallocated. Pass the new account back to AccountsViewController, save to device, update database, etc.

How to make selected cell turn a different color for custom cells UITableViewCell

I want to make a selected cell a specific color (UIColor.systemGray). I've tried almost every answer out there but for some reason the way that my cells are it isn't working.
Here is part of my view controller class:
extension CommunicationViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return communicationcells.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let communicationCell = communicationcells[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "CommunicationCell") as! CommunicationCell
cell.backgroundButton.tag = indexPath.row
cell.backgroundButton.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
cell.setCommunication(communication: communicationCell)
return cell
}
struct Holder {
static var _myComputedProperty:Int = -1
}
var myComputedProperty:Int {
get {
return Holder._myComputedProperty
}
set(newValue) {
Holder._myComputedProperty = newValue
}
}
#objc func buttonTapped(_ sender: UIButton) {
myComputedProperty = sender.tag
performSegue(withIdentifier: "CommunicationSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let detailsController = segue.destination as! CommunicationDetailsViewController
detailsController.passthroughstring = String(myComputedProperty)
}
}
And here is my communication cell controller:
class CommunicationCell: UITableViewCell {
#IBOutlet var backgroundcontentView: UIView!
#IBOutlet weak var CommunicationType: UILabel!
#IBOutlet weak var subject: UILabel!
#IBOutlet weak var partnerType: UILabel!
#IBOutlet weak var partnerID: UILabel!
#IBOutlet weak var effectiveFrom: UILabel!
#IBOutlet weak var effectiveTo: UILabel!
#IBOutlet weak var downtimeFrom: UILabel!
#IBOutlet weak var downtimeTo: UILabel!
#IBOutlet weak var backgroundButton: UIButton!
#IBOutlet var collectionOfCommunicationLabels: Array<UILabel>!
func applyTheme() {
for i in 0..<collectionOfCommunicationLabels.count {
collectionOfCommunicationLabels[i].textColor = Theme.current.textColor
collectionOfCommunicationLabels[i].backgroundColor = Theme.current.backgroundColor
}
backgroundcontentView.backgroundColor = Theme.current.backgroundColor
CommunicationType.textColor = Theme.current.textColor
subject.textColor = Theme.current.textColor
partnerType.textColor = Theme.current.textColor
partnerID.textColor = Theme.current.textColor
effectiveFrom.textColor = Theme.current.textColor
effectiveTo.textColor = Theme.current.textColor
downtimeFrom.textColor = Theme.current.textColor
downtimeTo.textColor = Theme.current.textColor
// backgroundButton.backgroundColor = Theme.current.backgroundColor
}
func setCommunication(communication: Communication) {
CommunicationType.text = communication.communicationType
subject.text = communication.subject
partnerType.text = communication.partnerType
partnerID.text = communication.partnerID
effectiveFrom.text = communication.effectiveFrom
effectiveTo.text = communication.effectiveTo
downtimeFrom.text = communication.downtimeFrom
downtimeTo.text = communication.downtimeTo
applyTheme()
}
}
I've tried almost every answer on stack overflow and nothing has worked for me.
Can you please try to implement the didSelectRowAtIndexPath method in addition to the cellForRowAtIndexPath method? In that case, you will not need a background button and also you will get a callback in the didSelectRowAtIndexPath method on tapping any row in the tableview.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedCell = tableView.cellForRow(at: indexPath)
selectCell.backgroundColor = .red //change to any color you want it to change to on selection
performSegue(withIdentifier: "CommunicationSegue", sender: self)
}

What is the missing command to carry the cell title from UITableView to new ViewController via this segue?

I have ViewController that contains a UITableView. Data is loaded into that table via the following code:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UsernameSentDelegate {
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var receiveUsername: UILabel!
#IBOutlet weak var userEmailText: UILabel!
var userEmail: String?
var communities = [String]() { didSet { communitiesTableView.reloadData()
}
}
var flag = false
#IBOutlet weak var communitiesTableView: UITableView!
#IBAction func unwindToHome(_ segue: UIStoryboardSegue) {
}
//recieves email address from delegate from LoginViewController
func userLoggedIn(data: String) {
userEmailText.text = data
}
override func viewDidLoad() {
super.viewDidLoad()
self.communitiesTableView.delegate = self
self.communitiesTableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.communities.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let title = self.communities[indexPath.row]
let cell = UITableViewCell()
cell.textLabel?.text = title
return cell
}
I then set up 1 prototype cell within the UITableView so I could create a segue to my second view controller, ShowCommunitiesViewController and named this segue, "showCommunitySegue"
In ShowCommunitiesViewController I have a label set up and ready to use as the title of the cell name carried across, named communityName.
In ViewController I have set up the following function to deal with the segue, including the destination variable for the cell title that has been selected.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "showCommunitySegue", sender: self)
showCommunityController.communityName = //what do I put here?
}
What do I need to put on that last line so showCommunityController.communityName displays the cell title?
Just declare selectedCellTitle as String in your viewController where your cells are.
var selectedCellTitle: String?
This will be the global variable keeping track of the selected cell's title.
Add the following in didSelectRowAt:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Set your global variable to the title
self.selectedCellTitle = self.communities[indexPath.row]
// Trigger your segue
performSegue(withIdentifier: "showCommunitySegue", sender: self)
}
Override prepareforsegue method the following way:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showCommunitySegue" {
// Check if the segue's destination viewcontroller is your viewcontroller
if let showCommunityController = segue.destination as? ShowCommunityViewController {
// Assign the selected title to communityName
showCommunityController.communityName = self.selectedCellTitle
}
}
}

Button on tableview cell is not working - swift

I have a button on my custom tableview cell that is showing the users name. When you click on it, you should be taken to his/her profile but nothing happens?
Here is my custom tableview cell class (commentsTableViewCell.swift) :
import UIKit
protocol commentsTableViewCellDelegate {
func namesIsTapped(cell: commentsTableViewCell)
}
class commentsTableViewCell: UITableViewCell {
#IBOutlet weak var nameButton: UIButton!
#IBOutlet weak var commentLabel: UILabel!
#IBOutlet weak var profilePic: UIImageView!
#IBOutlet weak var dateLabel: UILabel!
#IBOutlet weak var uidLabel: UILabel!
var delegate: commentsTableViewCellDelegate?
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
}
#IBAction func nameIsTapped(sender: AnyObject) {
//4. call delegate method
//check delegate is not nil
if let _ = delegate {
delegate?.namesIsTapped(self)
} else {
print("Delegate is \(delegate)")
}
}
}
Here is the important part of commentsTableViewController.swift:
class commentsTableViewController: UITableViewController, commentsTableViewCellDelegate, UITextViewDelegate {
#IBOutlet weak var commentLabel: UITextView!
#IBOutlet weak var theLabel: UILabel!
var dataGotten = "Nothing"
var updates = [CommentSweet]()
func namesIsTapped(cell: commentsTableViewCell) {
//Get the indexpath of cell where button was tapped
//let indexPath = self.tableView.indexPathForCell(cell)
let allDataSend = cell.nameButton.titleLabel?.text!
self.performSegueWithIdentifier("toDetailtableViewController", sender: allDataSend)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toDetailtableViewController" {
if let nextVC = segue.destinationViewController as? theProfileTableViewController {
nextVC.viaSegue = sender! as! String
}
}
}
override func viewDidLoad() {
Kindly set the delegate to self.
In cellforRowIndexPath
commentsTableViewCell.delegate = self
Where commentsTableViewCell is the custom UITableViewCell object
I suggest to handle the action of the cell's button in the viewController. You can recognize which button has been tapped by setting a tag for it.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! TableViewCell
cell.myButton?.tag = indexPath.row
cell.myButton?.addTarget(self, action: #selector(), for: .touchUpInside)
return cell
}
func namesIsTapped(tappedButton: UIButton) {
// get the user (from users array for example) by using the tag, for example:
let currentUser = users[tappedButton.tag]
// do whatever you want with this user now...
}

Swift Emptying array when returning from tableview

In first VC I have an array with images (which I picked - array.append) which goes over prepareforsegue to another VC with tableview, and tableview reads that array and everything works, however when going back from tableview to first VC to pick another set of images (array.append) tableview is populating cells with set of previous picked images, because array have previous picked images. How could I make that tableview remember only the last added images.
ViewController
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var image1: UIImageView!
#IBOutlet weak var image2: UIImageView!
#IBOutlet weak var image3: UIImageView!
#IBOutlet weak var label1: UILabel!
#IBOutlet weak var label2: UILabel!
#IBOutlet weak var label3: UILabel!
var array = [UIImage]()
var array2 = [String]()
var number = 0
#IBAction func gamb(sender: UIButton) {
}
#IBAction func gamb2(sender: UIButton) {
array.append(self.image2.image!)
array2.append(self.label2.text!)
}
#IBAction func gamb3(sender: AnyObject) {
array.append(self.image1.image!)
array2.append(self.label1.text!)
}
#IBAction func gamb4(sender: UIButton) {
array.append(self.image3.image!)
array2.append(self.label3.text!)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destVC = segue.destinationViewController as! TableViewController
let priljepak = self.array
destVC.array = priljepak
let priljepak2 = self.array2
destVC.array2 = priljepak2
}
}
TableViewController
import UIKit
class TableViewController: UIViewController,UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
var array = [UIImage]()
var array2 = [String]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.imageView?.image = array[indexPath.row]
cell.textLabel!.text = array2[indexPath.row]
return cell
}
}
In the first ViewController, override viewWillAppear. Call super and empty the arrays.
override func viewWillAppear(animated:bool) {
super.viewWillAppear(animated)
array = [UIImage]()
array2 = [String]()
}
This'll cause any selection to be removed when returning to the first VC.
Insert the following override into your ViewController:
override func viewWillAppear(animated: Bool){
super.viewWillAppear(animated)
array.removeAll()
array2.removeAll()
}
This will clean the arrays in your first view controller, when TableViewController is dismissed.

Resources