ReloadData calls numberOfSections, numberOfRows, not cellForRowAtIndexPath

First of all, it's a pity if it is not formatted correctly, the first time it is done. I used stackoverflow to find help for a long time, and it was very helpful (thank you all), but this is the first time I posted my question. This question has been asked many times, but when I call [myTable reloadTable], the methods numberOfSectionsInTableView and numberOfRowsInSection are called, but not cellForRowAtIndexPath.

Each answer that I saw during the search was changed: 1) TableView is zero 2) numberOfRowsInSection - 0 3) the delegate / data source tableView is not set 4) the reloadTable call in the wrong uiTableView.

None of them are suitable for me, so I wonder what else might be wrong.

What I'm trying to do: I have a custom uitableviewcell with a button on it when the button is pressed. I want to change the attributes of a cell (for example, let me say that I want to change the name of the cell to "Button Pressed"). Then I want to pause for a moment, and then delete the cell.

Not sure if this is important, but my tableView is inside a UIViewController, and viewController is a delegate and dataSource.

I have a method (below) that fires when I click a button and change the attributes of a cell if necessary, however, when I try to update a table to show the changes, it does not work. The first time [remTable reloadData] is called, numberofsectionsintableview and numberofrowsinsection are called, but not cellforrowatindexpath. However, the second time [remTable reloadData] all three methods are called, and everything works correctly.

doneCell.remName.text=@ "BUTTON PRESSED"; [remTable reloadData]; NSLog(@"sleep"); sleep(1); [remList removeObject:doneReminder]; [remTable reloadData]; 

To test this, I put the nslog statements at the beginning of numberofsections, numberofrows and cellforrow, the output is the name of the method followed by the number (for numberofsections / numberofrows).

Output:

 numberofsections 1 numberofrows 3 sleep numberofsections 1 numberofrows 2 cellforrow 

Any ideas on why cell rage isn't called for the first time? Thanks in advance, please let me know if anything else I can add to clarify.

+4
source share
3 answers

The table view does not actually request its cells until it is necessary to display them. When you call reloadData , it immediately asks for the number of sections and lines so that it knows how necessary it is. He does not request the cells themselves until he is asked to display. Views are automatically displayed as needed at the beginning of each run cycle.

Execution does not return to the execution loop until your code returns. When you call sleep , you are not executing any code, but also not returning. This means that the run loop does not get the ability to display the table, so it never queries for any cells. What you need to do is return from your method and ask the other method to be called after 1 second to remove doneReminder . An easy way to do this is to use the performSelector:withObject:afterDelay: . This method adds a timer that will call the method that you requested after the specified delay, which means that you can return to the execution loop and show the table.

 doneCell.remName.text=@ "BUTTON PRESSED"; [remTable reloadData]; [self performSelector:@selector(removeDoneReminder) withObject:nil afterDelay:1.0]; 
 - (void)removeDoneReminder { [remList removeObject:doneReminder]; [remTable reloadData]; } 

If remlist and remTable are not instance variables, you can use the withObject: parameter to send them to the removeDoneReminder method.

+8
source

CellForRowAtIndexPath is part of the UITableView data protocol, make sure remtable.datasource points to self

so when you initialize the tableview you must add

 remtable.datasource = self; 

i always did this after

 remtable.delegate = self; 

Hope for this help

0
source

Try using delayed performance:

 [remtable performSelector:@selector(reloadData) withObject:nil afterDelay:0.1]; 

I don’t know if this will help, because I don’t know what else you are doing.

0
source

Source: https://habr.com/ru/post/1396759/


All Articles