UITableView row action is not called - ios

I created UITableView with 1 section and 3 rows in it. Text in each row is hardcoded and I need to call specific actions by tapping each row.
Why UITableView delegae method func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) is not called in following code?
#IBOutlet weak var tbvSetting: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tbvSetting.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell
switch indexPath.row {
case 0:
cell.textLabel?.text = "Countries"
case 1:
cell.textLabel?.text = "Managers"
case 2:
cell.textLabel?.text = "Customers"
default:
break
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath.row)
}
Thanks for hints.

Setup up self as the delegate of the tableView.
You can do it in Storyboard(which seems like you are using) or in code as:
override func viewDidLoad() {
super.viewDidLoad()
tbvSetting.delegate = self
tbvSetting.reloadData()
}
Since, this has not been setup yet, even though tableView cells are getting selected, the OS is unaware of who is the handler of the selection gesture.

Related

Is it possible to give UITableViewCells each a unique ViewController?

To expand, I have a UITableView of two cells, and I want to give each of the cells, when tapped, their own unique view controller (as their functions will be different). I have scoured this website, as well as Google and could not quite find an answer. Here's the set up of my UITableView:
class ViewController: UITableViewController {
let functions = ["Rule of 72","Future Value", "Simple Interest"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = self.functions[indexPath.row]
return cell
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
}
Ultimately, the idea is to build an app that allows for the calculation of certain financial and economic equations. These equations will require different inputs and outputs (Rule of 72 will only need one UITextField, whereas Future Value will need four UITextField's).
I would suggest creating a enum confirming to CaseIterable. Also implement the func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath).
class ViewController: UITableViewController {
enum OptionTypes: CaseIterable {
case ruleOf72
case futureValue
case simpleInterest
var description: String {
switch self {
case .ruleOf72: return "Rule of 72"
case .futureValue: return "Future Value"
case .simpleInterest: return "Simple Interest"
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let item = OptionTypes.allCases[indexPath.row]
cell.textLabel?.text = item.description
return cell
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return OptionTypes.allCases.count
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let item = OptionTypes.allCases[indexPath.row]
switch item {
case .ruleOf72:
navigateToRuleOf72ViewController()
case .futureValue:
navigateToFutureValueViewController()
case .simpleInterest:
navigateToSimpleInterestViewController()
}
}
private func navigateToRuleOf72ViewController() {}
private func navigateToFutureValueViewController() {}
private func navigateToSimpleInterestViewController() {}
}

Cells in table view not responding

I am working on a todo list app and whenever I run it on the simulator and try to print the items in my array, the other cells item get printed.
Here's my code:
import UIKit
class TodoListViewController: UITableViewController {
let itemArray = ["math","english"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
// DATASOURCE METHODS
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoItemCell", for: indexPath)
cell.textLabel?.text = itemArray[indexPath.row]
return cell
}
// DELEGATE METHODS
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
print(itemArray[indexPath.row])
}
}
You need didSelectRowAt not didDeselectRowAt , the latter is triggered when you select a row so you get the print from the previous selected row
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(itemArray[indexPath.row])
}

TableView won't show data at all

Trying to crate a simple tableView that show data but it's not working. I checked and it's not even get into func tableView .. Any ideas what can be the problem?
import UIKit
class ViewController: UIViewController,UITableViewDelegate , UITableViewDataSource , UITextFieldDelegate {
let messageArray = ["First message ", "sec message " , "Third message"]
#IBOutlet weak var customTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
customTableView.delegate = self
customTableView.dataSource = self
customTableView.register(UINib(nibName: "customCell" , bundle: nil), forCellReuseIdentifier: "tableCell")
configureTableView()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell"
, for: indexPath)as! custom
cell.label0.text = messageArray[0]
cell.label1.text = messageArray[1]
cell.label2.text = messageArray[2]
cell.label3.text = "d"
customTableView.reloadData()
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func configureTableView() {
customTableView.rowHeight = UITableViewAutomaticDimension
customTableView.estimatedRowHeight = 120.0
}
}
Try deleting the reload data from cellForRowAt and implementing
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
I think you have a recursive reload becuase you have this in cellForRowAt
customTableView.reloadData()
so comment it and try
Register cell before set delegate and data source.
Remove customTableView.reloadData() in func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
Simply remove this line: customTableView.reloadData() from datasource method cellForRowAt.
You are basically asking the tableview to reload all of it's data while it is getting the first row from the datasource method. So it's an endless loop. It never gets to show any data.

