update a cell in tableview











up vote
1
down vote

favorite












My code reads cities array to a tableView.




  • When a cell is clicked, it move to a SecondViewController. The SecondViewController has a button.


  • If I clicked the button, it will display an image into that cell.




Problem: I am trying to update the cell whenever the button is clicked. It is working but no matter which cell is clicked, it always displays the image for cell 3, if I clicked again it displays for cell number 1 image.




How to fix this so that when the button is clicked, it display the image for it's cell and if the same cell is clicked, hide the image.



My codes:



var first = 0
var reload = false
var cellNumber: Int!

var cities:[String] = ["paris", "moscow", "milan","rome","madrid","garda","barcelona"]

@IBOutlet weak var tableView: UITableView!
// func to reload a cell
@objc func toReload(rowNumber: Int){
reload = true

let indexPath = IndexPath(row: rowNumber , section: 0)
tableView.reloadRows(at: [indexPath], with: .none)
}

// load tableview from a SecondViewController call
@objc func loadList(notification: NSNotification){

self.tableView.reloadData() // reload tableview
toReload(rowNumber: cellNumber)

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cities.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomCell

cell.label1.text = cities[indexPath.row]

let image : UIImage = UIImage(named: "20870718")! // assign imageView to an image

if first == 0 {
cell.myimage.isHidden = true // hide image
first = 1 // condition to never enter again
}

if reload == true {

if cell.myimage.isHidden == true {
cell.myimage.image = image
cell.myimage.isHidden = false
}
else{
cell.myimage.isHidden = true
}
reload = false

}

return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.cellNumber = indexPath.row

performSegue(withIdentifier: "send", sender: self)
}


override func viewDidLoad() {
super.viewDidLoad()
tableView.reloadData()

NotificationCenter.default.addObserver(self, selector: #selector(loadList), name: NSNotification.Name(rawValue: "load"), object: nil)
// call load list method
}


}



SecondViewController:



@IBAction func displayImage(_ sender: Any) {

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)

}









share|improve this question
























  • Well one thing to keep in mind is that self.tableView.reloadData() will reload everything so even though it seems like you wanted to only reload a specific row, the whole table will reload. However, even with that change, I think you should reconsider how you are keeping track which button is pressed. Maybe it would be better to send an object with the notification from the SecondViewController that indicates which button was pressed?
    – R. Lin
    Nov 11 at 3:32










  • I reload the whole tableview again once the button is clicked because im calling from another viewcontroller . So if I did not, it will be nil. So I reload it then I update the specific row. @R. Lin
    – call me AL
    Nov 11 at 3:52















up vote
1
down vote

favorite












My code reads cities array to a tableView.




  • When a cell is clicked, it move to a SecondViewController. The SecondViewController has a button.


  • If I clicked the button, it will display an image into that cell.




Problem: I am trying to update the cell whenever the button is clicked. It is working but no matter which cell is clicked, it always displays the image for cell 3, if I clicked again it displays for cell number 1 image.




How to fix this so that when the button is clicked, it display the image for it's cell and if the same cell is clicked, hide the image.



My codes:



var first = 0
var reload = false
var cellNumber: Int!

var cities:[String] = ["paris", "moscow", "milan","rome","madrid","garda","barcelona"]

@IBOutlet weak var tableView: UITableView!
// func to reload a cell
@objc func toReload(rowNumber: Int){
reload = true

let indexPath = IndexPath(row: rowNumber , section: 0)
tableView.reloadRows(at: [indexPath], with: .none)
}

// load tableview from a SecondViewController call
@objc func loadList(notification: NSNotification){

self.tableView.reloadData() // reload tableview
toReload(rowNumber: cellNumber)

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cities.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomCell

cell.label1.text = cities[indexPath.row]

let image : UIImage = UIImage(named: "20870718")! // assign imageView to an image

if first == 0 {
cell.myimage.isHidden = true // hide image
first = 1 // condition to never enter again
}

if reload == true {

if cell.myimage.isHidden == true {
cell.myimage.image = image
cell.myimage.isHidden = false
}
else{
cell.myimage.isHidden = true
}
reload = false

}

return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.cellNumber = indexPath.row

performSegue(withIdentifier: "send", sender: self)
}


override func viewDidLoad() {
super.viewDidLoad()
tableView.reloadData()

NotificationCenter.default.addObserver(self, selector: #selector(loadList), name: NSNotification.Name(rawValue: "load"), object: nil)
// call load list method
}


}



SecondViewController:



@IBAction func displayImage(_ sender: Any) {

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)

}









