TableViewCell is froze with an empty dataSource array












1















I make a request to the server for fetching data, then set its data to my array for dealing with TableView.



Sometimes happens issue when one cell or few cells is froze despite the fact that server response is an empty dataSource array is an empty too, but cells stacks anyway. And still on the screen till I closed. Also tableView contentOffset is changed:



enter image description here




The screenshot shows froze tableViewCell. But dataSource array is an empty, numberOfRowsInSection in section also equal to 0.




Some details:





  • tableView.reloadData() is called from the main thread;

  • ViewController lie in TabBarViewController;

  • I inspect mine model object class on Retain cycle, seems everything is fine;

  • I use xib for the cell. The cell has a custom content view. All IBOutlets in Cell class has weak references.


Please, help me to understand where I'm wrong



EDIT:



extension ViewController: UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let numberOfRowsInSection = items?.count else {
messageLabel?.text = messageText
return 0
}

return numberOfRowsInSection
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

guard let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier", for: indexPath) as? CustomTableViewCell else { return UITableViewCell() }

// Cell settings

return cell

}
}









share|improve this question




















  • 3





    Can you please show your datasources of tableview.

    – iPeter
    Nov 15 '18 at 9:33






  • 1





    Consider adding related code.

    – Ratul Sharker
    Nov 15 '18 at 9:36











  • iPeter, Ratul Sharker, I added a few lines of code. Will it be enough?

    – Roman Romanenko
    Nov 15 '18 at 9:48











  • Are you missing optional func numberOfSections(in tableView: UITableView) -> Int overrride? because if you do - the default implementation returns 1 which might be invalid for your usecase

    – Peter Pajchl
    Nov 15 '18 at 11:32











  • is it same problem with this code? func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 0 }

    – canister_exister
    Nov 15 '18 at 18:28
















1















I make a request to the server for fetching data, then set its data to my array for dealing with TableView.



Sometimes happens issue when one cell or few cells is froze despite the fact that server response is an empty dataSource array is an empty too, but cells stacks anyway. And still on the screen till I closed. Also tableView contentOffset is changed:



enter image description here




The screenshot shows froze tableViewCell. But dataSource array is an empty, numberOfRowsInSection in section also equal to 0.




Some details:





  • tableView.reloadData() is called from the main thread;

  • ViewController lie in TabBarViewController;

  • I inspect mine model object class on Retain cycle, seems everything is fine;

  • I use xib for the cell. The cell has a custom content view. All IBOutlets in Cell class has weak references.


Please, help me to understand where I'm wrong



EDIT:



extension ViewController: UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let numberOfRowsInSection = items?.count else {
messageLabel?.text = messageText
return 0
}

return numberOfRowsInSection
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

guard let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier", for: indexPath) as? CustomTableViewCell else { return UITableViewCell() }

// Cell settings

return cell

}
}









share|improve this question




















  • 3





    Can you please show your datasources of tableview.

    – iPeter
    Nov 15 '18 at 9:33






  • 1





    Consider adding related code.

    – Ratul Sharker
    Nov 15 '18 at 9:36











  • iPeter, Ratul Sharker, I added a few lines of code. Will it be enough?

    – Roman Romanenko
    Nov 15 '18 at 9:48











  • Are you missing optional func numberOfSections(in tableView: UITableView) -> Int overrride? because if you do - the default implementation returns 1 which might be invalid for your usecase

    – Peter Pajchl
    Nov 15 '18 at 11:32











  • is it same problem with this code? func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 0 }

    – canister_exister
    Nov 15 '18 at 18:28














1












1








1








I make a request to the server for fetching data, then set its data to my array for dealing with TableView.



Sometimes happens issue when one cell or few cells is froze despite the fact that server response is an empty dataSource array is an empty too, but cells stacks anyway. And still on the screen till I closed. Also tableView contentOffset is changed:



enter image description here




The screenshot shows froze tableViewCell. But dataSource array is an empty, numberOfRowsInSection in section also equal to 0.