How to show UITableView Cell in UITableView without array?

I have this code:
class UserProfilViewController: UIViewController {
// Outlets
#IBOutlet weak var userProfileTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.navigationItem.title = "Profil"
}
}
// MARK: - Table View Data Source
extension UserProfilViewController {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "UserProfilCell", for: indexPath)
return cell
}
}
My project in bitbucket: https://bitbucket.org/trifek/karta-nauka/src/master/
I placed one tableviewcell cell on the tableview (UserProfil.storyboard). I have a form on it that I would like to display in this cell. The problem is the cell does not display. Does anyone know how to fix it?
As per the code you have shared, Please change your code to following.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "UserProfilCell", for: indexPath) as! UserProfilTableViewCell
return cell
}
Let me know in case of any queries.
IMHO, first try to clear your requirements. If you want to display fix number of cells then you can simply use static cells. If your cells are dynamic i.e their number depends on some calculation or other logic, then you can use dynamic cell. While using dynamic cell, verify if you have registered it or not (if you are using xib for cell) and also verify for the cell identifier.
#Lukasz
Please use the below code for this.
class UserProfileViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setUIComponents()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
private func setUIComponents(){
registerNibs()
}
}
extension UserProfileViewController: UITableViewDelegate, UITableViewDataSource{
internal func registerNibs(){
tableView.register(UINib(nibName: String(describing: UserProfileTableCell.self), bundle: Bundle.main), forCellReuseIdentifier: kUserProfileCellReuseId)
}
//MARK: TableView Methods -
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let sessionCell: UserProfileTableCell.self = tableView.dequeueReusableCell(withIdentifier: kUserProfileCellReuseId, for: indexPath) as! UserProfileTableCell.self
cell.titleLabel.text = “TEST”
return sessionCell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
}
class UserProfileTableCell: UITableViewCell {
//Set the "kUserProfileCellReuseId" in nib to register identifier.
let kUserProfileCellReuseId = "kUserProfileCellReuseId"
//MARK: - Override Methods
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
setUIComponents()
}
private func setUIComponents(){
}
}
You never declare that your view controller conforms to the UITableViewDataSource or UITableViewDelegate protocols. Given that you don't do that, you would not be able to set your view controller as the data source or delegate of your table view.

how handle click cell in UITableViewController

I would like to handle click each cell in my controller but my log doesn't show anything !
class RightMenuController: UITableViewController {
let row_items = ["دسته بندی", "ثبت نام/ورود"]
override func viewDidLoad() {
navigationController?.navigationBar.isHidden = true
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return row_items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)
cell.textLabel?.text = row_items[indexPath.row]
return cell
}
/*
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
*/
/*
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
*/
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print(self.row_items[indexPath.row]) // not print anything
}
}
Try this instead of your didSelectRow-method:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(self.row_items[indexPath.row])
}
The "method signature" (function name) that you are using for the didSelect method is incorrect. What you have listed in your current implementation is the "Swift 2 Version". You might note the way that the argument labels are separated to make the functions a bit more readable like in tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
This same renaming style should be applied to didSelectRowAtIndexPath. Here is the full code you can use to test this out in a playground.
import UIKit
class RightMenuController: UITableViewController {
let row_items = ["دسته بندی", "ثبت نام/ورود"]
override func viewDidLoad() {
// We must register a cell manually since there is no storyboard
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "LabelCell")
navigationController?.navigationBar.isHidden = true
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return row_items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)
cell.textLabel?.text = row_items[indexPath.row]
return cell
}
// Old Swift 2 method signature
//func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(self.row_items[indexPath.row]) // not print anything
}
}
// Create the ViewController
let right = RightMenuController(style: .plain)
// Ask the Playground to show your ViewController in the Assistant (2 rings - Right side view)
import PlaygroundSupport
PlaygroundPage.current.liveView = right

Resources