share|improve this question
























  • Well one thing to keep in mind is that self.tableView.reloadData() will reload everything so even though it seems like you wanted to only reload a specific row, the whole table will reload. However, even with that change, I think you should reconsider how you are keeping track which button is pressed. Maybe it would be better to send an object with the notification from the SecondViewController that indicates which button was pressed?
    – R. Lin
    Nov 11 at 3:32










  • I reload the whole tableview again once the button is clicked because im calling from another viewcontroller . So if I did not, it will be nil. So I reload it then I update the specific row. @R. Lin
    – call me AL
    Nov 11 at 3:52













up vote
1
down vote

favorite









up vote
1
down vote

favorite











My code reads cities array to a tableView.




  • When a cell is clicked, it move to a SecondViewController. The SecondViewController has a button.


  • If I clicked the button, it will display an image into that cell.




Problem: I am trying to update the cell whenever the button is clicked. It is working but no matter which cell is clicked, it always displays the image for cell 3, if I clicked again it displays for cell number 1 image.




How to fix this so that when the button is clicked, it display the image for it's cell and if the same cell is clicked, hide the image.



My codes:



var first = 0
var reload = false
var cellNumber: Int!

var cities:[String] = ["paris", "moscow", "milan","rome","madrid","garda","barcelona"]

@IBOutlet weak var tableView: UITableView!
// func to reload a cell
@objc func toReload(rowNumber: Int){
reload = true

let indexPath = IndexPath(row: rowNumber , section: 0)
tableView.reloadRows(at: [indexPath], with: .none)
}

// load tableview from a SecondViewController call
@objc func loadList(notification: NSNotification){

self.tableView.reloadData() // reload tableview
toReload(rowNumber: cellNumber)

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cities.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomCell

cell.label1.text = cities[indexPath.row]

let image : UIImage = UIImage(named: "20870718")! // assign imageView to an image

if first == 0 {
cell.myimage.isHidden = true // hide image
first = 1 // condition to never enter again
}

if reload == true {

if cell.myimage.isHidden == true {
cell.myimage.image = image
cell.myimage.isHidden = false
}
else{
cell.myimage.isHidden = true
}
reload = false

}

return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.cellNumber = indexPath.row

performSegue(withIdentifier: "send", sender: self)
}


override func viewDidLoad() {
super.viewDidLoad()
tableView.reloadData()

NotificationCenter.default.addObserver(self, selector: #selector(loadList), name: NSNotification.Name(rawValue: "load"), object: nil)
// call load list method
}


}



SecondViewController:



@IBAction func displayImage(_ sender: Any) {

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)

}









share|improve this question















My code reads cities array to a tableView.




  • When a cell is clicked, it move to a SecondViewController. The SecondViewController has a button.


  • If I clicked the button, it will display an image into that cell.




Problem: I am trying to update the cell whenever the button is clicked. It is working but no matter which cell is clicked, it always displays the image for cell 3, if I clicked again it displays for cell number 1 image.




How to fix this so that when the button is clicked, it display the image for it's cell and if the same cell is clicked, hide the image.



My codes:



var first = 0
var reload = false
var cellNumber: Int!

var cities:[String] = ["paris", "moscow", "milan","rome","madrid","garda","barcelona"]

@IBOutlet weak var tableView: UITableView!
// func to reload a cell
@objc func toReload(rowNumber: Int){
reload = true

let indexPath = IndexPath(row: rowNumber , section: 0)
tableView.reloadRows(at: [indexPath], with: .none)
}

// load tableview from a SecondViewController call
@objc func loadList(notification: NSNotification){

self.tableView.reloadData() // reload tableview
toReload(rowNumber: cellNumber)

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cities.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomCell

cell.label1.text = cities[indexPath.row]

let image : UIImage = UIImage(named: "20870718")! // assign imageView to an image

if first == 0 {
cell.myimage.isHidden = true // hide image
first = 1 // condition to never enter again
}

if reload == true {

if cell.myimage.isHidden == true {
cell.myimage.image = image
cell.myimage.isHidden = false
}
else{
cell.myimage.isHidden = true
}
reload = false

}

return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.cellNumber = indexPath.row

performSegue(withIdentifier: "send", sender: self)
}


override func viewDidLoad() {
super.viewDidLoad()
tableView.reloadData()

NotificationCenter.default.addObserver(self, selector: #selector(loadList), name: NSNotification.Name(rawValue: "load"), object: nil)
// call load list method
}


}



SecondViewController:



@IBAction func displayImage(_ sender: Any) {

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)

}






ios swift tableview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 5:19









kit

902116




902116










asked Nov 11 at 3:05









call me AL

222