Some details:





  • tableView.reloadData() is called from the main thread;

  • ViewController lie in TabBarViewController;

  • I inspect mine model object class on Retain cycle, seems everything is fine;

  • I use xib for the cell. The cell has a custom content view. All IBOutlets in Cell class has weak references.


Please, help me to understand where I'm wrong



EDIT:



extension ViewController: UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let numberOfRowsInSection = items?.count else {
messageLabel?.text = messageText
return 0
}

return numberOfRowsInSection
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

guard let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier", for: indexPath) as? CustomTableViewCell else { return UITableViewCell() }

// Cell settings

return cell

}
}









share|improve this question
















I make a request to the server for fetching data, then set its data to my array for dealing with TableView.



Sometimes happens issue when one cell or few cells is froze despite the fact that server response is an empty dataSource array is an empty too, but cells stacks anyway. And still on the screen till I closed. Also tableView contentOffset is changed:



enter image description here




The screenshot shows froze tableViewCell. But dataSource array is an empty, numberOfRowsInSection in section also equal to 0.




Some details:





  • tableView.reloadData() is called from the main thread;

  • ViewController lie in TabBarViewController;

  • I inspect mine model object class on Retain cycle, seems everything is fine;

  • I use xib for the cell. The cell has a custom content view. All IBOutlets in Cell class has weak references.


Please, help me to understand where I'm wrong



EDIT:



extension ViewController: UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let numberOfRowsInSection = items?.count else {
messageLabel?.text = messageText
return 0
}

return numberOfRowsInSection
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

guard let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier", for: indexPath) as? CustomTableViewCell else { return UITableViewCell() }

// Cell settings

return cell

}
}






ios swift xcode xib nib






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 18:20







Roman Romanenko

















asked Nov 15 '18 at 9:32









Roman RomanenkoRoman Romanenko

373418




373418








  • 3





    Can you please show your datasources of tableview.

    – iPeter
    Nov 15 '18 at 9:33






  • 1





    Consider adding related code.

    – Ratul Sharker
    Nov 15 '18 at 9:36











  • iPeter, Ratul Sharker, I added a few lines of code. Will it be enough?

    – Roman Romanenko
    Nov 15 '18 at 9:48











  • Are you missing optional func numberOfSections(in tableView: UITableView) -> Int overrride? because if you do - the default implementation returns 1 which might be invalid for your usecase

    – Peter Pajchl
    Nov 15 '18 at 11:32











  • is it same problem with this code? func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 0 }

    – canister_exister
    Nov 15 '18 at 18:28














  • 3





    Can you please show your datasources of tableview.

    – iPeter
    Nov 15 '18 at 9:33






  • 1





    Consider adding related code.

    – Ratul Sharker
    Nov 15 '18 at 9:36











  • iPeter, Ratul Sharker, I added a few lines of code. Will it be enough?

    – Roman Romanenko
    Nov 15 '18 at 9:48











  • Are you missing optional func numberOfSections(in tableView: UITableView) -> Int overrride? because if you do - the default implementation returns 1 which might be invalid for your usecase

    – Peter Pajchl
    Nov 15 '18 at 11:32











  • is it same problem with this code? func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 0 }

    – canister_exister
    Nov 15 '18 at 18:28








3




3





Can you please show your datasources of tableview.

– iPeter
Nov 15 '18 at 9:33





Can you please show your datasources of tableview.

– iPeter
Nov 15 '18 at 9:33




1




1





Consider adding related code.

– Ratul Sharker
Nov 15 '18 at 9:36





Consider adding related code.

– Ratul Sharker
Nov 15 '18 at 9:36













iPeter, Ratul Sharker, I added a few lines of code. Will it be enough?

– Roman Romanenko
Nov 15 '18 at 9:48





iPeter, Ratul Sharker, I added a few lines of code. Will it be enough?

– Roman Romanenko
Nov 15 '18 at 9:48













Are you missing optional func numberOfSections(in tableView: UITableView) -> Int overrride? because if you do - the default implementation returns 1 which might be invalid for your usecase

– Peter Pajchl
Nov 15 '18 at 11:32





