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
. TheSecondViewController
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
add a comment |
up vote
1
down vote
favorite
My code reads cities array to a tableView
.
When a cell is clicked, it move to a
SecondViewController
. TheSecondViewController
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
Well one thing to keep in mind is thatself.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
add a comment |
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
. TheSecondViewController
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
My code reads cities array to a tableView
.
When a cell is clicked, it move to a
SecondViewController
. TheSecondViewController
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
ios swift tableview
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 thatself.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
add a comment |
Well one thing to keep in mind is thatself.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
add a comment |
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
}
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
add a comment |
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
}
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
add a comment |
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
}
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
add a comment |
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
}
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
}
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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