UiTableView records Problem - iphone

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!

Related

Page reloading in iPhone Application

Summary of the issue:
When I am loading some data into my iPhone application from a webservice, it populates the tableview two times with double number of records. I found that Viewdidload method is called two times causing this issue.
Scenario
From the first view I parse the webservice and collect the data into an array object.
This array object is being used by the second view to populate the tableview.
I am wondering what is behind reloading of the page causing the tableview has double number of records. Hope somebody can give me a hint.
You may want to consider calling applicationDidFinishLaunchingWithOptions in your app delegate, as this is only called during launch.
Thanks for the tips. What I found the issue was the way adding the view. i replaced [self.view addSubview:trlist.view] with [self presentModelViewController:trlist animated:NO]; and now it loads only one time.

`[[self tableView] reloadData]` changes the NSMutableArray count

I have an app that uses a UITablewView and in that TableView there is an NSMutableArray that holds the data to be displayed in the TabelView.
The problem is in my TableView Controller I use:
[[self tableView] reloadData]
To reload the TableView data and it works fine for a couple of times but doesn't work on the 3rd time and instead it changes the array count in a weird way.
Now I read similar question here and most likely I have 2 instances of the TableView or the TableViewController but I can't figure out how to troubleshoot this? How to detect if there are 2 instances of the object and fix that?
EDIT: Here is a pic how my navigation goes
So the 1st round of going from ScrollView to TableView things show up correctly. Then I go to UIView and back to TableView and it's working fine. So I go back all the way to ScrollView and back again to TableView and it's good.
But when I go to the UIView for this time then back to TableView this problem happens. TableView doesn't display the changes. So I put a breakpoint just before [[self tableView] reloadData] and found out that the array _displayedObjetcs gets its count changed. -1 actually. see the following screen shots b4 and after I step over the reloadData
and then
My immediate thought is that your array is not being retained properly, by your application. Your "weird" count is probably a number like 234791..., which is highly indicative of an array that has been released incorrectly. Therefore, I would check how your are retaining and releasing this array.
However, without more posted code, it is impossible to help you further.

iphone - scrollRectToVisible issue

This functions perfectly, but I want to make it an once-function, not fixed-function. When I change tableview with other data, the data displays at the index from previous tableview. So my solution to this is implementing the code below. When I implement this, it works, but when I scroll down, it scrolls up all the time, so it is virtually impossible to scroll down further. Any idea how to make it performs only once when I change tableview?
The code:
[tableView scrollRectToVisible:CGRectMake(0,0,1,1) animated:NO];
Edit 21 august:
Guys, thank you very much! The code was in cellforrowatindexpath. I moved it to inside the function which changes tableview and it works like a charm :D
You could override the reloadData method if that is how you are reloading the Table View with new data and put the code in there. Something like this in your table view controller should suffice:
- (void)reloadData {
[super reloadData];
[tableView scrollRectToVisible:CGRectMake(0,0,1,1) animated:NO];
}
If it's scrolling up every time you scroll down, I assume you put the code in the cellForRowAtIndexPath method which will get called every time you want to present the cell. This is not the correct place to put that code.
It IS a once-function. Most probably, this code of yours is executing again & again. If you have kept this in a function such as cellForRowAtIndexPath:, which is called frequently, that may be the cause of this problem. Where have you put it?
HTH,
Akshay

TableView's viewWillAppear() called but data is not refreshed

Sometimes tiny looking problem is ignored to handle in the last but you never know that will become nightmare for you, happening with me.
Thanks in advance, problem my app's settings page contains tableView and every setting element is one of the view. One of the setting item (row) offers show expands a few list of items in another listTableView. After making a selection in listTableView when i come back to my settings tableView using navigationItem.leftButton (Back), selected item value overlaps the previous item's value, in the sense [tableView reloadData] in viewWillAppear works fine but tableView rows are not fresh drawn. It doesn't refresh the cell's UI.
Note that if settingTableView has any subview like UIButton etc it has the latest value, i can use that as workaround but the only problem is when is select this row again Selection has old value that overlaps new value on selecting the row.
I will appreciate any suggestion or sample code using will be great help.
Thanks a ton
Amit Singh
Use [tableView reloadData]; in your viewWillAppear method.
or use [tableView reloadData]; in viewDidAppear method
The problem you are facing is perhaps due to portion of cell that is reusable and the one that is not reused. Check out the code you are writing inside the block of
if(cell==nil){}
components you have created inside block will not get recreated and others might be recreating causing the overlapping on the cell.
In my case, I had to use [self.tableView reloadData]; rather than [tableView reloadData]; in the viewWillAppear method.

Problem reloading UITableView

I have a UITableView which is populated by an array, I have a button on the navigaton bar which (when pressed) adds an item to the array and calls [self.tableView reloadData] in the UITableView. This results in numberOfRowsInSection being called and returning the correct number of rows (the number of items in the array) BUT doesn't call cellForRowAtIndexPath.
I have created a new navigation based application to try and find a solution but have exactly the same problem!
If anyone knows the answer it would be greatly appreciated, i've been tearing what's left of my hair out for the last day!
I have put the source for the test project up on my site at www.sofaracing.com/Downloads/Test3.zip
Works for me! ;)
In MainWindow.xib, You added an object Root View Controller (that's not part of the Navigation Controller) - Delete it, not necessary. Then connect outlet refreshFriendsList to the Bar Button of the NavigationItem of RootViewController. Wha-la, magic!
BTW: You may need to clean up the warning. And you might want to think about creating a class for your data model instead of using UIApplication sharedApplication.
I quickly debugged your test app. I couldnt't spot the root cause, but it seems that you have two different table views due some mess up in Interface Builder setup.
If you initialize original array with an item, cellForRowAtIndexPath is correctly called. If you examine self.tableView instance in this call and later in subsequent calls to refreshFriendsList, self.tableView points to a different instance.
2009-05-17 14:33:07.591 Test3[33580:20b] cellForRowAtIndexPath 0
2009-05-17 14:33:07.594 Test3[33580:20b] self.tableView: <UITableView: 0x52b9b0>
2009-05-17 14:42:36.810 Test3[33762:20b] numberOfRowsInSection: tableView <UITableView: 0x53bcd0>