Reload UITableView in edit mode - uitableview

I have a dictionary that holds every item in a list. I then have a plist that holds user information for each item in the list, including if the item has been collected or not. Then I load a UITableView with information based on the plist against the dictionary, only displaying items that the user still has not collected.
Users can tap on each row and navigate to a UIView page with information for the item with a static button to add the item to the completed list. When the button is pressed the program changes the "Completed" field related to the item in the users plist file to "YES" and when the user navigates back to the UITableView the item is removed from the list by calling [self.tableView reloadData] in the viewDidAppear function.
I also want the user to be able to enter "Edit mode" in the UITableView and select multiple rows to be added in one go. I have managed to get the functions working correctly, using self.tableView.allowsMultipleSelectionDuringEditing = YES; and NSArray *temp = self.tableView.indexPathsForSelectedRows; but the tableView will not refresh the screen until the user navigates out of the page and back in again. I have tried adding the [self.tableView reloadData] in different functions but none of them work.
Thanks in advance for any help.

I cannot think of a reason why reloadData should not work, but IMO you should try
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation
instead of reloadData(if you haven't already).
Also http://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html#//apple_ref/doc/uid/TP40007451-CH10-SW9 this could be of help.
Let me know if this works for you.

Related

Reload TableView Tab Bar Application

I have a tab bar application that displays tableviews in two of the tabs. The tableviews are populated using NSFileManager to read the contents of a plist file stored with user data. One of the tabs displays the "complete" items, the other tab displays the "Incomplete" items. As the user selects a row, a page with the details of the item selected is displayed. In the incomplete table there is a button the user can press to move these details from the "Incomplete" table to a "Completed" table. The plist is then updated using NSFileManager to change a field detailing if the item is in the complete or the incomplete list for this item.
The problem I have is that the changes are not updated on the two tables just by selecting between the two tabs. The user has to quit the application and start it up again to see the item moved from one table to the other.
The data seems to be updated its just the view that doesn't update.
I'm new to cocoa so any help at all would be greatly appreciated.
As you haven't provided the code, so i cannot tell where exactly you should reload your tableview.
You have to call
[self.tableView reloadData];
just after you are finished using NSFileManager to save your data in the plist.
Table views are not updating because you are probably not calling [self.tableview reloadData]; when you call reloadData for a tableview it should update the tableviews.
Since you are using tab bar controllers when each view is pushed to the stack it stays there you need to detect if view is appear again and call reloadData method.
in completed.m add this:
-(void) viewWillAppear:(BOOL)animated{
[self.tableview reloadData];
}
in incomplete.m detect if NSFileManager is saved data then call [self.tableview reloadData];
there are also other ways to solve this problem, you can communicate between viewcontrollers via Delegate or NSNotifications pattern. So lets say you saved the data you call your delegate or NSNotification method and receiver view controller runs your desired method. In your case refreshes the tableview
How do I set up a simple delegate to communicate between two view controllers?
http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_nsnotificationcenter/

How to know that no row is selected in my UITableView?

I use a UITableView to display a list of items, and when a row is selected I show that item in a detail view next to it (using a split view controller).
As the selection in the table view changes I want my detail view to change as well, which is of course standard behavior and it works flawlessly... except when no row is selected anymore in the table view.
Users can for example delete a row from the table view, and during and also after that deletion there will be no selected row in the table view. Of course after a row is deleted I let my detail view controller know, but during the editing process in the table view, when no row is selected, my detail view controller does not know this.
I tried to use the UITableViewSelectionDidChangeNotification but that only gets sent when the user selects a different row, not when a deselection occurs.
How can I be notified of the fact that the table view switches from having a row selected to having no selection at all?
EDIT:
As a temporary solution, I tried renaming the Edit-button to "Reorder" and only allowing moving rows from A to B but without allowing the deletion controls, but this can not be done, there is no move control without enabling editing on a row. The thing is, I do get a pointer to a row that is up for moving, so I can keep that selected. I do not get this pointer for a row up for deletion, so no go for me. I always want to know what row a user works on, and keep that row selected at all times.
I may have to resort to ditching the standard editing behavior and adding my own buttons and methods for it. Or see what gestures can do for me to capture touches on any row...
There is a property indexPathForSelectedRow. I'm not sure what it would return when there is no selected row, but it may return nil or something like that, which you could use as a trigger to know if there are any rows selected. Something like
if (myTable.indexPathForSelectedRow == nil)
{
//do something here to account for no rows being selected
}
else
{
//do stuff here to set up your detail view
}
Let me know if this works for you.
How are you implementing the deletion of rows? Are you implementing the following data source method?
tableView:commitEditingStyle:forRowAtIndexPath:
You can have a property like
NSIndexPath *selectedIndexPath
and set it to nil when a row is being deleted.
Example:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//select another row in your table
[tableView selectRowAtIndexPath:*indexPathOfTheRowToBeSelected* animated:YES scrollPosition:UITableViewScrollPositionNone];
//then delete row from data source
}
The method above will be called when the user touches the delete button on the table he/she has selected to delete.
Okay I misread what you were asking at first, I'm pretty sure what you want is either – tableView:willDeselectRowAtIndexPath: or – tableView:didDeselectRowAtIndexPath:. These methods will be triggered automatically when the user deselects a row (either right before the row is deselected or right after, respectively). Implement one of these in your table view delegates code, and you'll know whenever a row gets deselected, and you can put your code to select the next row or whatever you want in one of these methods.
I struggled with this as well. Turns out that when the "Edit" button is hit, all rows are deselected at that point. You can override setEditing:animated: to detect when it is hit, but you must call [super setEditing:editing animated:animated] before you return.
See this link for where I found this:
How can i identify self.editButtonItem button's clicked event?

Refresh Table View Data does not work

I have a UITableView, named Favorites section. If i add an entry to the favorites section, i can see it loaded correctly in the favorites section, but if i continue and try to add another entry, it will only work if i go to favorites section, then the original section and then back to the favorites section, as if something is holding her from refreshing.
I tried to put [UiTableView reloadData] in the viewWillAppear , but still no result. I even tried [self.tableView reloadData] and still. Any ideas what to do?
Note that i am using a tab bar
Please make sure you have properly added your record in datasource, and after that only u have reloaded data.
You can just reload a section if you know in which section you are adding else, you can use insertRows method to just update new record in your table, reloading entire table for a row is bad practice.
Hope this helps

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

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.

Possible to allow a uitableview to allow multiple and single selections?

Essentially I have a list of items, I want to have some type of checkbox on the end where they can select multiple items. I also need them to be able to just select one of the rows and have it open up a detail view. Is this possible? Or do I need to do something like they do with the messages where you choose the edit button to select them and show an additional button to take action on the selected?
Set your cell's UITableViewAccessoryType property to UITableViewCellAccessoryCheckmark to make a check mark show up on a certain cell, you'll have to keep track of which cell's are checked on your own though, since this property doesn't track.
So have an array or something that represents which cells ar checked and which aren't, then in your didSelectRowAtIndexPath: method you can modify the data in the array to reflect the cell's selected state, and call [tableView reloadData] so the checkmark shows up right away.
I found the answer, there's an option to allow multiple selections on edit mode.

Resources