TableView Cell not loading View Controller until Different TableView Cell is clicked - ios

I've searched around and haven't truly found why this is happening. Basically, I followed this tutorial https://www.youtube.com/watch?v=yupIw9FXUso by Jared Davison on creating Table View Cells to Multiple View controllers. In his example, everything works perfectly, but for some reason when you click on a table view cell in my code the cell is highlighted in grey. Then, when the user clicks on a separate table view cell the view controller that should have been loaded by the first table view cell is loaded. If the user then clicks back on the original table view cell the page that should have been loaded by the second table view cell is loaded. In summary, all of the view controllers are loaded a "click" behind.
Here is the code for the table view:
//Feed Navigation Functions
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return elements.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 75
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "feedCell") as! FeedTableViewCell
cell.txtTitle.text = "The Fight Against \(elements[indexPath.row])"
cell.issueIcon.image = UIImage(named: "Issue Icons_\(elements[indexPath.row])")
return cell
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
}
Update: The array is a simple array of strings for example [One, Two, Three] there are 6 strings in the array.

When you select a cell then select another one the method didDeselectRow is called so the Vc is pushed you actually want to implement didSelectRowAt
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
// this to deSelect it after push
tableView.deselectRow(at: indexPath, animated: false)
}
This method fired when a cell is selected
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath
This method fired when a cell is deSelected
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath

You want to use the tableView's delegate method didSelectRow instead of didDeselectRow I think...

Issue: click on a table view cell in my code the cell is highlighted in grey
This is due to the selectionStyle, which you can read about here. If you don't want the cell highlighted, you can set cell.selectionStyle = .none.
Edit: As indicated in other correct response - issue was with incorrect/typo in method - we should use didSelectRowAt not didDeselectRowAt.

Related

How do I delete UITableview cells by tapping a UIButton?

I want to delete all the UITableviewCells in my Tableview by tapping a button
UITableView DataSource Methods
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return UserList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FriendAddCell" , for: indexPath) as! AddFriendLTableViewCell
cell.lb_Email.text = UserList[indexPath.row].userEmail
let imageUrl = URL(string: UserList[indexPath.row].imageUrl)
cell.img_Avatar.loadImageUrl(url: imageUrl!)
return cell
}
My Button Function
#IBAction func btn_NewAddUser(_ sender: Any) {
UserList.removeAll()
table_SearchFirends.reloadData()
}
the cells I tried to delete still exist when I add a new cell
How can I delete all the cells by pressing the button?
How can I make the tableView as if there were no rows created?
How I can understand your problem that you see empty lines with separator lines on bottom.
Make this for remove extra lines:
tableView.tableFooterView = UIView()
And some advise - use camelCase style in Swift.

UITableView embeded in NavigationController and UITabBar won't Scroll

I am currently learning IOS. I have UITableView embedded in NavigationController and UITabBar. I have populated the TableView with data and its showing in the table when I run the app. The TableView is not scrolling and for the life of me I can't figure out why.
Here is the table view code
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return petitions.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
// code to initialize the cell here
return cell
}
Any suggestions as to what am missing, or how to make it work is welcomed
Maybe if you changed your table view scroll options. So table view is a scroll view. Options must be like below:

Swift 5 | didSelectRowAt is selecting two cells at the same time

I am doing a screen where there a list o cells with a switch, like an image below;
I have a struct where a save the label of the cell and the switch state value. This struct is loaded at: var source: [StructName] = [] and then source values are attributed to the UITableView cells.
The problem is that when a touch a cell the function: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) change multiples cells switches states at the same time.
I try to work around the problem by implementing the following function:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let cell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
for n in 0..<source.count{ // This loop search for the right cell by looking at the cell label text and the struct where the state of the switch is saved
if cell.label.text! == source[n].Label{
// If the label text is equal to the position where the values is saved (is the same order that the cells are loaded in the UITableView) then a change the state of the switch
let indexLabel = IndexPath(row: n, section: 0)
let cellValues = tableView.cellForRow(at: indexLabel) as! CustomTableViewCell
if cellValues.switchButton.isOn {
cellValues.switchButton.setOn(false, animated: true)
source[n].valor = cellValues.switchButton.isOn
} else {
cellValues.switchButton.setOn(true, animated: true)
source[n].valor = cellValues.switchButton.isOn
}
break
}
}
although is saved the right values to the switch state array(source) the visual state of multiples switches also changes even though the cells where never touch.
How could I change my code to select and change only the touched cell?
You should not store / read the state of anything in a cell.
But first things first:
Why do loop through all the values? You should be able to access the row in the data model directly by indexPath.row
You should only modify the model data, not the cell
You then tell the table view to reload the cell, which will then ask the model for the correct data to be displayed.
I would suggest the following:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let row = indexPath.row
source[row].valor.toggle()
tableView.reloadRows(at:[indexPath], with:.automatic)
}

How to create clickable cells in UITableView (Swift)

I'm working on a personal project and I want to make the cells in my UITableView clickable, such that when they are pressed they segue to the next view, and pass information from their cell along with it when they are pressed.
I've attempted to look over other guides and videos that demonstrate how to do this, however they are all outdated. One that stood out to me that seemed to show promise but didn't end up working was in reference to a certain "didSelectRowAt" function, but I could not get this to work.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as!
CandidateTableViewCell
if (indexPath.row >= candidateCells.count){
print("???")
candidateCells.append(cell)
print("Strange error")
}
tableView.delegate = self
cell.CandidateName?.text = candidatesNames[indexPath.item]
cell.CandidatePos?.text = candidatesPositions[indexPath.item]
//cell.candidateButton.tag = indexPath.row
cell.CandidateName.sizeToFit()
cell.CandidatePos.sizeToFit()
print(candidateCells)
print(indexPath.row)
print(candidateCells.count)
print(indexPath.row >= candidateCells.count)
candidateCells[indexPath.item] = cell
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "Segue", sender: Any?.self)
}
What I was expecting to happen was that the cell would become clickable, and when the cell is clicked it would send me to the next page in the app, however when clicked, nothing happens. The cell does not become highlighted, and the segue does not occur. Thank you so much for any and all suggestions!
If you want to pass information to the next cell, I usually avoid using a Segue, and setup the ViewController with a reuse identifier. By doing this, you can pass information easier. For instance
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "YOURRESUEIDENTIFIER") as! YOURVIEWCONTROLLERCLASS
vc.infoToPass = cellData[indexPath.row]
DispatchQueue.main.async {
self.navigationController?.pushViewController(vc, animated: true)
}
}
Your other option would be to implement the shouldPerformSegueWithIdentifier() method in order to set the data properly. You would click your cell, which calls the performSegue function, which would call the shouldPerformSegueWithIdentifier where you can set the data and return true

Load 10 items at a time when scroll to bottom load more items on tableview ios ,swift , Firebase

Loading all the date from the firebase to the tableview at a time but i wanted to load 10 items at a time when i scroll to bottom again 10 items should load to the tableview, below is my code
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = items[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "inciCell") as!
IncidentCell
cell.delegate = self
cell.setupCell(with: item, indexPath)
return cell
}
}
In the delegate method cellForRowAtIndexPath you may check something like indexPath.row == items.count - 1.
if it's true, then you get 10 items from your DB and append it to your `items.
then do tableView.reloadData() to update the table view with 10 new items.
You may also make some nice spinner in the bottom (tableFooterView) of the table view. But it could be the next step.
Hope it helps.

Resources