I have a UITableView to display a list of data.
The list will contain images, so I am loading images with lazy loading. Now I don't want to load the whole list at a time.
The loading should be like, first it should load some 10 records and when we scrolling down to the tableview, it should automatically load next 10 records as on.
For this I need to add rows when I am scrolling to bottom. The tableview may contain different sections.
So how can I add new rows and new sections at the end of the tableview while scrolling down?
Look at the Apple documentation about UITableView.
You can use the following method to add a row :
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
Firstly, the thing you want to do is known as Lazy Loading.
Means your table view will load images or show data in the cells which are currently visible to user. So have a look at this link. It provides sample code that might help you.
And to insert row, you can use
(void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
At
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == [yourArray count]-1)
{
//start fetching the next set of data in background asynchronously and when fetching is complete use delegate methods to reload or append your current UITableView , meanwhile start a activity indicator as footer to show loading
}
}
this is the effective method, if you try to implement synchronously when scrolling reaches bottom, it will make your device stuck
Related
I am working on iPhone application . This application contains plenty of web services. I need to show list in UITableView with page by page. How to show "Loading" and call web service when scrolling down the UITableview ? Please help me..
I assume that you are adding a custom cell to the bottom of your table view to indicate the end of the current data set. When you create that cell, set its tag property to a meaningful constant you've previously defined, like END_OF_TABLE_CELL.
Then use the delegate method tableView:willDisplayCell:forRowAtIndexPath to check if the cell about to display is your custom cell, and trigger a reload of the table's datasource.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//check your cell's tag
if (cell.tag == END_OF_TABLE_CELL) {
//Call your custom method to fetch more data and reload the table
// You should also remove this custom cell from the table and add one at the bottom
}
}
This way you will have a table that refreshes itself whenever a user scrolls down far enough to bring the custom cell into view.
Good Luck!
I have a table view in which all are customised cells which is loading from 3 types of custom cells object classes.I have done paging also so that it will load 5 pages at the begning and then the second one like that.the problem is at the end of the table view cells i need to add a loading view.For that i used - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)sectionand added an activity indicator and loading label.but its position is not getting correctly.some times if the contents are more its position is not going correctly.Can anybody guide me the correct approach in achieving this?
You say : "loading view at the end of table view" whereas this function :
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
Will add a view at the end of a section that is different from what you want to do.
I suggest you to have a look at the following UITableView property :
tableFooterView
(documentation)
I am using table view in my app, the task is multiple deletion of cells like iPhone message application. My problem is when select the cells check box and scroll the table then selected cells check marks are deselect. What is the problem?
When you scroll the table then its reload function automatically called. Thats why your selection have cleared. do one thing when you checked your check boxes then stores that values in an array after that call an custom reload function and in that reload your all values in your table...
You need to Manage an array which contain the indexes of the rows for which you select a checkbox.
in cellForRowAtIndexPath: delegate method check whether the row index is in your index array show the checked image otherwise show unchecked image.
UITableView will reuse your tableviewcell, that means it will never keep many cell object, just 5~6 cell maybe. It will save a lot of memory, but it may not keep the state you have set. So you must keep the data by yourself and assign to the cell in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
In my application there is a table view.What I want to do is when I scrolls the table view and when it come to its end row I want to perform some action.
Please tell me which approach should I use to do this.
Thanks in advance!!
I think you should make a check in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)
when it loads the last row then call that method.
I think this will work. because table does not load all data at once but i loads data when user scroll
I think (don't the docs up here) but there is no method within the UIScrollViewDelegate that handles the scrolling to the bottom. There is when scrolling to top, but can't remember if there is scrolling to bottom. Check it please.
If not, the easy way is just to check the indexpath.row at cellForRow. If it is your last row, call whatever action you want.
I have created an application in which i have to add users to the sqlite database.
Now the problem is I want the look of the standard iPhone Contact application Where while adding user we have the width of first cell smaller than other cells and the image before that cell..
Can you please give me the idea how such thing is possible.
How to make one cell small and rest others of normal size..
Thanks for any help in advance
There are three UITableViewDelegate messages you can listen for to adjust height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
However, even thought I didn't write Contacts.app I have a feeling they are also using
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
To adjust the views as well. Remember, you don't have to pack everything into a single monolithic custom table view cell. You can create multiple custom table view cells and load them each appropriately depending on the index path.
The contact detail view is a grouped tableview. Each cluster of cells is a section. The top section is a single custom cell with two subviews that look like squashed tableview cells. The left view shows the contact's photo. The right view shows the name.
To reproduce, create a custom UITableView subclass and lay it out like you want either programmatically or in Interface Builder. Then in the tableview delegate's cellForRowAtIndexPath check indexPath.section and return the proper row for the section.
It appears that the Contacts app uses a custom tableHeaderView when presenting the contact details with an image and label. A similar implementation is included in the sample project iPhoneCoreDataRecipes. The RecipeDetailView loads a separate nib in tableViewHeaderView that is used to set the tableView.tableHeaderView property. Have a look at RecipeDetailViewController.{h,m} and DetailHeaderView.xib. When the Contacts app switches to editing mode, the headerView appears to be swapped out for another view that has a button and a tableView with a single cell. This will allow you to set up a separate tableViewDelegate to handle the Name parts of the contact and a delegate to handle the address / telephony details.