222












  • Well one thing to keep in mind is that self.tableView.reloadData() will reload everything so even though it seems like you wanted to only reload a specific row, the whole table will reload. However, even with that change, I think you should reconsider how you are keeping track which button is pressed. Maybe it would be better to send an object with the notification from the SecondViewController that indicates which button was pressed?
    – R. Lin
    Nov 11 at 3:32










  • I reload the whole tableview again once the button is clicked because im calling from another viewcontroller . So if I did not, it will be nil. So I reload it then I update the specific row. @R. Lin
    – call me AL
    Nov 11 at 3:52


















  • Well one thing to keep in mind is that self.tableView.reloadData() will reload everything so even though it seems like you wanted to only reload a specific row, the whole table will reload. However, even with that change, I think you should reconsider how you are keeping track which button is pressed. Maybe it would be better to send an object with the notification from the SecondViewController that indicates which button was pressed?
    – R. Lin
    Nov 11 at 3:32










  • I reload the whole tableview again once the button is clicked because im calling from another viewcontroller . So if I did not, it will be nil. So I reload it then I update the specific row. @R. Lin
    – call me AL
    Nov 11 at 3:52
















Well one thing to keep in mind is that self.tableView.reloadData() will reload everything so even though it seems like you wanted to only reload a specific row, the whole table will reload. However, even with that change, I think you should reconsider how you are keeping track which button is pressed. Maybe it would be better to send an object with the notification from the SecondViewController that indicates which button was pressed?
– R. Lin
Nov 11 at 3:32




Well one thing to keep in mind is that self.tableView.reloadData() will reload everything so even though it seems like you wanted to only reload a specific row, the whole table will reload. However, even with that change, I think you should reconsider how you are keeping track which button is pressed. Maybe it would be better to send an object with the notification from the SecondViewController that indicates which button was pressed?
– R. Lin
Nov 11 at 3:32












I reload the whole tableview again once the button is clicked because im calling from another viewcontroller . So if I did not, it will be nil. So I reload it then I update the specific row. @R. Lin
– call me AL
Nov 11 at 3:52




I reload the whole tableview again once the button is clicked because im calling from another viewcontroller . So if I did not, it will be nil. So I reload it then I update the specific row. @R. Lin
– call me AL
Nov 11 at 3:52












1 Answer
1






active

oldest

votes

















up vote
1
down vote













There is a problem in your cellForRowAt.when secondViewController notify the first one no matter which cell you are reloading cellForRowAt always will be called because when you scroll tableView wants to recuse cell and reload == true becomes true for all cells.so you have to check if indexPath.row == cellNumber then do the rest work :



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomCell
cell.label1.text = cities[indexPath.row]
let image : UIImage = UIImage(named: "20870718")! // assign imageView to an image
if first == 0 {
cell.myimage.isHidden = true // hide image
first = 1 // condition to never enter again
}
if indexPath.row == cellNumber {
if reload == true {
if cell.myimage.isHidden == true {
cell.myimage.image = image
cell.myimage.isHidden = false
}
else {
cell.myimage.isHidden = true
}
reload = false
}
}
return cell
}





share|improve this answer























  • what u did fix one problem thanks. the problem now is that how to know if the image is displayed or hidden for a specific cell. what i did here is wrong ( if cell.myimage.isHidden == true { ) @andesta.erfan
    – call me AL
    Nov 11 at 17:02












  • @callmeAL that's another question please explain about your problem i didn't get it , you are checking if the image is hidden or not for that cell where is the problem?
    – andesta.erfan
    Nov 12 at 3:30











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245500%2fupdate-a-cell-in-tableview%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













There is a problem in your cellForRowAt.when secondViewController notify the first one no matter which cell you are reloading cellForRowAt always will be called because when you scroll tableView wants to recuse cell and reload == true becomes true for all cells.so you have to check if indexPath.row == cellNumber then do the rest work :



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomCell
cell.label1.text = cities[indexPath.row]
let image : UIImage = UIImage(named: "20870718")! // assign imageView to an image
if first == 0 {
cell.myimage.isHidden = true // hide image
first = 1 // condition to never enter again
}
if indexPath.row == cellNumber {
if reload == true {
if cell.myimage.isHidden == true {
cell.myimage.image = image
cell.myimage.isHidden = false
}
else {
cell.myimage.isHidden = true
}
reload = false
}
}
return cell
}





