numberOfSectionsInTableView is requested in/after viewDidLoad ONLY if tableview is empty - ios

A simple tableviewController, empty. A modal that can be launched from a button. No data in the data source for the tableview, and no rows displayed.
I open the modal, use it to add an item, and return to the tableview controller. The tableview updates itself automatically, and the new row is displayed.
I add a second item. The table view does NOT update automatically.
I can tell by logging inside numberOfSectionsInTableView that even if I go to add the first item and cancel, the tableview refreshes - it asks for the number of sections, rows, and the cell to display (if there is one). But not if there is one pre-existing row in the table.
I've scoured my code but can't find anything that would cause the tableview to know when the first item is added, but none afterwards.
Any ideas?
EDIT - I have further narrowed my issue so will close this shortly and have posted a more specific question at Why does an empty tableView check the number of sections but a non-empty one does not?

from Apple's documentation:
UITableView overrides the layoutSubviews method of UIView so that it
calls reloadData only when you create a new instance of UITableView or
when you assign a new data source.
maybe you are setting data source of table view after showing that modal? So at the second time data source does not changes and tableView does not update.
another key (I'm not sure about that) may be the showing of the modal first time. The method -addSubview for modal is causing -layoutSubviews.

Related

UITableView Cells Disappear after Dismiss View That I Add

I'm having a difficult time understanding what I should even be looking for to debug this issue.
I have a UITableView that populates correctly.
I select one of the UITableViewCells which upon selection presents a new view to the user with more detail.
If I click Back on this new window, the original UITableView shows none of the UITableViewCells anymore.
I guess the problem is when you are coming back, your array is being empty somehow. In your first "ViewController" override the "viewWillAppear()" method and print the count of your array in this method. Then load the app and check the print result, then go to second "ViewController" and press the back button and see the print results again. if it shows zero "0" then check your code and try to find out where your array is being empty.
Hope this will help.
I ended up setting the delegate and datasource in the viewDidAppear() rather than the viewDidLoad(). Once I did that, I added a tableView.reloadData() inside of the completion handler for retrieving the data from the API and the tableView was then being populated as expected.

How to refresh a UITableView in iphone?

I have a UI table view loading data from network. If the user move the finger on one cell, one button will be displayed, just like iPhone style delete button. If the user click this button, the value of one label in the cell will be changed accordingly. However, I didn't find any way to make table view to re-draw this cell, except for reload the whole table. I don't want to use reload data method, because I just need change the value in one cell instead of the whole table. Any suggestion on this?
Take a look at the method
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
Calling this method will result in your table view's data source asking its delegate for information about that cell.
For future reference, I found this very easily by checking the documentation on UITableView. I suggest you do that in the future before posting here.

How to call EditTable method of UITableView from some other class?

I want to edit UITableView by calling EditTable method on a button click of some other class. In other words, I want to delete rows from UITableview from some other viewcontroller without navigating to that UITableView.
Please tell me the way to achieve this or any sample code for it.
If you want to delete rows from a tableview you just need to delete the corresponding rows from the datasource. When the table is next shown it should know how to reload it's data so the changes in the datasource are applied to the table.
Edit
You shouldn't rely on sending messages to view controllers that are deeper in the stack because they may have been unloaded already.

UITableView initial row selection

Maybe I'm missing something obvious, but I'm have a hard time programmatically setting the row selection in a tableView. The goal is to simply have a tableView open with a row already selected. The problem appears to be that I have to wait until the tableView is fully loaded before I can modify the selection.
I've read various strategies such as calling reloadData for the tableView in the viewController's viewWillAppear method, then immediately calling selectRowAtIndexPath for the target row. But when I do that, I get a range exception because the tableView has zero rows at that point. The UITableViewDelegate methods (numberOfRowsInSection, etc.) don't appear to be called immediately in response to reloadData (which makes sense if the table rows are drawn "lazily").
The only way I've been able to get this to work is to call selectRowAtIndexPath after a short delay, but then you can see the tableView scroll the selected row into view.
Surely, there's a better way of doing this?
Well, you can use another strategy. You can create a hidden table view, configure how you want and than show to user. Use the tableview.hidden = YES.

Updating a UITableView's data

In my application, I have a UITableView with a few rows (let's call it TV1), now, at the bottom of these rows is a row that drills down into another (TV2). This new TV2 asks you which type of data you would like to add to the first view. Then, when the user selects which kind, they're brought to another view, in which they fill in some fields, the data is saved, and they're sent back to TV1, however, the data they entered, isn't loaded and I'm not really sure how to do this. Any ideas?
Maybe you did this already, but how about using [TV1 reloadData];? This forces the entire table to reload, yet there are also more specific methods for reloading just the cells that have been altered. See also the documentation: UITableView
Reload your table view by calling [self.tableView reloadData] in the viewcontroller's viewWillAppear: method. It will be called when the user switches back to your view. Right before it is actually put on screen.

Resources