Are you missing optional func numberOfSections(in tableView: UITableView) -> Int overrride? because if you do - the default implementation returns 1 which might be invalid for your usecase

– Peter Pajchl
Nov 15 '18 at 11:32













is it same problem with this code? func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 0 }

– canister_exister
Nov 15 '18 at 18:28





is it same problem with this code? func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 0 }

– canister_exister
Nov 15 '18 at 18:28












2 Answers
2






active

oldest

votes


















0














My guess is that you are missing data source method



optional func numberOfSections(in tableView: UITableView) -> Int


https://developer.apple.com/documentation/uikit/uitableviewdatasource/1614860-numberofsections




The number of sections in tableView. The default value is 1.




This should break your implementation when you have 0 items in your array. Based on your structure it could look similar to



override func numberOfSections(in tableView: UITableView) -> Int {
guard let arr = items else { return 0 }
return arr.count == 0 ? 0 : 1
}





share|improve this answer
























  • I have the problem with number Of Rows In Section, not with number Of Sections

    – Roman Romanenko
    Nov 15 '18 at 13:05






  • 1





    Also as I wrote above "Sometimes happens issue when one cell or few cells is froze"

    – Roman Romanenko
    Nov 15 '18 at 13:11











  • I can only assess your issue based on provided information and code. Since you don't provide your full data source implementation I have to assume the setup is wrong hence my answer. I am happy to help but you need to provided us with more implementation details.

    – Peter Pajchl
    Nov 15 '18 at 13:29



















0














The problem is here:



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

guard let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier", for: indexPath) as? CustomTableViewCell else { return UITableViewCell() }

// Cell settings

return cell

}


You are returning a cell without dequeueing it.



If you instantiate a cell yourself the TableView doesn't have control over it, so it is not able to reuse it after you update your data source.



Please change your code like this:



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier", for: indexPath) as! CustomTableViewCell

// Cell settings

return cell

}


But also make sure you are registering the cell before that, in your viewDidLoad probably.



Something like:



tableView.register(CustomTableViewCell.self, forCellReuseIdentifier:"Identifier")





share|improve this answer



















  • 1





    It seems the "func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int" returns num of cells, but not the "func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell". Also as I wrote above "Sometimes happens issue when one cell or few cells is froze"

    – Roman Romanenko
    Nov 15 '18 at 13:08











  • if there are no cells, then func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell should not be called at all. Maybe you forgot to call tableView.reloadData() after updating the data source? Besides that another problem is the instantiation of the cells, you should not instantiate them manually.

    – vauxhall
    Nov 15 '18 at 16:47











  • I knew. Therefore didn't understand your answer. I didn't forget to call tableView.reloadData(). I should add this to this to the explanation, thanks.

    – Roman Romanenko
    Nov 15 '18 at 18:17













  • But you are still instantiating (creating) cells on your own. This part specifically: UITableViewCell(). I still suggest you change your cellForRowAt like in my answer.

    – vauxhall
    Nov 16 '18 at 8:40











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316290%2ftableviewcell-is-froze-with-an-empty-datasource-array%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









0














My guess is that you are missing data source method



optional func numberOfSections(in tableView: UITableView) -> Int


https://developer.apple.com/documentation/uikit/uitableviewdatasource/1614860-numberofsections




The number of sections in tableView. The default value is 1.




This should break your implementation when you have 0 items in your array. Based on your structure it could look similar to



override func numberOfSections(in tableView: UITableView) -> Int {
guard let arr = items else { return 0 }
return arr.count == 0 ? 0 : 1
}





share|improve this answer
























  • I have the problem with number Of Rows In Section, not with number Of Sections

    – Roman Romanenko
    Nov 15 '18 at 13:05






  • 1





    Also as I wrote above "Sometimes happens issue when one cell or few cells is froze"

    – Roman Romanenko
    Nov 15 '18 at 13:11











  • I can only assess your issue based on provided information and code. Since you don't provide your full data source implementation I have to assume the setup is wrong hence my answer. I am happy to help but you need to provided us with more implementation details.

    – Peter Pajchl
    Nov 15 '18 at 13:29
















