Updating uiTableViewCell from data model after intial load
I'm very beginner in iOS development, so this problem may look trivial. I searched and could not find any questions about this problem. But I wonder what is correct approach of updating custom cells of a UITableView when you change your data model at run time after initial loading of cells from data model. From change, I mean data entry change, not adding or removing data.
Here is an example. Let's say that I have these DataModel and DataModelCell as follows:
class DataModelView : UITableViewCell {
@IBOutlet weak var mainLabel: UILabel!
}
class DataModel {
var title: String = "" {
didSet {
// which cell this entry is connected to?
}
}
}
...
items: [DataModel] =
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DataModelView", for: indexPath)
cell.mainLabel?.text = items[indexPath.item].title
// addition line for each approach
// approach 1:
// items[indexPath.item].view = cell
// approach 2:
// items[indexPath.item].viewIndexPath = indexPath
return cell
}
My problem is that when I changed title of one of cells in my data model at run time, I like to update corresponding cell in UI. I like to know what is the best approach to make a relationship between data model'd entry and cell in UITableView.
There are 3 different approaches that comes to my mind. I want to know if they are correct or not or if there is any better method:
1st approach: a weak pointer to cell in my data entry like this:
class DataModel {
weak var view: DataModelView?
var title: String = "" {
didSet {
view?.mainLabel?.text = title
}
}
}
2nd approach: keep IndexPath of cell in my data entry like this:
class DataModel {
var viewIndexPath: IndexPath?
var title: String = "" {
didSet {
// call delegate to controller and ask to update cell for viewIndexPath
}
}
}
3rd approach: don't keep anything which corresponds to cell in data entry:
class DataModel {
var title: String = "" {
didSet {
// call delegate to controller and ask to find and update cell for self
}
}
}
In first 2 approaches, I keep relationship between cell and data model in my data model. In 3rd approach, I need to keep this relationship in controller.
Are all these approaches correct(especially first one)? Which one do you suggest? and What is best approach generally?
I have seen that people keep a pointer to data model in their view in order to update data model from view. I wonder if it is correct in the other way too or not(1st approach).
ios ios-mvc
|
show 5 more comments
I'm very beginner in iOS development, so this problem may look trivial. I searched and could not find any questions about this problem. But I wonder what is correct approach of updating custom cells of a UITableView when you change your data model at run time after initial loading of cells from data model. From change, I mean data entry change, not adding or removing data.
Here is an example. Let's say that I have these DataModel and DataModelCell as follows:
class DataModelView : UITableViewCell {
@IBOutlet weak var mainLabel: UILabel!
}
class DataModel {
var title: String = "" {
didSet {
// which cell this entry is connected to?
}
}
}
...
items: [DataModel] =
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DataModelView", for: indexPath)
cell.mainLabel?.text = items[indexPath.item].title
// addition line for each approach
// approach 1:
// items[indexPath.item].view = cell
// approach 2:
// items[indexPath.item].viewIndexPath = indexPath
return cell
}
My problem is that when I changed title of one of cells in my data model at run time, I like to update corresponding cell in UI. I like to know what is the best approach to make a relationship between data model'd entry and cell in UITableView.
There are 3 different approaches that comes to my mind. I want to know if they are correct or not or if there is any better method:
1st approach: a weak pointer to cell in my data entry like this:
class DataModel {
weak var view: DataModelView?
var title: String = "" {
didSet {
view?.mainLabel?.text = title
}
}
}
2nd approach: keep IndexPath of cell in my data entry like this:
class DataModel {
var viewIndexPath: IndexPath?
var title: String = "" {
didSet {
// call delegate to controller and ask to update cell for viewIndexPath
}
}
}
3rd approach: don't keep anything which corresponds to cell in data entry:
class DataModel {
var title: String = "" {
didSet {
// call delegate to controller and ask to find and update cell for self
}
}
}
In first 2 approaches, I keep relationship between cell and data model in my data model. In 3rd approach, I need to keep this relationship in controller.
Are all these approaches correct(especially first one)? Which one do you suggest? and What is best approach generally?
I have seen that people keep a pointer to data model in their view in order to update data model from view. I wonder if it is correct in the other way too or not(1st approach).
ios ios-mvc
please show uscellForRowAtIndexPathimplementation
– andesta.erfan
Dec 28 '18 at 11:07
How you can load TableView from the other class...... every approach is wrong.
– dahiya_boy
Dec 28 '18 at 11:21
@andesta.erfan I addedcellForRowAtIndexPath, but it just a default and simple implementation to load initial items. I don't have any problem here. I like to see updates after this initial load.
– Afshin
Dec 28 '18 at 11:21
@dahiya_boy each data entry is connected to 1 cell. and I want to update corresponding cell when I update its data.
– Afshin
Dec 28 '18 at 11:22
Something like the 3rd approach. Your view controller should be notified of updates in the view model and it can then reload the appropriate cells
– Paulw11
Dec 28 '18 at 11:26
|
show 5 more comments
I'm very beginner in iOS development, so this problem may look trivial. I searched and could not find any questions about this problem. But I wonder what is correct approach of updating custom cells of a UITableView when you change your data model at run time after initial loading of cells from data model. From change, I mean data entry change, not adding or removing data.
Here is an example. Let's say that I have these DataModel and DataModelCell as follows:
class DataModelView : UITableViewCell {
@IBOutlet weak var mainLabel: UILabel!
}
class DataModel {
var title: String = "" {
didSet {
// which cell this entry is connected to?
}
}
}
...
items: [DataModel] =
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DataModelView", for: indexPath)
cell.mainLabel?.text = items[indexPath.item].title
// addition line for each approach
// approach 1:
// items[indexPath.item].view = cell
// approach 2:
// items[indexPath.item].viewIndexPath = indexPath
return cell
}
My problem is that when I changed title of one of cells in my data model at run time, I like to update corresponding cell in UI. I like to know what is the best approach to make a relationship between data model'd entry and cell in UITableView.
There are 3 different approaches that comes to my mind. I want to know if they are correct or not or if there is any better method:
1st approach: a weak pointer to cell in my data entry like this:
class DataModel {
weak var view: DataModelView?
var title: String = "" {
didSet {
view?.mainLabel?.text = title
}
}
}
2nd approach: keep IndexPath of cell in my data entry like this:
class DataModel {
var viewIndexPath: IndexPath?
var title: String = "" {
didSet {
// call delegate to controller and ask to update cell for viewIndexPath
}
}
}
3rd approach: don't keep anything which corresponds to cell in data entry:
class DataModel {
var title: String = "" {
didSet {
// call delegate to controller and ask to find and update cell for self
}
}
}
In first 2 approaches, I keep relationship between cell and data model in my data model. In 3rd approach, I need to keep this relationship in controller.
Are all these approaches correct(especially first one)? Which one do you suggest? and What is best approach generally?
I have seen that people keep a pointer to data model in their view in order to update data model from view. I wonder if it is correct in the other way too or not(1st approach).
ios ios-mvc
I'm very beginner in iOS development, so this problem may look trivial. I searched and could not find any questions about this problem. But I wonder what is correct approach of updating custom cells of a UITableView when you change your data model at run time after initial loading of cells from data model. From change, I mean data entry change, not adding or removing data.
Here is an example. Let's say that I have these DataModel and DataModelCell as follows:
class DataModelView : UITableViewCell {
@IBOutlet weak var mainLabel: UILabel!
}
class DataModel {
var title: String = "" {
didSet {
// which cell this entry is connected to?
}
}
}
...
items: [DataModel] =
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DataModelView", for: indexPath)
cell.mainLabel?.text = items[indexPath.item].title
// addition line for each approach
// approach 1:
// items[indexPath.item].view = cell
// approach 2:
// items[indexPath.item].viewIndexPath = indexPath
return cell
}
My problem is that when I changed title of one of cells in my data model at run time, I like to update corresponding cell in UI. I like to know what is the best approach to make a relationship between data model'd entry and cell in UITableView.
There are 3 different approaches that comes to my mind. I want to know if they are correct or not or if there is any better method:
1st approach: a weak pointer to cell in my data entry like this:
class DataModel {
weak var view: DataModelView?
var title: String = "" {
didSet {
view?.mainLabel?.text = title
}
}
}
2nd approach: keep IndexPath of cell in my data entry like this:
class DataModel {
var viewIndexPath: IndexPath?
var title: String = "" {
didSet {
// call delegate to controller and ask to update cell for viewIndexPath
}
}
}
3rd approach: don't keep anything which corresponds to cell in data entry:
class DataModel {
var title: String = "" {
didSet {
// call delegate to controller and ask to find and update cell for self
}
}
}
In first 2 approaches, I keep relationship between cell and data model in my data model. In 3rd approach, I need to keep this relationship in controller.
Are all these approaches correct(especially first one)? Which one do you suggest? and What is best approach generally?
I have seen that people keep a pointer to data model in their view in order to update data model from view. I wonder if it is correct in the other way too or not(1st approach).
ios ios-mvc
ios ios-mvc
edited Dec 28 '18 at 11:27
Afshin
asked Dec 28 '18 at 10:56
AfshinAfshin
3,0011624
3,0011624
please show uscellForRowAtIndexPathimplementation
– andesta.erfan
Dec 28 '18 at 11:07
How you can load TableView from the other class...... every approach is wrong.
– dahiya_boy
Dec 28 '18 at 11:21
@andesta.erfan I addedcellForRowAtIndexPath, but it just a default and simple implementation to load initial items. I don't have any problem here. I like to see updates after this initial load.
– Afshin
Dec 28 '18 at 11:21
@dahiya_boy each data entry is connected to 1 cell. and I want to update corresponding cell when I update its data.
– Afshin
Dec 28 '18 at 11:22
Something like the 3rd approach. Your view controller should be notified of updates in the view model and it can then reload the appropriate cells
– Paulw11
Dec 28 '18 at 11:26
|
show 5 more comments
please show uscellForRowAtIndexPathimplementation
– andesta.erfan
Dec 28 '18 at 11:07
How you can load TableView from the other class...... every approach is wrong.
– dahiya_boy
Dec 28 '18 at 11:21
@andesta.erfan I addedcellForRowAtIndexPath, but it just a default and simple implementation to load initial items. I don't have any problem here. I like to see updates after this initial load.
– Afshin
Dec 28 '18 at 11:21
@dahiya_boy each data entry is connected to 1 cell. and I want to update corresponding cell when I update its data.
– Afshin
Dec 28 '18 at 11:22
Something like the 3rd approach. Your view controller should be notified of updates in the view model and it can then reload the appropriate cells
– Paulw11
Dec 28 '18 at 11:26
please show us
cellForRowAtIndexPath implementation– andesta.erfan
Dec 28 '18 at 11:07
please show us
cellForRowAtIndexPath implementation– andesta.erfan
Dec 28 '18 at 11:07
How you can load TableView from the other class...... every approach is wrong.
– dahiya_boy
Dec 28 '18 at 11:21
How you can load TableView from the other class...... every approach is wrong.
– dahiya_boy
Dec 28 '18 at 11:21
@andesta.erfan I added
cellForRowAtIndexPath, but it just a default and simple implementation to load initial items. I don't have any problem here. I like to see updates after this initial load.– Afshin
Dec 28 '18 at 11:21
@andesta.erfan I added
cellForRowAtIndexPath, but it just a default and simple implementation to load initial items. I don't have any problem here. I like to see updates after this initial load.– Afshin
Dec 28 '18 at 11:21
@dahiya_boy each data entry is connected to 1 cell. and I want to update corresponding cell when I update its data.
– Afshin
Dec 28 '18 at 11:22
@dahiya_boy each data entry is connected to 1 cell. and I want to update corresponding cell when I update its data.
– Afshin
Dec 28 '18 at 11:22
Something like the 3rd approach. Your view controller should be notified of updates in the view model and it can then reload the appropriate cells
– Paulw11
Dec 28 '18 at 11:26
Something like the 3rd approach. Your view controller should be notified of updates in the view model and it can then reload the appropriate cells
– Paulw11
Dec 28 '18 at 11:26
|
show 5 more comments
2 Answers
2
active
oldest
votes
4th approach: a weak pointer to data model in the cell and key value observing
class DataModelView : UITableViewCell {
@IBOutlet weak var mainLabel: UILabel!
var observation : NSKeyValueObservation?
weak var model : DataModel? {
didSet {
if model == nil { observation = nil }
else {
observation = model!.observe(.title, options: [.new]) { [weak self] (model, _) in
self?.mainLabel.text = model.title
}
}
}
}
}
KVO requires inheritance from NSObject
class DataModel : NSObject {
@objc dynamic var title: String = ""
}
In cellForRow pass the model
let cell = tableView.dequeueReusableCell(withIdentifier: "DataModelView", for: indexPath) as! DataModelView
cell.model = items[indexPath.row]
and remove the observer in didEndDisplaying
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
(cell as! DataModelView).observation = nil
}
This approach looks great. I think the only drawback is if you have multiple field you like to update or not. if for example you want to update 5-6 fields for cell, you will install a lot of observers which will make code a little messed up. Anyway, thanks for this approach.
– Afshin
Dec 28 '18 at 12:10
add a comment |
It should be like this
class DataModelView: UITableViewCell {
@IBOutlet weak var mainLabel: UILabel!
var yourModel: DataModel? {
didSet {
mainLabel.text = yourModel?.title
}
}
And in your cellForRowAt
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DataModelView", for: indexPath)
cell.yourData = items[indexPath.item]
return cell
}
After any item changed in your items array just call
tableView.reloadData()
The problem is, How OP knows when data is changed in didset?
– dahiya_boy
Dec 28 '18 at 11:31
I think it this a very bad approach.Loading whole table for updating 1 cell is bad...and I cannot find when to do this....
– Afshin
Dec 28 '18 at 11:32
@Afshin As I know, It will look to array and only reload cells that are changed.
– Emre Önder
Dec 28 '18 at 11:33
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53957402%2fupdating-uitableviewcell-from-data-model-after-intial-load%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
4th approach: a weak pointer to data model in the cell and key value observing
class DataModelView : UITableViewCell {
@IBOutlet weak var mainLabel: UILabel!
var observation : NSKeyValueObservation?
weak var model : DataModel? {
didSet {
if model == nil { observation = nil }
else {
observation = model!.observe(.title, options: [.new]) { [weak self] (model, _) in
self?.mainLabel.text = model.title
}
}
}
}
}
KVO requires inheritance from NSObject
class DataModel : NSObject {
@objc dynamic var title: String = ""
}
In cellForRow pass the model
let cell = tableView.dequeueReusableCell(withIdentifier: "DataModelView", for: indexPath) as! DataModelView
cell.model = items[indexPath.row]
and remove the observer in didEndDisplaying
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
(cell as! DataModelView).observation = nil
}
This approach looks great. I think the only drawback is if you have multiple field you like to update or not. if for example you want to update 5-6 fields for cell, you will install a lot of observers which will make code a little messed up. Anyway, thanks for this approach.
– Afshin
Dec 28 '18 at 12:10
add a comment |
4th approach: a weak pointer to data model in the cell and key value observing
class DataModelView : UITableViewCell {
@IBOutlet weak var mainLabel: UILabel!
var observation : NSKeyValueObservation?
weak var model : DataModel? {
didSet {
if model == nil { observation = nil }
else {
observation = model!.observe(.title, options: [.new]) { [weak self] (model, _) in
self?.mainLabel.text = model.title
}
}
}
}
}
KVO requires inheritance from NSObject
class DataModel : NSObject {
@objc dynamic var title: String = ""
}
In cellForRow pass the model
let cell = tableView.dequeueReusableCell(withIdentifier: "DataModelView", for: indexPath) as! DataModelView
cell.model = items[indexPath.row]
and remove the observer in didEndDisplaying
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
(cell as! DataModelView).observation = nil
}
This approach looks great. I think the only drawback is if you have multiple field you like to update or not. if for example you want to update 5-6 fields for cell, you will install a lot of observers which will make code a little messed up. Anyway, thanks for this approach.
– Afshin
Dec 28 '18 at 12:10
add a comment |
4th approach: a weak pointer to data model in the cell and key value observing
class DataModelView : UITableViewCell {
@IBOutlet weak var mainLabel: UILabel!
var observation : NSKeyValueObservation?
weak var model : DataModel? {
didSet {
if model == nil { observation = nil }
else {
observation = model!.observe(.title, options: [.new]) { [weak self] (model, _) in
self?.mainLabel.text = model.title
}
}
}
}
}
KVO requires inheritance from NSObject
class DataModel : NSObject {
@objc dynamic var title: String = ""
}
In cellForRow pass the model
let cell = tableView.dequeueReusableCell(withIdentifier: "DataModelView", for: indexPath) as! DataModelView
cell.model = items[indexPath.row]
and remove the observer in didEndDisplaying
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
(cell as! DataModelView).observation = nil
}
4th approach: a weak pointer to data model in the cell and key value observing
class DataModelView : UITableViewCell {
@IBOutlet weak var mainLabel: UILabel!
var observation : NSKeyValueObservation?
weak var model : DataModel? {
didSet {
if model == nil { observation = nil }
else {
observation = model!.observe(.title, options: [.new]) { [weak self] (model, _) in
self?.mainLabel.text = model.title
}
}
}
}
}
KVO requires inheritance from NSObject
class DataModel : NSObject {
@objc dynamic var title: String = ""
}
In cellForRow pass the model
let cell = tableView.dequeueReusableCell(withIdentifier: "DataModelView", for: indexPath) as! DataModelView
cell.model = items[indexPath.row]
and remove the observer in didEndDisplaying
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
(cell as! DataModelView).observation = nil
}
edited Dec 28 '18 at 11:45
answered Dec 28 '18 at 11:40
vadianvadian
144k13154170
144k13154170
This approach looks great. I think the only drawback is if you have multiple field you like to update or not. if for example you want to update 5-6 fields for cell, you will install a lot of observers which will make code a little messed up. Anyway, thanks for this approach.
– Afshin
Dec 28 '18 at 12:10
add a comment |
This approach looks great. I think the only drawback is if you have multiple field you like to update or not. if for example you want to update 5-6 fields for cell, you will install a lot of observers which will make code a little messed up. Anyway, thanks for this approach.
– Afshin
Dec 28 '18 at 12:10
This approach looks great. I think the only drawback is if you have multiple field you like to update or not. if for example you want to update 5-6 fields for cell, you will install a lot of observers which will make code a little messed up. Anyway, thanks for this approach.
– Afshin
Dec 28 '18 at 12:10
This approach looks great. I think the only drawback is if you have multiple field you like to update or not. if for example you want to update 5-6 fields for cell, you will install a lot of observers which will make code a little messed up. Anyway, thanks for this approach.
– Afshin
Dec 28 '18 at 12:10
add a comment |
It should be like this
class DataModelView: UITableViewCell {
@IBOutlet weak var mainLabel: UILabel!
var yourModel: DataModel? {
didSet {
mainLabel.text = yourModel?.title
}
}
And in your cellForRowAt
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DataModelView", for: indexPath)
cell.yourData = items[indexPath.item]
return cell
}
After any item changed in your items array just call
tableView.reloadData()
The problem is, How OP knows when data is changed in didset?
– dahiya_boy
Dec 28 '18 at 11:31
I think it this a very bad approach.Loading whole table for updating 1 cell is bad...and I cannot find when to do this....
– Afshin
Dec 28 '18 at 11:32
@Afshin As I know, It will look to array and only reload cells that are changed.
– Emre Önder
Dec 28 '18 at 11:33
add a comment |
It should be like this
class DataModelView: UITableViewCell {
@IBOutlet weak var mainLabel: UILabel!
var yourModel: DataModel? {
didSet {
mainLabel.text = yourModel?.title
}
}
And in your cellForRowAt
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DataModelView", for: indexPath)
cell.yourData = items[indexPath.item]
return cell
}
After any item changed in your items array just call
tableView.reloadData()
The problem is, How OP knows when data is changed in didset?
– dahiya_boy
Dec 28 '18 at 11:31
I think it this a very bad approach.Loading whole table for updating 1 cell is bad...and I cannot find when to do this....
– Afshin
Dec 28 '18 at 11:32
@Afshin As I know, It will look to array and only reload cells that are changed.
– Emre Önder
Dec 28 '18 at 11:33
add a comment |
It should be like this
class DataModelView: UITableViewCell {
@IBOutlet weak var mainLabel: UILabel!
var yourModel: DataModel? {
didSet {
mainLabel.text = yourModel?.title
}
}
And in your cellForRowAt
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DataModelView", for: indexPath)
cell.yourData = items[indexPath.item]
return cell
}
After any item changed in your items array just call
tableView.reloadData()
It should be like this
class DataModelView: UITableViewCell {
@IBOutlet weak var mainLabel: UILabel!
var yourModel: DataModel? {
didSet {
mainLabel.text = yourModel?.title
}
}
And in your cellForRowAt
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DataModelView", for: indexPath)
cell.yourData = items[indexPath.item]
return cell
}
After any item changed in your items array just call
tableView.reloadData()
answered Dec 28 '18 at 11:30
Emre ÖnderEmre Önder
811523
811523
The problem is, How OP knows when data is changed in didset?
– dahiya_boy
Dec 28 '18 at 11:31
I think it this a very bad approach.Loading whole table for updating 1 cell is bad...and I cannot find when to do this....
– Afshin
Dec 28 '18 at 11:32
@Afshin As I know, It will look to array and only reload cells that are changed.
– Emre Önder
Dec 28 '18 at 11:33
add a comment |
The problem is, How OP knows when data is changed in didset?
– dahiya_boy
Dec 28 '18 at 11:31
I think it this a very bad approach.Loading whole table for updating 1 cell is bad...and I cannot find when to do this....
– Afshin
Dec 28 '18 at 11:32
@Afshin As I know, It will look to array and only reload cells that are changed.
– Emre Önder
Dec 28 '18 at 11:33
The problem is, How OP knows when data is changed in didset?
– dahiya_boy
Dec 28 '18 at 11:31
The problem is, How OP knows when data is changed in didset?
– dahiya_boy
Dec 28 '18 at 11:31
I think it this a very bad approach.Loading whole table for updating 1 cell is bad...and I cannot find when to do this....
– Afshin
Dec 28 '18 at 11:32
I think it this a very bad approach.Loading whole table for updating 1 cell is bad...and I cannot find when to do this....
– Afshin
Dec 28 '18 at 11:32
@Afshin As I know, It will look to array and only reload cells that are changed.
– Emre Önder
Dec 28 '18 at 11:33
@Afshin As I know, It will look to array and only reload cells that are changed.
– Emre Önder
Dec 28 '18 at 11:33
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53957402%2fupdating-uitableviewcell-from-data-model-after-intial-load%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
please show us
cellForRowAtIndexPathimplementation– andesta.erfan
Dec 28 '18 at 11:07
How you can load TableView from the other class...... every approach is wrong.
– dahiya_boy
Dec 28 '18 at 11:21
@andesta.erfan I added
cellForRowAtIndexPath, but it just a default and simple implementation to load initial items. I don't have any problem here. I like to see updates after this initial load.– Afshin
Dec 28 '18 at 11:21
@dahiya_boy each data entry is connected to 1 cell. and I want to update corresponding cell when I update its data.
– Afshin
Dec 28 '18 at 11:22
Something like the 3rd approach. Your view controller should be notified of updates in the view model and it can then reload the appropriate cells
– Paulw11
Dec 28 '18 at 11:26