Rearrange Uitableview row - iphone

I have an iPhone application that has a user preference page where the user can rearrange the uitableview rows just like the iPhone built in weather forecast app's settings page. Now my problem is after the user in done with rearranging, I need to save the data in database in rearranged order. But I'm not been able to read the data from the uitableview.
How can I read the rearranged data from the uitableview?
Looking for your valuable response
Thanks in advance
Joy

You do not read the rearraged order at the end of the editing, UITableView tells it's dataSource what is being changed while the editing takes place. It does so using the messages:
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath;
You would implement your datasource, so that it records this changes and can store them when the editing is finished.
Another not recommend way would be the to enumerate your tableView cell's using
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Then figure out with dataEntity is represented by this cell, and reconstruct the underlying data.
The last approach is only for completeness, please use the first.

Related

Tableview delegate method willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath not called for every row when scrolling really fast

I load more data from server when i am 5 rows from the end of my tableview.
I use (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath for this.
It works most of the time, but sometimes when i scroll really fast, a row gets skipped and so my function for loading more data is not called.
How should i fix it, so willDisplayCel delegate function will be called for every row that gets scrolled by?
Are the cells still visible for which the method is not called or have you skipped them by scrolling real fast?
This method will only be called for cells that are visible - so if you scroll fast the system thinks that they are not being displayed anyway.
I would suggest to change the logic on when to load more data - since no connection will be fast enough for this to get you the data from the server.
You may either use the bouncing technique or have the very last line of your table view to initiate loading more lines. This way you can be sure that the app and the user will wait a little for the data to be retrieved from the server.

NSUrlConnection with table view and search display controller

I am writing an iPhone app which gets data from a mysql database. I am trying to use a search display controller to search the database as the user types input into the search bar, but the problem I am having is that uitableview methods are called before the nsurlconnection methods.
My current coding logic is as follows;
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
//code to start connection
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
//populate array of search results
//Load data into search results
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Does anyone know how to make this work or know of another method of doing this? If something is unclear or you need some more information please let me know.
Thank you for your help.
Are you calling [tableView reloadData] after populating search results?

is there any if statement for showing/hiding a method in objC?

i use same uiviewcontroller's instance in different tabs.
there is a uitableview in viewcontroller.
in firstviewController instance, i dont wanna edit the uitableview.
in the second one i use edit mode for tableview.
thats why i want to show or hide this method:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
is it possible to make an if statement like this:
#if (editingOK)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
....some codes
}
#endif
editing OK is a BOOL property.
if you ask why i want it, because if the user swipes on the cell, it display Delete button.
i just want it if my editingOK=YES.
The #if/#endif syntax is used for conditional compilation: it lets you modify your program at compile time based on build configuration. Read about the "C preprocessor" to learn more.
If you are, as you say, using the same object instance as the delegate of different UITableViews, you must have some way to determine which table you are dealing with.
What you need to do is implement an additional method:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
That method is called when the user swipes the cell, and you can decide if a delete button should appear or not, then return the appropriate UITableViewCellEditingStyle constant.
Isn't editability controlled by calling the setEditing method of the UITableViewController? So you could set that depending on whether or not you want to enable editing, w/o this #ifdef ugliness.

how can I put particular action as I select any row of UITableViewController?

I have an table and some item on it, now I want to call a method as I select particular row and like some sort of alert msg with index which in selected,
Any Solution????
All you need is implement this method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
For more details, you can just do a search in your Xcode's developer documentation.

Deleting custom cell in Table View

I am using a Table View Cell to draw custom cell in table view. Now the problem is that when I try to delete the cell in editing style UITableViewCellEditingStyleDelete only the
the little red -ve sign bitton appear and when I click it nothing happen.
Please help me and tell me how to delete custom cell in table view
Thanks
Gyani its not at all possible that when you click on (-) sign nothing happens.
May be your tables user interaction is disabled. Try enabling tableView.userInteractionEnabled=YES
And still it does not work then post some code.
hAPPY cODING...
did you implement the appropriate delegate methods? Specifically
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
Did you implement this delegate method by following form?
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// remove the cell from UITableView
[tableView deleteRowsAtIndexPaths:indexPath withRowAnimation:YES];
// remove the data from self array or something else.
...
}
}
Do you have a subview in the cell that stops the user interaction? maybe located in negative x, so it is over the delete button.