How to update CollectionView with TableView "Did Select Row At" method - ios

I have a ViewController that contains a collectionview and a tableview. I would like to be able to press a row in my tableView and update the collectionView with new images. I'm not sure how I can reference the collectionView in this scenario.
I'm assuming I would have to put some code in my "DidSelectRowAt" method in my tableView, and pass data using dictionaries. Anyone have any idea? thanks!

Try this method and pass your latest dictionary to collectionView Dictionary
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
yourCollectionViewDict = arrDict[indexPath.row]
collectionView.reloadData()
}

Have you tried triggering collectionView.reloadData() from your tableView's didSelectRowAtIndexPath?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
collectionView.reloadData()
}

Related

Change Parent Collection View cell background color when select child tableview?

I have the parent custom Collectionviewcell and inside I have the child custom Tableview. When I click table view I need to change the background color of collectionviewcell and tableview row background. I am trying achieve using gesture for tableview. but its not working.
try with protocols
first your tableview inform the collection cell
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
backgroundColor = .red
delegate?.didSelectItem(indexPath: indexPath)
}
then on your collectionView Cell
func didSelectItem(indexPath: IndexPath) {
self.backgroundColor = .blue
}
Why you don't use didSelect of tableView?
Some code should be easy to see how you use gestures.
I have created one main array, inside created another array for table content, and manage selection on tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath).
Please check demo : https://github.com/karan7698007/CollectionViewDemo.git

How to get the index of the row (cell) I clicked on in TableViewController [duplicate]

This question already has answers here:
How to detect tableView cell touched or clicked in swift
(9 answers)
Closed 4 years ago.
I am beginner in Swift and probably this is fundamental stuff but I need to get the index of the row (cell) I clicked on in the tableview.
I have TableViewController with predefined methods, but I wasn't able to find what I am looking for.
Rows (cells) are string from the string array.
Implement table view delegate then you can detect your index cell by using method tableView(_:didSelectRowAt:): https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614877-tableview
Specifically:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("your row number: \(indexPath.row)")
}
override func tableView(_ tableView: UITableView,
didSelectRowAt indexPath: IndexPath) {
print(indexPath.row)
}
Your TableView contains TableViewCells. All the logic when you touch a cell can be programmed in
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
Update
indexPath.row will get you the index of the chosen tableviewcell

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

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.

Push to the next table view

I have two table views containing data, the first one is completely okay. I want to push from this table view to another view to another table view, but I cannot set the rows for it. How can I navigate through a multidimensional array in
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
Basically, my problem is about setting the right text for a row and getting from a multidimensional array :) (P.S. I have a different number of Strings in each subarray)
You just need to check on which tableView you are setting the values.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
if tableView == myFirstTableView {
// put the data for the first tableView
} else {
// put the data for the second tableview
}
}

UITableViewController's didSelectRowAtIndexPath doesn't work

I am using an UITableViewController for it and I override the following method and try to enable different segues when the user select different row.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("You selected row #\(indexPath.row)!")
switch indexPath.row {
case 3:
performSegue(withIdentifier: "segue3", sender: self)
case 8:
performSegue(withIdentifier: "segue8", sender: self)
default:
break
}
}
But actually, this method never get called, and the print out never show up when I select a row.
I did enable single selection, enable user interaction and set the delegate and datasource to the controller itself (That's automatically set when using UITableViewController, right?)
I am using static cells and default UITableViewCell. The view controller is UITableViewController.
Update:
I try to replace:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath)
With:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
And this doesn't work also.
for Swift 3
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
// your code ...
}
and one thing make sure your tableView single selection property is selected ...
Also check the selection of tableView in AttributeInspector of table view in your Storyboard. It should be selected to single selection.
Since you are using a UITableViewController override the function. Otherwise if you are using a tableView in a ViewController make sure the delegate is set to your ViewController class.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("You selected row #\(indexPath.row)!")
switch indexPath.row {
case 3:
performSegue(withIdentifier: "segue3", sender: self)
case 8:
performSegue(withIdentifier: "segue8", sender: self)
default:
break
}
}
Ok from what I’m seeing with your code. You is trying to print a string and also segue at the same time. But which object is you selecting to segue. It should be an array or something else example:
Var array[“Swift”, “IOS”]
DidSelect{
Switch: array[indexpath.row]
Case 3:
Print(“ u selected /(indexpath.row)”)
PerformSegue.........
Case 4 and so on.....

Resources