share|improve this answer























  • what u did fix one problem thanks. the problem now is that how to know if the image is displayed or hidden for a specific cell. what i did here is wrong ( if cell.myimage.isHidden == true { ) @andesta.erfan
    – call me AL
    Nov 11 at 17:02












  • @callmeAL that's another question please explain about your problem i didn't get it , you are checking if the image is hidden or not for that cell where is the problem?
    – andesta.erfan
    Nov 12 at 3:30















up vote
1
down vote













There is a problem in your cellForRowAt.when secondViewController notify the first one no matter which cell you are reloading cellForRowAt always will be called because when you scroll tableView wants to recuse cell and reload == true becomes true for all cells.so you have to check if indexPath.row == cellNumber then do the rest work :



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomCell
cell.label1.text = cities[indexPath.row]
let image : UIImage = UIImage(named: "20870718")! // assign imageView to an image
if first == 0 {
cell.myimage.isHidden = true // hide image
first = 1 // condition to never enter again
}
if indexPath.row == cellNumber {
if reload == true {
if cell.myimage.isHidden == true {
cell.myimage.image = image
cell.myimage.isHidden = false
}
else {
cell.myimage.isHidden = true
}
reload = false
}
}
return cell
}





share|improve this answer























  • what u did fix one problem thanks. the problem now is that how to know if the image is displayed or hidden for a specific cell. what i did here is wrong ( if cell.myimage.isHidden == true { ) @andesta.erfan
    – call me AL
    Nov 11 at 17:02












  • @callmeAL that's another question please explain about your problem i didn't get it , you are checking if the image is hidden or not for that cell where is the problem?
    – andesta.erfan
    Nov 12 at 3:30













up vote
1
down vote










up vote
1
down vote









There is a problem in your cellForRowAt.when secondViewController notify the first one no matter which cell you are reloading cellForRowAt always will be called because when you scroll tableView wants to recuse cell and reload == true becomes true for all cells.so you have to check if indexPath.row == cellNumber then do the rest work :



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomCell
cell.label1.text = cities[indexPath.row]
let image : UIImage = UIImage(named: "20870718")! // assign imageView to an image
if first == 0 {
cell.myimage.isHidden = true // hide image
first = 1 // condition to never enter again
}
if indexPath.row == cellNumber {
if reload == true {
if cell.myimage.isHidden == true {
cell.myimage.image = image
cell.myimage.isHidden = false
}
else {
cell.myimage.isHidden = true
}
reload = false
}
}
return cell
}





share|improve this answer














There is a problem in your cellForRowAt.when secondViewController notify the first one no matter which cell you are reloading cellForRowAt always will be called because when you scroll tableView wants to recuse cell and reload == true becomes true for all cells.so you have to check if indexPath.row == cellNumber then do the rest work :



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomCell
cell.label1.text = cities[indexPath.row]
let image : UIImage = UIImage(named: "20870718")! // assign imageView to an image
if first == 0 {
cell.myimage.isHidden = true // hide image
first = 1 // condition to never enter again
}
if indexPath.row == cellNumber {
if reload == true {
if cell.myimage.isHidden == true {
cell.myimage.image = image
cell.myimage.isHidden = false
}
else {
cell.myimage.isHidden = true
}
reload = false
}
}
return cell
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 6:45









Himanshu Moradiya

3,41531240




3,41531240










answered Nov 11 at 5:42









andesta.erfan

33919




33919












  • what u did fix one problem thanks. the problem now is that how to know if the image is displayed or hidden for a specific cell. what i did here is wrong ( if cell.myimage.isHidden == true { ) @andesta.erfan
    – call me AL
    Nov 11 at 17:02












  • @callmeAL that's another question please explain about your problem i didn't get it , you are checking if the image is hidden or not for that cell where is the problem?
    – andesta.erfan
    Nov 12 at 3:30


















  • what u did fix one problem thanks. the problem now is that how to know if the image is displayed or hidden for a specific cell. what i did here is wrong ( if cell.myimage.isHidden == true { ) @andesta.erfan
    – call me AL
    Nov 11 at 17:02












  • @callmeAL that's another question please explain about your problem i didn't get it , you are checking if the image is hidden or not for that cell where is the problem?
    – andesta.erfan
    Nov 12 at 3:30
















what u did fix one problem thanks. the problem now is that how to know if the image is displayed or hidden for a specific cell. what i did here is wrong ( if cell.myimage.isHidden == true { ) @andesta.erfan
– call me AL
Nov 11 at 17:02






what u did fix one problem thanks. the problem now is that how to know if the image is displayed or hidden for a specific cell. what i did here is wrong ( if cell.myimage.isHidden == true { ) @andesta.erfan
– call me AL
Nov 11 at 17:02














@callmeAL that's another question please explain about your problem i didn't get it , you are checking if the image is hidden or not for that cell where is the problem?
– andesta.erfan
Nov 12 at 3:30




@callmeAL that's another question please explain about your problem i didn't get it , you are checking if the image is hidden or not for that cell where is the problem?
– andesta.erfan
Nov 12 at 3:30


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245500%2fupdate-a-cell-in-tableview%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine