SO, UICollectionView is being a real pain for me right now. Consider I have a UIViewController which has a UICollectionView embedded in it. Well each cell of the CollectionView is almost the entire width of the UIViewController. And each cell contains some buttons and images. When I select one button and tend to make the button retain its state, the CollectionView reuses the cell and kind of duplicates the cell states across other cells as well. However when I try to put the cells in an array and kind of want to check the states of cells in that array, the cellForItemAt method overwrites those cells. I am so confused. Please help. Even prepareForReuse in UICollectionViewCell isn't helping. Here is some code:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! AddressCollectionViewCell
cell.shopAddressDetailLbl.text = ""
cell.addressObj = addresses[indexPath.row]
cell.configureCellForAddress(cell.addressObj)
cell.cellTag = indexPath.row
cell.cellDelegate = self
if addressCells.contains(cell) == false {
addressCells.append(cell)
} else {
if cell.isAddressConfirmed == true {
cell.confirmAddress.setTitle("CONFIRMED", for: .normal)
cell.confirmAddress.isEnabled = false
cell.confirmAddress.backgroundColor
= UIColor(red: 0, green: 100/255, blue: 0, alpha: 1)
addressCells[indexPath.row] = cell
}
}
return cell
}
extension AddressesCollectionViewController: AddressCollectionViewCellDelegate {
func confirmBtnPressed(confirmAddressObj: Address, cell:AddressCollectionViewCell) {
for cellTemp in addressCells {
if cellTemp == cell && cellTemp.isAddressConfirmed == false {
if let dele = addressCollectionViewDelegate {
cellTemp.isAddressConfirmed = true
dele.configureCellsAccordingToChanges(cell: cellTemp)
}
}
}
}
}
override func prepareForReuse() {
super.prepareForReuse()
cellTag = 0
confirmAddress.setTitle("Confirm Address", for: .normal)
confirmAddress.backgroundColor = APP_UNIVERSAL_COLOR
confirmAddress.isEnabled = true
}
Any help is more than appreciated.
🙌 #Vadian, #Abu Ul Hassan 👍
Pretty slick! To others who need help in this regard. Vadian suggested in comments that I just need to update and monitor my model and thats exactly what I did. SO here it goes:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! AddressCollectionViewCell
cell.shopAddressDetailLbl.text = ""
cell.addressObj = addresses[indexPath.row]
cell.configureCellForAddress(cell.addressObj)
cell.cellTag = indexPath.row
cell.cellDelegate = self
if addresses[indexPath.row].isConfirmed! == true {
cell.confirmAddress.setTitle("CONFIRMED", for: .normal)
cell.confirmAddress.isEnabled = false
cell.confirmAddress.backgroundColor = UIColor(red: 0, green: 100/255, blue: 0, alpha: 1)
} else {
cell.confirmAddress.setTitle("Confirm Address", for: .normal)
cell.confirmAddress.isEnabled = true
cell.confirmAddress.backgroundColor = APP_UNIVERSAL_COLOR
}
return cell
}
extension AddressesCollectionViewController: AddressCollectionViewCellDelegate {
func confirmBtnPressed(confirmAddressObj: Address, cell:AddressCollectionViewCell) {
if confirmAddressObj.isConfirmed! == false {
if let dele = addressCollectionViewDelegate {
cell.isAddressConfirmed = true
dele.configureCellsAccordingToChanges(cell: cell)
}
}
}
}
And its ALIVE :D
Related
I have a UITableView. Its cell contains a label that will display a question, a yes button and a no button. The goal is to view questions one by one.
First I call the API to get the questions in the viewDidLoad method:
override func viewDidLoad() {
super.viewDidLoad()
tableView.allowsSelection = false
getQuestions(baseComplainID: "1") { (questions, error) in
self.questions = questions
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
In the cellForRowAt method I display them one by one:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? TableViewCell else {
fatalError("Fatal Error")
}
cell.yesButton.isHidden = false
cell.noButton.isHidden = false
if indexPath.row + 1 == displayNumber {
cell.questionLabel.text = questions[indexPath.row].question_name
} else {
cell.yesButton.isHidden = true
cell.noButton.isHidden = true
}
cell.yesButton.addTarget(self, action: #selector(action), for: .touchUpInside)
cell.noButton.addTarget(self, action: #selector(action), for: .touchUpInside)
return cell
}
and this is the action being executed on clicking yes or no:
#objc func action(sender: UIButton){
let indexPath = self.tableView.indexPathForRow(at: sender.convert(CGPoint.zero, to: self.tableView))
let cell = tableView.cellForRow(at: indexPath!) as? TableViewCell
cell?.yesButton.isEnabled = false
cell?.noButton.isEnabled = false
if sender == cell?.yesButton {
sender.setTitleColor(.black, for: .normal)
sender.backgroundColor = .green
} else {
sender.setTitleColor(.black, for: .normal)
sender.backgroundColor = .green
}
displayNumber += 1
self.tableView.reloadData()
}
Here I just change the background color of the button and increment the display number to display the next question.
All of this works perfect EXCEPT when I scroll, the data gets overridden and sometimes I find the question label empty and the questions replaces each other. I know this is normal due to the cell reusability but I don't know how to fix it.
Any suggestions please?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? TableViewCell else {
fatalError("Fatal Error")
}
cell.yesButton.isHidden = false
cell.noButton.isHidden = false
if indexPath.row + 1 == displayNumber {
cell.questionLabel.text = questions[indexPath.row].question_name
} else {
cell.yesButton.isHidden = true
cell.noButton.isHidden = true
}
cell.yesButton.addTarget(self, action: #selector(action), for: .touchUpInside)
cell.noButton.addTarget(self, action: #selector(action), for: .touchUpInside)
return cell
}
i feel like your issue lies here in cellForRowAt function.
you have this written
if indexPath.row + 1 == displayNumber { your code here }
but i am unsure as to why you need this.
you should be doing something like this inside cellForRowAt
let data = self.questions
data = data[indexPath.row]
cell.questionLabel.text = data.question_name
you should not be adding 1 to your indexPath.row
You're going to need to keep track of your yes's no's and neither's for each cell. I'd tack an enum onto another data structure along with your questions. Your primary problem was that you were only keeping track of your question. You need to keep track of your answer as well. That way, when you load a cell, you can configure each button with the colors that you want in cellForRow(at:)
struct QuestionAndAnswer {
enum Answer {
case yes
case no
case nada
}
var question: Question
var answer: Answer
}
And try not to reload your whole tableView when a button is pressed. tableView.reloadData() is expensive and distracting to the user. You should only be reloading the row that changed when a button was pressed.
Add callbacks on your cell so that you know which cell the corresponding buttons belong to. Notice how in the onYes and onNo callbacks we keep track of your "yes" or "no" selection then immediately reload the row below. When the row is reloaded, we finally know which color to make the button.
class AnswerCell: UITableViewCell {
#IBOutlet weak var yesButton: UIButton!
#IBOutlet weak var noButton: UIButton!
var onYes: (() -> Void)) = {}
var onNo: (() -> Void)) = {}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// ...
cell.yesButton.backgroundColor = qAndA.answer == .yes ? .green : .white
cell.noButton.backgroundColor = qAndA.answer == .no ? .green : .white
cell.onYes = {
questionsAndAnswers[indexPath.row].answer = .yes
tableView.reloadRows(at: [indexPath], with: .fade)
}
cell.onNo = {
questionsAndAnswers[indexPath.row].answer = .no
tableView.reloadRows(at: [indexPath], with: .fade)
}
// ...
}
Well, assume you have 10 questions, so a very simple and workaround fix is to declare a new array which has 10 elements as follow
var questionIsLoaded = Array(repeating:true , count 10)
the previous line will declare an array with 10 elements each element is bool which in our case will be true
then declare a function that handles if the question is loaded or not as follows, so if the question is loaded thus, the question with its indexPath should be marked as true and as a result, the yes and no buttons should be hidden else, the buttons should be shown
func handleQuestionIfLoaded(cell:yourCellType, indexPath:IndexPath) {
if questionIsLoaded[indexPath.row] , indexPath.row + 1 == displayNumber { {
questionIsLoaded[indexPath.row] = false
cell.questionLabel.text = questions[indexPath.row].question_name
cell.yesButton.isHidden = questionIsLoaded[indexPath.row]
cell.noButton.isHidden = questionIsLoaded[indexPath.row]
} else {
cell.yesButton.isHidden = questionIsLoaded[indexPath.row]
cell.noButton.isHidden = questionIsLoaded[indexPath.row]
}
cell.yesButton.addTarget(self, action: #selector(action), for: .touchUpInside)
cell.noButton.addTarget(self, action: #selector(action), for: .touchUpInside)
}
then replace the body of cellForRowAt with the function above, then your action function will be as follows
#objc func action(sender: UIButton){
let indexPath = self.tableView.indexPathForRow(at: sender.convert(CGPoint.zero, to: self.tableView))
let cell = tableView.cellForRow(at: indexPath!) as? TableViewCell
cell?.yesButton.isEnabled = questionIsLoaded[indexPath.row]
cell?.noButton.isEnabled = questionIsLoaded[indexPath.row]
if sender == cell?.yesButton {
sender.setTitleColor(.black, for: .normal)
sender.backgroundColor = .green
} else {
sender.setTitleColor(.black, for: .normal)
sender.backgroundColor = .green
}
displayNumber += 1
self.tableView.reloadData()
}
Now, your cells depend on an external dependency which is the array you have declared earlier, this means that when the cells are dequeued, they will be reused according to if the question is loaded or not by asking the array's element at the specific indexPath at first if the element is true or false
I have a uicollectionview with collectionviewcells and each cell has a boolean value associated with a favorites button. There are over 50 cells positioned vertically (four cells are viewable at a time). If the favorite button is clicked it toggles between a highlighted image and a non-highlighted image.
That functionality works, but for some reason when I click one then scroll down I see other cells with their favorite button highlighted. When I scroll back up the cell favorite button is no longer highlighted.
Is there something missing from this code?
NOTE:: As a default I set each cell's boolean value to false. It's only changed when I click on the cell's favorite button.
My code below:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! SimpleDispensarySubCell
cell.backgroundColor = UIColor.init(white: 0.10, alpha: 0.25)
cell.infoLine2TextVw.text = ""
cell.infoLine3TextVw.text = ""
if let heading_name = self.dict_dict_holder[indexPath.item]["Name"]{
cell.headerTextVw.text = heading_name
cell.infoLine1TextVw.text = self.dict_dict_holder[indexPath.item]["Phone"]
}
if cell.isFavorite{
cell.isFavorite = true
cell.favorite_button.setImage(#imageLiteral(resourceName: "heart_fill_icon"), for: .normal)
}
else{
cell.isFavorite = false
cell.favorite_button.setImage(#imageLiteral(resourceName: "heart_nofill_icon"), for: .normal)
}
cell.bringSubview(toFront: cell.headerTextVw)
//cell.favorite_button.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(AddFavorite(withSender:))))
cell.favorite_button.addTarget(self, action:#selector(AddFavorite), for: .touchUpInside)
return cell
}
#objc func AddFavorite(withSender sender:UIButton){
let cell = sender.superview as! SimpleDispensarySubCell
if cell.isFavorite{
cell.isFavorite = false
cell.favorite_button.setImage(#imageLiteral(resourceName: "heart_nofill_icon"), for: .normal)
}
else{
cell.isFavorite = true
cell.favorite_button.setImage(#imageLiteral(resourceName: "heart_fill_icon"), for: .normal)
}
}
It's because you are using collectionView.dequeueReusableCell you should define an array to hold favorite state of each cell on it. It could solve your problem.
let favoriteStateArray = countOfRows;
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! SimpleDispensarySubCell
cell.backgroundColor = UIColor.init(white: 0.10, alpha: 0.25)
cell.infoLine2TextVw.text = ""
cell.infoLine3TextVw.text = ""
if let heading_name = self.dict_dict_holder[indexPath.item]["Name"]{
cell.headerTextVw.text = heading_name
cell.infoLine1TextVw.text = self.dict_dict_holder[indexPath.item]["Phone"]
}
if favoriteStateArray[indexPath.row]{
cell.isFavorite = true
cell.favorite_button.setImage(#imageLiteral(resourceName: "heart_fill_icon"), for: .normal)
}
else{
cell.isFavorite = false
cell.favorite_button.setImage(#imageLiteral(resourceName: "heart_nofill_icon"), for: .normal)
}
cell.bringSubview(toFront: cell.headerTextVw)
//cell.favorite_button.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(AddFavorite(withSender:))))
cell.favorite_button.addTarget(self, action:#selector(AddFavorite), for: .touchUpInside)
return cell
}
#objc func AddFavorite(withSender sender:UIButton){
let cell = sender.superview as! SimpleDispensarySubCell
let index = collectionView.indexPath(for: cell)
if favoriteStateArray[indexPath.row]{
favoriteStateArray[indexPath.row] = false
cell.favorite_button.setImage(#imageLiteral(resourceName: "heart_nofill_icon"), for: .normal)
}
else{
favoriteStateArray[indexPath.row] = false
cell.favorite_button.setImage(#imageLiteral(resourceName: "heart_fill_icon"), for: .normal)
}
}
Since the cells are getting reused changes are affecting multiple cells.
You can use prepare for reuse method to clear changes that must affect only a particular cell and Clear the cell and prepare it for Reuse.
override func prepareForReuse() {
cell.isFavorite = false
super.prepareForReuse()
}
I'm working on an app that has a UICollectionView with an infinite scroll. Each cell has a like button. The problem is that the lower I scroll - the slower the like button changes its color when tapped on.
It can take up to 3 seconds between the print("BEGIN") and print("END") in my code. I also observed that print(post) gets called dozens of times and it prints dozens of posts that I haven't tapped the like button on. I want it to only call the didSet in the cell I'm updating.
Can someone please explain why it seems like it's calling didSet in multiple cells instead of just one cell I tapped on? Does it have something to do with that cells are being reused?
How would you fix this long delay problem? I would like the cell to update it's like button image to a selected one without such a long delay.
protocol HomePostCellDelegate {
func didTapLike(for cell: HomePostCell)
}
class HomePostCell: UICollectionViewCell {
var delegate: HomePostCellDelegate?
var post: Post? {
didSet {
print(post)
likeButton.setImage(post?.isLiked == true ? #imageLiteral(resourceName: "like_selected").withRenderingMode(.alwaysTemplate) : #imageLiteral(resourceName: "like_unselected").withRenderingMode(.alwaysTemplate), for: .normal)
likeButton.tintColor = .white
// ...
}
}
lazy var likeButton: UIButton = {
let button = UIButton(type: .system)
button.setImage(#imageLiteral(resourceName: "like_unselected").withRenderingMode(.alwaysOriginal), for: .normal)
button.addTarget(self, action: #selector(handleLike), for: .touchUpInside)
return button
}()
#objc func handleLike() {
NSLog("#objc func handleLike()")
delegate?.didTapLike(for: self)
}
// ...
}
class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout, HomePostCellDelegate {
// ...
func didTapLike(for cell: HomePostCell) {
let generator = UIImpactFeedbackGenerator(style: .light)
generator.impactOccurred()
guard let indexPath = collectionView?.indexPath(for: cell) else { return }
guard let post = cell.post else { return }
cell.post.isLiked = !cell.post.isLiked
if cell.post.isLiked {
cell.post.numberOfLikes += 1
} else {
cell.post.numberOfLikes -= 1
}
print("BEGIN")
self.collectionView?.reloadItems(at: [indexPath])
print("END")
}
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == self.presenter.posts.count - 5 && !presenter.isFinishedPaging && !presenter.isCurrentlyPaging {
presenter.isCurrentlyPaging = true
presenter.paginatePosts(withSuccess: { [weak self] () in
self?.collectionView?.refreshControl?.endRefreshing()
self?.collectionView?.reloadData()
self?.presenter.isCurrentlyPaging = false
}) { [weak self] (errorMessage) in
self?.presenter.isCurrentlyPaging = false
print(errorMessage)
}
}
if presenter.posts.count == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: errorCellId, for: indexPath) as! ErrorCell
setUpErrorCellAnimation(cell: cell)
return cell
} else if presenter.posts[indexPath.item].id == nil {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: adCellId, for: indexPath) as! AdCell
// some ad logic here
// ...
cell.contentView.addSubview(bannerView)
return cell
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! HomePostCell
cell.delegate = self
if presenter.isAdmin && presenter.isInAdminView {
cell.isInAdminView = true
} else {
cell.isInAdminView = false
}
cell.post = presenter.posts[safe: indexPath.item]
return cell
}
}
UPDATE:
Stack trace when putting a breakpoint inside the didSet method:
Here I am loading collection view data dynamically in table view cell and table view cells also created on dynamic json array count and here after selecting any element in collection view which is in first table view cell then the collection view needs to be reloaded with new data which is in second table view cell can anyone help me how to reload the collection view in second table view cell swift 3 if this is not possible can anyone provide me any alternative layout to implement this ?
Here is my cell for row method
if indexPath.section == 0 {
let cell = addToCartTableView.dequeueReusableCell(withIdentifier: "addToCartCollectionCell") as! AddToCartCollectionTableViewCell
cell.configurableProduct = self.detailModel
print(self.detailModel)
cell.collectionView.tag = indexPath.row
self.addToCartTableView.setNeedsLayout()
self.addToCartTableView.layoutIfNeeded()
cell.collectionView.reloadData()
cell.cellLabel.text = detailModel?.extensionAttribute?.productOptions[indexPath.row].label
if detailModel?.extensionAttribute?.productOptions[indexPath.row].label == "Size"{
cell.sizeGuideBtn.isHidden = false
}else{
cell.sizeGuideBtn.isHidden = true
}
cell.getCurrentRow = indexPath.row
return cell
}else {
let cell = addToCartTableView.dequeueReusableCell(withIdentifier: "addToCartQtyCell") as! AddToCartQuantityTableViewCell
self.addToCartTableView.setNeedsLayout()
self.addToCartTableView.layoutIfNeeded()
cell.QtyLabel.text = "Qty"
return cell
}
Here is my table view cell code
override func awakeFromNib() {
super.awakeFromNib()
collectionView.delegate = self
collectionView.dataSource = self
print(getCurrentRow)
// Initialization code
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 50, height: 30)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print(configurableProduct?.extensionAttribute?.productOptions[getCurrentRow].values.count)
return (configurableProduct?.extensionAttribute?.productOptions[getCurrentRow].values.count)!
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! AddToCartCollectionViewCell
if indexPath.item == 0 {
let items = configurableProduct?.extensionAttribute?.productOptions[getCurrentRow].values[indexPath.row]
cell.collectionLabel.text = "\(items?.valueIndex as! Int)"
if indexPath.item == self.selectedIndex{
cell.backgroundColor = #colorLiteral(red: 0.007509540026, green: 0.6581087804, blue: 0.01165772038, alpha: 1)
}else if self.selectedIndex == nil {
cell.backgroundColor = UIColor.white
}else{
cell.backgroundColor = UIColor.white
}
}
else {
if selectedValue != nil {
for item in (self.configurableProduct?.extensionAttribute?.productStock)! {
// let jsonStr = "{\"label\":\"57-175\",\"stock\":0}"
let dict = try! JSONSerialization.jsonObject(with: item.data(using: .utf8)!, options: []) as! [String:Any]
let labelValue = dict["label"] as! String
print(labelValue)
let values:[String] = labelValue.components(separatedBy: "-")
print(values)
self.colorNumber = Int(values[0])
self.sizeNumber = Int(values[1])
let stock = dict["stock"] as! Int
let value = selectedValue
if value == self.colorNumber {
if stock != 0 {
self.sizeArray.append(self.sizeNumber!)
print(self.sizeArray)
cell.collectionLabel.text = "\(self.sizeNumber)"
}
}
}
if indexPath.item == self.selectedIndex{
cell.backgroundColor = #colorLiteral(red: 0.007509540026, green: 0.6581087804, blue: 0.01165772038, alpha: 1)
}else if self.selectedIndex == nil {
cell.backgroundColor = UIColor.white
}else{
cell.backgroundColor = UIColor.white
}
}
else {
let items = configurableProduct?.extensionAttribute?.productOptions[getCurrentRow].values[indexPath.item]
print(items?.valueIndex)
for item in (self.configurableProduct?.extensionAttribute?.productStock)! {
// let jsonStr = "{\"label\":\"57-175\",\"stock\":0}"
let dict = try! JSONSerialization.jsonObject(with: item.data(using: .utf8)!, options: []) as! [String:Any]
let labelValue = dict["label"] as! String
print(labelValue)
let values:[String] = labelValue.components(separatedBy: "-")
print(values)
self.colorNumber = Int(values[0])
self.sizeNumber = Int(values[1])
let stock = dict["stock"] as! Int
let value = self.selectedIndex
if value == self.colorNumber {
if stock != 0 {
self.sizeArray.append(self.sizeNumber!)
print(self.sizeArray)
cell.collectionLabel.text = "\(items?.valueIndex as! Int)"
}
}else {
cell.collectionLabel.text = "\(items?.valueIndex as! Int)"
}
}
if indexPath.item == self.selectedIndex{
cell.backgroundColor = #colorLiteral(red: 0.007509540026, green: 0.6581087804, blue: 0.01165772038, alpha: 1)
}else if self.selectedIndex == nil {
cell.backgroundColor = UIColor.white
}else{
cell.backgroundColor = UIColor.white
}
}
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView.tag == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! AddToCartCollectionViewCell
cell.backgroundColor = #colorLiteral(red: 0.007509540026, green: 0.6581087804, blue: 0.01165772038, alpha: 1)
cell.collectionLabel.layer.cornerRadius = 15
cell.collectionLabel.layer.borderColor = #colorLiteral(red: 0.007509540026, green: 0.6581087804, blue: 0.01165772038, alpha: 1)
self.dataSelected = true
self.selectedIndex = indexPath.item
print(self.selectedIndex)
self.collectionView.reloadData()
self.sizeArray.removeAll()
self.selectedValue = configurableProduct?.extensionAttribute?.productOptions[1].values[indexPath.item].valueIndex
self.getCurrentRow = 1
self.collectionView.reloadData()
print(self.selectedValue)
}
else {
print(collectionView.tag)
}
}
here is my layout image
in didSelectItemAt of collection view, reload the table cell which contains size by doing
let indexpath = IndexPath(item: value, section: 0)
tableview.reloadRows(at: [indexpath], with: .none)
here value is position of row which you want to upate
Create a new variable "cellObj" which data type is same as you custom cell where you added collection view.
var cellObj:AddToCartCollectionTableViewCell!
After add assign cellObj value after this line.
let cell = addToCartTableView.dequeueReusableCell(withIdentifier: "addToCartCollectionCell") as! AddToCartCollectionTableViewCell
cellObj = cell
*** After doing you are able to reload collection view any where.
cellObj.collectionView.reloadData()
DispatchQueue.main.async {
// cell!.tableView.reloadData()
collectionView.reloadItems(at: [indexPath])
cell!.tableView.reloadData()
}
})
I am creating a pokedex app and the way I want it to work is basically there is a scroller at the top of the screen which allows you to select any pokemon and upon choosing the pokemon, underneath the scroller the entry for the pokemon will show up (bulbasaur will be there by default until a pokemon is selected because bulbasaur is the first pokemon with an ID of 1). To achieve this I have my view controller return two types of cells, the first being a "chooser cell" which is the scroller, and the second being a "description cell" which is the dex entry. I gave the view controller a data member called dex entry and return dex entry in the cellForItemAt function but the image of the cell is not changing (from bulbasaur to whichever pokemon I select). I print to the console what is the value of dex entry's pokemon every time a pokemon is selected so I am sure that the dex entry is being directly changed but I don't know why the image is not changing as well. Below are relevant parts of my code.
view controller (only part of it):
import UIKit
class PokeDexController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var dexEntry = DescriptionCell()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "PokeDex 386"
collectionView?.backgroundColor = UIColor(red: 52/255.0, green: 55/255.0, blue: 64/255.0, alpha: 1.0)
//collectionView?.backgroundColor = UIColor.white
collectionView?.register(chooserCell.self, forCellWithReuseIdentifier: cellID)
collectionView?.register(DescriptionCell.self, forCellWithReuseIdentifier: descID)
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if (indexPath.row == 0)
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! chooserCell
return cell
}
else{
let descCell = collectionView.dequeueReusableCell(withReuseIdentifier: descID, for: indexPath) as! DescriptionCell
dexEntry = descCell
return dexEntry
}
}
descriptionCell class:
import UIKit
class DescriptionCell: UICollectionViewCell
{
private var pokemon : Pokemon?
{
didSet
{
if let id = pokemon?._id
{
imageView.image = UIImage(named: String(id))
print("Pokemon with the id of " + String(id))
}
}
}
override init(frame: CGRect)
{
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setPokemon(poke: Pokemon)
{
self.pokemon = poke
}
func getPokemon() -> Pokemon
{
return pokemon!
}
let imageView: UIImageView =
{
let iv = UIImageView()
iv.image = UIImage(named: "1")
iv.contentMode = .scaleAspectFill
return iv
}()
func setupViews()
{
backgroundColor = UIColor(red: 52/255.0, green: 55/255.0, blue: 64/255.0, alpha: 1.0)
addSubview(imageView)
imageView.frame = (CGRect(x: frame.width/6, y: frame.height/30, width: frame.width/4, height: frame.height/4))
}
}
choosercell class (specifically the didSelectItemAt):
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
let poke = pokemon[indexPath.row]
print("Selected " + poke._name)
let vc = PokeDexController()
vc.dexEntry.setPokemon(poke: poke)
let name = vc.dexEntry.getPokemon()._name
print(name ?? "nothing there")
}
image of the app and the console output
any help is appreciated, thanks.
You need to change the dexEntry when you select a cell and reload the collection view cell.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
let poke = pokemon[indexPath.row]
print("Selected " + poke._name)
let cell = collectionView.cellForItem(at: IndexPath(row: 1, section: 0) as! DescriptionCell
cell.setPokemon(poke: poke)
collectionView.reloadItems(at: IndexPath(row: 1, section: 0))
}
Hope this helps.
I haven't solved my problem but I realize that the cell that I am returning in my viewController is independent of dexEntry so as far as I can cell, once that cell is set, it is set, so I now i will figure out how to reload things when a cell is selected so the cell that is returned has an image of a different pokemon.