0














My guess is that you are missing data source method



optional func numberOfSections(in tableView: UITableView) -> Int


https://developer.apple.com/documentation/uikit/uitableviewdatasource/1614860-numberofsections




The number of sections in tableView. The default value is 1.




This should break your implementation when you have 0 items in your array. Based on your structure it could look similar to



override func numberOfSections(in tableView: UITableView) -> Int {
guard let arr = items else { return 0 }
return arr.count == 0 ? 0 : 1
}





share|improve this answer
























  • I have the problem with number Of Rows In Section, not with number Of Sections

    – Roman Romanenko
    Nov 15 '18 at 13:05






  • 1





    Also as I wrote above "Sometimes happens issue when one cell or few cells is froze"

    – Roman Romanenko
    Nov 15 '18 at 13:11











  • I can only assess your issue based on provided information and code. Since you don't provide your full data source implementation I have to assume the setup is wrong hence my answer. I am happy to help but you need to provided us with more implementation details.

    – Peter Pajchl
    Nov 15 '18 at 13:29














0












0








0







My guess is that you are missing data source method



optional func numberOfSections(in tableView: UITableView) -> Int


https://developer.apple.com/documentation/uikit/uitableviewdatasource/1614860-numberofsections




The number of sections in tableView. The default value is 1.




This should break your implementation when you have 0 items in your array. Based on your structure it could look similar to



override func numberOfSections(in tableView: UITableView) -> Int {
guard let arr = items else { return 0 }
return arr.count == 0 ? 0 : 1
}





share|improve this answer













My guess is that you are missing data source method



optional func numberOfSections(in tableView: UITableView) -> Int


https://developer.apple.com/documentation/uikit/uitableviewdatasource/1614860-numberofsections




The number of sections in tableView. The default value is 1.




This should break your implementation when you have 0 items in your array. Based on your structure it could look similar to



override func numberOfSections(in tableView: UITableView) -> Int {
guard let arr = items else { return 0 }
return arr.count == 0 ? 0 : 1
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 11:39









Peter PajchlPeter Pajchl

2,2321624




2,2321624













  • I have the problem with number Of Rows In Section, not with number Of Sections

    – Roman Romanenko
    Nov 15 '18 at 13:05






  • 1





    Also as I wrote above "Sometimes happens issue when one cell or few cells is froze"

    – Roman Romanenko
    Nov 15 '18 at 13:11











  • I can only assess your issue based on provided information and code. Since you don't provide your full data source implementation I have to assume the setup is wrong hence my answer. I am happy to help but you need to provided us with more implementation details.

    – Peter Pajchl
    Nov 15 '18 at 13:29



















  • I have the problem with number Of Rows In Section, not with number Of Sections

    – Roman Romanenko
    Nov 15 '18 at 13:05






  • 1





    Also as I wrote above "Sometimes happens issue when one cell or few cells is froze"

    – Roman Romanenko
    Nov 15 '18 at 13:11











  • I can only assess your issue based on provided information and code. Since you don't provide your full data source implementation I have to assume the setup is wrong hence my answer. I am happy to help but you need to provided us with more implementation details.

    – Peter Pajchl
    Nov 15 '18 at 13:29

















I have the problem with number Of Rows In Section, not with number Of Sections

– Roman Romanenko
Nov 15 '18 at 13:05





I have the problem with number Of Rows In Section, not with number Of Sections

– Roman Romanenko
Nov 15 '18 at 13:05




1




1





Also as I wrote above "Sometimes happens issue when one cell or few cells is froze"

– Roman Romanenko
Nov 15 '18 at 13:11





Also as I wrote above "Sometimes happens issue when one cell or few cells is froze"

– Roman Romanenko
Nov 15 '18 at 13:11













I can only assess your issue based on provided information and code. Since you don't provide your full data source implementation I have to assume the setup is wrong hence my answer. I am happy to help but you need to provided us with more implementation details.

– Peter Pajchl
Nov 15 '18 at 13:29





I can only assess your issue based on provided information and code. Since you don't provide your full data source implementation I have to assume the setup is wrong hence my answer. I am happy to help but you need to provided us with more implementation details.

– Peter Pajchl
Nov 15 '18 at 13:29













0














The problem is here:



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

guard let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier", for: indexPath) as? CustomTableViewCell else { return UITableViewCell() }

// Cell settings

return cell

}


