How can i do some pagination with the parsed data in iOS - iphone

In my application i parsed some JSON RSS feed and stored the parsed data into a NSMutableArray.
here ,i want to display only 4 items in my tableview at a time . Below the tableview , there a UIButton 'more'. When the user clicks this button, i want to display another 4 like that ..... How to do that.
Can any one give me an idea or point me any simple example that i can refer . How can i do some pagination with the parsed data . Thanks in advance .......

http://www.iphonedevsdk.com/forum/iphone-sdk-development/6676-paging-tableview.html

As you already know there is a data source for a uiTableView. You can easily achieve this as your requirement needs a button press. Pagination without button press on scroll of the table view would be tricky.
You have to create a NSMutableArray as a data source. Everytime the more button is pressed add 4 items to the end of the NSMutableArray.
Using NSRange you can get the objects from the main array and add them 4 at a time to the data source. Once you add call [tableview reload];
Also you can create the data source again
NSArray *dataSource = [mainArray subarrayWithRange:NSMakeRange(0, PageNum*4)];

Related

Deleting rows UItableview iphone using userdefaults

I need your help, maybe its something silly but I can't find the error.
I have a table view and an add button, when its clicked I show a modal view with a little form, when save button is pressed I send the data to the TableView controller to show it, also I'm using NSUserDefaults to save this data in an array.
My problem comes when, for example, I add 2 new rows and delete 1 of them, then when I add another, it shows the last row I delete instead of showing the one I just add.
I'm doing the saving and reading this way:
in viewDidAppear I read the NSUserDefaults and get the data if exist.
in the method that catches the data from the ModalView I save to userdefaults.
in commitEditing method I read userdefaults and then delete the row from the array and from the table and then save this change in userdefaults.
I don't know what I doing wrong, Can anybody help me with this?
Use [self.tableView reloadData]; in the viewWillAppear of that class. You can also reference that class in another view controller, create an object for it and call [classObject.tableView reloadData];
Always remember to reload the tableView after an add operation or a delete operation.
Don’t use NSUserDefaults to act as the direct datasource of a table. Store it in an intermediate model. It’ll be a LOT easier to debug that way.
If you’re having trouble keeping your model synced with the user defaults, call [[NSUserDefaults standardUserDefaults] synchronize] whenever you alter them.
Also, make sure you’re not making some mistakes in your table cell reuse. You might be seeing old data if you have a customer UITableViewCell class and you’re not correctly implementing -prepareForReuse.

cant able to exchanges elements in array

hi i am new to iPhone.what i did is creating an array with images as shown below
images = [NSMutableArray alloc] initWithObjets:#"image1.jpg",#"image2.jpg",#"image3.jpg",#"image4.jpg",#"image5.jpg",#"image6.jpg",nil]
and i create a button when ever button click images are exchanged for that i am writing code as fallows in button clicked event.
[_images exchangeObjectAtIndex:2 withObjectAtIndex:4];
but images are not exchanged and i did n't get any warnings and images.i am writing array in viewDidLoad.what is the wrong.how can i exchange elements in the array. thank u in advance.
Are you getting an array of images to then put into a UIImage so it goes through all the images one by one?
If so then when you change this array it will not change the order of the UIImages. You will need to change the array as you currently are then assign the array to the UIImage again like you would have done before. This would then show you the new and hopefully correct sequence of images.
I may be a bit wrong and you are using each location in the array to create a UIImage then put all those UIImages into a UIImageView. But what i am saying still applies, you would have to assign the new array to the UIImageView.

Saving data when navigating

how can i save the data in from texfield when i navigate back to previous view?
i have set the app to save all data to plist when terminate but i when i am navigating up and down view, i need to save the data to some place. any suggestion? save data when "back" button at nav bar is press? but how? save to NSUserDefaults?
Have you tried to use session variables? You can store all the fields in session vars and everytime a user visits that page, it will first check to see if the session vars are set. If so, render the form with those input fields pre-populated with the session var values. If not, then render a blank form.
You may consider storing the contents of the text field to an NSString * instance variable of your application delegate. The best place to do so would be viewDidUnload method, provided that you did retain the text field.
If you are using NSuserDefaults i used this to save my tableview data when the back button was pressed. list is my NSMutableArray that holds my data
(void)viewDidDisappear:(BOOL)animated{
[NSKeyedArchiver archiveRootObject:self.list toFile:[self dataFilePath]];
}

how do I take the URL and page title from a UITableView?

I have an iPhone app that shows the user a UIWebView in the first view, what I am trying to do is allow the user to save what is essentially favorites to be saved and then displayed in a table view in the second view. To do this all i need is the page URL and also the Page Title to be saved to then populate the table view. Now here is the problem I am just starting to learn how to make iPhone apps and I have no clue what would be the best way to save it and how to get this information from the web view?
Implement the UIWebViewDelegate protocol. You can obtain the URL from the request object in the delegate method -webView:shouldStartLoadWithRequest:navigationType:
To extract the title from the HTML, run some Javascript on the content using – stringByEvaluatingJavaScriptFromString:. Make sure you do this after the content is fully loaded, i.e. in the delegate method - (void)webViewDidFinishLoad:(UIWebView *)webView
To populate and display in a TableView, you have to have some sort of a DataSource, preferably an NSArray, since you shall be appending or altering this array, use a NSMutableArray. Now to save two bits of information, the URL and the Title, have two arrays, one for Title and one for the URL. (Since the title can be customised).
In the TableView delegate, where you return the number of rows, return [URLArray count];
and in the function to populate the cell, set the Text to [titleArray objectAtIndex:[indexPath row]] and the SubTitle to [URLArray objectAtIndex:[indexPath row]];
Hope this resolves your question and is what you were looking for.

UiTableView records Problem

i Have some problem. i have 25 records in my NSMutableArray and i want to display them 5 records at a time when user press Next button then another 5 records to display and when user press back button then previous 5 records to display.
How can i do this any help from you would be great.
Thank You.
Make an ivar in your table view controller that tracks which records you should display. Set the ivar in your Next/Back buttons action method and send [self.tableView reloadData]. Use the ivar in -tableView:numberOfRowsInSection: and -tableView:cellForRowAtIndexPath: to determine how many and which cells to display.
First of all, I recommend against this design. Tableviews on iPhone apps are great to scroll with your fingers, and having buttons like this defeats this. However, I don't know what you're doing, so I'll assume it's cool.
My suggestion is to use slices of the larger array as follows:
- (NSArray *)itemsForPage:(NSUInteger)p {
NSIndexSet *indexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(5*p,5)];
return [_thatOneLargeArray objectsAtIndexes:indexes];
}
Note that I've refactored this code into a method. Let me know if this is helpful!