Segue is not working from static UITableView - ios

I have a TableViewController with static cells
I tried both creating segue from TableViewController and TableView cell (Not at the same time)
However, in both scenario, didSelectRowAtIndexPath was not fired
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("select")
}
I also have embedded collectionviewcontroller
class EventDetail: UITableViewController, UITextFieldDelegate, UICollectionViewDataSource, UICollectionViewDelegate {
What may be causing this?

Check your tableview selection must be single selection, if it is "No selection" then didSelectedRowAtIndex would not get called.
You can download sample code and observe it.
Sample download
Also check
Ideally your cellForRowAtIndex should be like this
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell : CustomCell! = tableView.dequeueReusableCellWithIdentifier("ID_CustomCell", forIndexPath: indexPath) as! CustomCell
cell.selectionStyle = UITableViewCellSelectionStyle.None
cell.lblData.text = "CustomCell....."
return cell
}
Swift 4 code
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : CustomCell! = tableView.dequeueReusableCell(withIdentifier: "ID_CustomCell", for: indexPath) as! CustomCell
cell.selectionStyle = .none
cell.lblData.text = "CustomCell....."
return cell
}

call performseguewithidentifier from didselectRow if you have created segue from tableviewcontroller. like,
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("select")
self.performSegueWithIdentifier("your segue identifier", sender: self)
}
hope this will help :)

Related

How to make selection style in UITableViewCell

I created Xib file for my uitableviewcell in the attributed inspector i choose SelectionStyle default, but it didn't help me. So and i added accessory type .detailDisclosureButton.
So when i tapped on a cell , My cell doesn't highlighted gray color, only disclosurebutton does
Maybe you can try this one,
extension CWScoreBoardViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
}
or you can try this,
extension CWScoreBoardViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CWUsersTableViewCell", for: indexPath) as! CWUsersTableViewCell
cell.selectionStyle = .none
return cell
}
}

TableViewCell Unresolved Identifier when creating cell

in my Xcode Project, I have the main storyboard set up with a table view and a prototype cell with the identifier set to "cell". The table view has a data source outlet and a delegate outlet.
Yet, my build crashes every time because of an "unresolved identifier" when I create the table view in line 11 (see code below).
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
let array = createArray()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return(validDeck.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
cell = UITableViewCell(style: UITableViewStyle.default, reuseIdentifier: "cell") |"ERROR: Unknown Identifier 'cell' "
cell.textlabel?.text = ""
return cell
}
Does anybody know this error and is willing to help?
Use below code for swift 3.*
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return validDeck.count // assuming count must be greater then 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
cell.textlabel?.text = "Cell tittle"
return cell
}
You never initialize UITableViewCell instances directly.
In viewDidLoad:
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
and then:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for indexPath: indexPath)
cell.textlabel?.text = ""
return(cell)
}

Swift 3 error with tableView

An error keeps occurring even changing the new swift3.0 syntax like removing the NS prefixes and other. The error says
cannot call value of non-function type UITableView
I'm glad if I could have a hint for solving this problem.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {
let cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath)
return cell
}
You need to return cell because cellforRowAtIndexPath is non void function :
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {
let cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath)
return cell
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cartcel", for: indexPath) as! cartTableViewCell // My custom tableview cell class
cell.productName.text = Addtocartdata[indexPath.row].cartproName
cell.productQty.text = Addtocartdata[indexPath.row].cartproPrice
cell.productAmount.text = Addtocartdata[indexPath.row].cartproPrice
return cell;
}

Does not call didSelectRowAtIndexPath

Basically, my table is displaying properly as what I would want it to be. However, when I click on my cell, it doesn't goes into my didSelectRowAtIndexPath method.
The below screenshot also doesn't show any preloaded didSelectRowAtIndexPath method.
My codes are as follows:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return personList.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let myCell = stockTableView.dequeueReusableCellWithIdentifier("personCell", forIndexPath: indexPath) as UITableViewCell
myCell.textLabel?.text = personList[indexPath.row].personName
myCell.detailTextLabel?.text = personList[indexPath.row].GetPersonDescription()
return myCell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
let myCell = stockTableView.dequeueReusableCellWithIdentifier("personCell", forIndexPath: indexPath) as UITableViewCell
let selectedCell = myCell.textLabel?.text
}
EDIT: After researching, I realised that I've not set my UITableViewDelegate protocol. I tried to find online but can't find any solution. Could someone help me with this?
// Set the UITableViewDataSource and UITableViewDelegate for your class
class YourClass: UIViewController, UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.YourTableName.registerClass(UITableViewCell.self, forCellReuseIdentifier: "TotalsCell")
// Delegate and DataSource for your TableView
YourTableName.dataSource = self
YourTableName.delegate = self
}
}

How to get textLabel of selected row in swift?

So i am trying to get the value of the textLabel of the row I select. I tried printing it, but it didn't work. After some research I found out that this code worked, but only in Objective-C;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"did select and the text is %#",[tableView cellForRowAtIndexPath:indexPath].textLabel.text);]
}
I could not find any solution for Swift. Printing the indexpath.row is possible though, but that is not what I need.
so what should I do? or what is the 'Swift-version' of this code?
Try this:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let indexPath = tableView.indexPathForSelectedRow() //optional, to get from any UIButton for example
let currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell
print(currentCell.textLabel!.text)
If you're in a class inherited from UITableViewController, then this is the swift version:
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let cell = self.tableView.cellForRowAtIndexPath(indexPath)
NSLog("did select and the text is \(cell?.textLabel?.text)")
}
Note that cell is an optional, so it must be unwrapped - and the same for textLabel. If any of the 2 is nil (unlikely to happen, because the method is called with a valid index path), if you want to be sure that a valid value is printed, then you should check that both cell and textLabel are both not nil:
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let cell = self.tableView.cellForRowAtIndexPath(indexPath)
let text = cell?.textLabel?.text
if let text = text {
NSLog("did select and the text is \(text)")
}
}
Swift 4
To get the label of the selected row:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! TableViewCell
print(cell.textLabel?.text)
}
To get the label of the deselected row:
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! TableViewCell
print(cell.textLabel?.text)
}
If you want to print the text of a UITableViewCell according to its matching NSIndexPath, you have to use UITableViewDelegate's tableView:didSelectRowAtIndexPath: method and get a reference of the selected UITableViewCell with UITableView's cellForRowAtIndexPath: method.
For example:
import UIKit
class TableViewController: UITableViewController {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
switch indexPath.row {
case 0: cell.textLabel?.text = "Bike"
case 1: cell.textLabel?.text = "Car"
case 2: cell.textLabel?.text = "Ball"
default: cell.textLabel?.text = "Boat"
}
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedCell = tableView.cellForRowAtIndexPath(indexPath)
print(selectedCell?.textLabel?.text)
// this will print Optional("Bike") if indexPath.row == 0
}
}
However, for many reasons, I would not encourage you to use the previous code. Your UITableViewCell should only be responsible for displaying some content given by a model. In most cases, what you want is to print the content of your model (could be an Array of String) according to a NSIndexPath. By doing things like this, you will separate each element's responsibilities.
Thereby, this is what I would recommend:
import UIKit
class TableViewController: UITableViewController {
let toysArray = ["Bike", "Car", "Ball", "Boat"]
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return toysArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
cell.textLabel?.text = toysArray[indexPath.row]
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let toy = toysArray[indexPath.row]
print(toy)
// this will print "Bike" if indexPath.row == 0
}
}
As you can see, with this code, you don't have to deal with optionals and don't even need to get a reference of the matching UITableViewCell inside tableView:didSelectRowAtIndexPath: in order to print the desired text.
In my case I made small changes, when i search the value in tabelview select (didSelectRowAtIndexPath) the cell its return the index of the cell so im get problem in move one viewControler to another.By using this method i found a solution to redirect to a new viewControler
let indexPath = tableView.indexPathForSelectedRow!
let currentCellValue = tableView.cellForRow(at: indexPath!)! as UITableViewCell
let textLabelText = currentCellValue.textLabel!.text
print(textLabelText)
In swift 4 :
by overriding method
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name : "Main", bundle: nil)
let next vc = storyboard.instantiateViewController(withIdentifier: "nextvcIdentifier") as! NextViewController
self.navigationController?.pushViewController(prayerVC, animated: true)
}
This will work:
let item = tableView.cellForRowAtIndexPath(indexPath)!.textLabel!.text!
Maintain an array which stores data in the cellforindexPath method itself :-
[arryname objectAtIndex:indexPath.row];
Using same code in the didselectaAtIndexPath method too.. Good luck :)

Resources