You are returning a cell without dequeueing it.



If you instantiate a cell yourself the TableView doesn't have control over it, so it is not able to reuse it after you update your data source.



Please change your code like this:



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier", for: indexPath) as! CustomTableViewCell

// Cell settings

return cell

}


But also make sure you are registering the cell before that, in your viewDidLoad probably.



Something like:



tableView.register(CustomTableViewCell.self, forCellReuseIdentifier:"Identifier")





share|improve this answer



















  • 1





    It seems the "func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int" returns num of cells, but not the "func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell". Also as I wrote above "Sometimes happens issue when one cell or few cells is froze"

    – Roman Romanenko
    Nov 15 '18 at 13:08











  • if there are no cells, then func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell should not be called at all. Maybe you forgot to call tableView.reloadData() after updating the data source? Besides that another problem is the instantiation of the cells, you should not instantiate them manually.

    – vauxhall
    Nov 15 '18 at 16:47











  • I knew. Therefore didn't understand your answer. I didn't forget to call tableView.reloadData(). I should add this to this to the explanation, thanks.

    – Roman Romanenko
    Nov 15 '18 at 18:17













  • But you are still instantiating (creating) cells on your own. This part specifically: UITableViewCell(). I still suggest you change your cellForRowAt like in my answer.

    – vauxhall
    Nov 16 '18 at 8:40
















0














The problem is here:



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

guard let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier", for: indexPath) as? CustomTableViewCell else { return UITableViewCell() }

// Cell settings

return cell

}


You are returning a cell without dequeueing it.



If you instantiate a cell yourself the TableView doesn't have control over it, so it is not able to reuse it after you update your data source.



Please change your code like this:



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier", for: indexPath) as! CustomTableViewCell

// Cell settings

return cell

}


But also make sure you are registering the cell before that, in your viewDidLoad probably.



Something like:



tableView.register(CustomTableViewCell.self, forCellReuseIdentifier:"Identifier")





share|improve this answer



















  • 1





    It seems the "func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int" returns num of cells, but not the "func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell". Also as I wrote above "Sometimes happens issue when one cell or few cells is froze"

    – Roman Romanenko
    Nov 15 '18 at 13:08











  • if there are no cells, then func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell should not be called at all. Maybe you forgot to call tableView.reloadData() after updating the data source? Besides that another problem is the instantiation of the cells, you should not instantiate them manually.

    – vauxhall
    Nov 15 '18 at 16:47











  • I knew. Therefore didn't understand your answer. I didn't forget to call tableView.reloadData(). I should add this to this to the explanation, thanks.

    – Roman Romanenko
    Nov 15 '18 at 18:17













  • But you are still instantiating (creating) cells on your own. This part specifically: UITableViewCell(). I still suggest you change your cellForRowAt like in my answer.

    – vauxhall
    Nov 16 '18 at 8:40














0












0








0







The problem is here:



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

guard let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier", for: indexPath) as? CustomTableViewCell else { return UITableViewCell() }

// Cell settings

return cell

}


You are returning a cell without dequeueing it.



If you instantiate a cell yourself the TableView doesn't have control over it, so it is not able to reuse it after you update your data source.



Please change your code like this:



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier", for: indexPath) as! CustomTableViewCell

// Cell settings

return cell

}


But also make sure you are registering the cell before that, in your viewDidLoad probably.



Something like:



tableView.register(CustomTableViewCell.self, forCellReuseIdentifier:"Identifier")





share|improve this answer













The problem is here:



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

guard let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier", for: indexPath) as? CustomTableViewCell else { return UITableViewCell() }

// Cell settings

return cell

}


You are returning a cell without dequeueing it.



If you instantiate a cell yourself the TableView doesn't have control over it, so it is not able to reuse it after you update your data source.



Please change your code like this:



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier", for: indexPath) as! CustomTableViewCell

// Cell settings

return cell

}


But also make sure you are registering the cell before that, in your viewDidLoad probably.



Something like:



tableView.register(CustomTableViewCell.self, forCellReuseIdentifier:"Identifier")






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 11:49









vauxhallvauxhall

793611




793611








  • 1





    It seems the "func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int" returns num of cells, but not the "func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell". Also as I wrote above "Sometimes happens issue when one cell or few cells is froze"

    – Roman Romanenko
    Nov 15 '18 at 13:08











  • if there are no cells, then func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell should not be called at all. Maybe you forgot to call tableView.reloadData() after updating the data source? Besides that another problem is the instantiation of the cells, you should not instantiate them manually.

    – vauxhall
    Nov 15 '18 at 16:47











  • I knew. Therefore didn't understand your answer. I didn't forget to call tableView.reloadData(). I should add this to this to the explanation, thanks.

    – Roman Romanenko
    Nov 15 '18 at 18:17













  • But you are still instantiating (creating) cells on your own. This part specifically: UITableViewCell(). I still suggest you change your cellForRowAt like in my answer.

    – vauxhall
    Nov 16 '18 at 8:40














  • 1





    It seems the "func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int" returns num of cells, but not the "func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell". Also as I wrote above "Sometimes happens issue when one cell or few cells is froze"

    – Roman Romanenko
    Nov 15 '18 at 13:08











  • if there are no cells, then func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell should not be called at all. Maybe you forgot to call tableView.reloadData() after updating the data source? Besides that another problem is the instantiation of the cells, you should not instantiate them manually.

    – vauxhall
    Nov 15 '18 at 16:47











  • I knew. Therefore didn't understand your answer. I didn't forget to call tableView.reloadData(). I should add this to this to the explanation, thanks.

    – Roman Romanenko
    Nov 15 '18 at 18:17













  • But you are still instantiating (creating) cells on your own. This part specifically: UITableViewCell(). I still suggest you change your cellForRowAt like in my answer.

    – vauxhall
    Nov 16 '18 at 8:40








1




1





It seems the "func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int" returns num of cells, but not the "func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell". Also as I wrote above "Sometimes happens issue when one cell or few cells is froze"

– Roman Romanenko
Nov 15 '18 at 13:08





It seems the "func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int" returns num of cells, but not the "func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell". Also as I wrote above "Sometimes happens issue when one cell or few cells is froze"

– Roman Romanenko
Nov 15 '18 at 13:08













if there are no cells, then func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell should not be called at all. Maybe you forgot to call tableView.reloadData() after updating the data source? Besides that another problem is the instantiation of the cells, you should not instantiate them manually.

– vauxhall
Nov 15 '18 at 16:47





if there are no cells, then func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell should not be called at all. Maybe you forgot to call tableView.reloadData() after updating the data source? Besides that another problem is the instantiation of the cells, you should not instantiate them manually.

– vauxhall
Nov 15 '18 at 16:47













I knew. Therefore didn't understand your answer. I didn't forget to call tableView.reloadData(). I should add this to this to the explanation, thanks.

– Roman Romanenko
Nov 15 '18 at 18:17







I knew. Therefore didn't understand your answer. I didn't forget to call tableView.reloadData(). I should add this to this to the explanation, thanks.

– Roman Romanenko
Nov 15 '18 at 18:17















But you are still instantiating (creating) cells on your own. This part specifically: UITableViewCell(). I still suggest you change your cellForRowAt like in my answer.

– vauxhall
Nov 16 '18 at 8:40





But you are still instantiating (creating) cells on your own. This part specifically: UITableViewCell(). I still suggest you change your cellForRowAt like in my answer.

– vauxhall
Nov 16 '18 at 8:40


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316290%2ftableviewcell-is-froze-with-an-empty-datasource-array%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

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly