Load Data On Uitableview scroll [duplicate] - iphone

This question already has answers here:
making a "load more" button in uitableviewcell?
(4 answers)
Closed 9 years ago.
I have 2000 records and i have to display only 15 records in UITableView, When User Scroll UITableView , More 15 records shold be load in table. How should I do it?

I think the Answer is : - (void)scrollViewDidScroll:(UIScrollView *)scrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint offset = scrollView.contentOffset;
CGRect bounds = scrollView.bounds;
CGSize size = scrollView.contentSize;
UIEdgeInsets inset = scrollView.contentInset;
float y = offset.y + bounds.size.height - inset.bottom;
float h = size.height;
float reload_distance = 15;
if(y > h + reload_distance)
{
//Call the Method to load More Data...
}
}
Hope it will be helpful...!!!

You have to check the UIScrollView contentOffset for that (UITableView is built on UIScrollView). So, add this, to your UITableViewDelegate class:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// when reaching bottom, load more
float offs = (scrollView.contentOffset.y+scrollView.bounds.size.height);
float val = (scrollView.contentSize.height);
if (offs==val)
{
//add more data on your data array
}
}
in order to add more cells to your table when user reaches bottom of your table view.

You can use two delegate methods of UIScrollView. As UITableView comes in hierachy of UIScrollView you'll get control in this delegate methods of UIScrollView.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(#"TableView scrolling");
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(#"TableView Started scrolling");
}
Try above two methods. And choose the one appropriate for you. First see how NSLog are coming when you start scrolling your UITableVIew. And then write your code inside them.

By Default numberOfItemsToDisplay = 15;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (numberOfItemsToDisplay >= [self.yourArray count])
{
numberOfItemsToDisplay = [self.yourArray count];
return 1;
}
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
{
return numberOfItemsToDisplay;
}
else
{
return 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
static NSString *CellIdentifier = #"YourCustomCell";
YourCustomCell *objYourCustomCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (objYourCustomCell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"YourCustomCell" owner:self options:nil];
for(id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[YourCustomCell class]])
{
objYourCustomCell = (AlertsCustomCell *) currentObject;
break;
}
}
}
objYourCustomCell.lbl.text = [[self.yourArray objectAtIndex:indexPath.row]valueForKey:#"vName"];
return objYourCustomCell;
}
else
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell.contentView addSubview:[self loadMoreViewForTable:self.view]];
}
return cell;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tblAlertsList deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.section == 1)
{
numberOfItemsToDisplay = [self loadMore:numberOfItemsToDisplay arrayTemp:self.yourArray tblView:self.tblAlertsList];
[self.tblAlertsList endUpdates];
}else
{
// action if it is not LoadMore
}
}
+ (NSInteger)loadMore : (NSInteger)numberOfItemsToDisplay arrayTemp:(NSMutableArray*)aryItems tblView:(UITableView*)tblList
{
int count =0;
NSUInteger i, totalNumberOfItems = [aryItems count];
NSUInteger newNumberOfItemsToDisplay = MIN(totalNumberOfItems, numberOfItemsToDisplay + kNumberOfItemsToAdd);
NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
for (i=numberOfItemsToDisplay; i<newNumberOfItemsToDisplay; i++)
{
count++;
[indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
numberOfItemsToDisplay = newNumberOfItemsToDisplay;
[tblList beginUpdates];
[tblList insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];
if (numberOfItemsToDisplay == totalNumberOfItems) {
[tblList deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationLeft];
}
NSIndexPath *scrollPointIndexPath;
if (newNumberOfItemsToDisplay < totalNumberOfItems)
{
scrollPointIndexPath = [NSIndexPath indexPathForRow:numberOfItemsToDisplay-kNumberOfItemsToAdd inSection:0];
}
else
{
scrollPointIndexPath = [NSIndexPath indexPathForRow:i-count inSection:0];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 100000000), dispatch_get_main_queue(), ^(void){
[tblList scrollToRowAtIndexPath:scrollPointIndexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
});
return numberOfItemsToDisplay;
}
I have to take custom cell to use this method? I have only taken UItableview

Try this:
http://www.cocoacontrols.com/controls/stableviewcontroller
use this two methods
//for refresh
- (void) addItemsOnTop {
//[self getLoginFollowingUserVideosCall];
[self refreshCompleted];
}
//for load more
- (void) addItemsOnBottom {
//[self getLoginFollowingUserVideosCall];
[self loadMoreCompleted];
}

What you have to do is load the tableview data lazily. Like load data for the tableview cell that are current visible & not all of the data at a time.

Related

How to close another expanded section when second Section is clicked

I am creating an app that contain the expandable section. I have implemented that using this tutorial.
But now i want to close first expanded section when i clicked another section. Can someone help me with this.
Apple has provided a sample code. Here is the link TVAnimationGestures.
But there is a small bug in it please refer post.
You can move the logic code in tableView:didSelectRowAtIndexPath: to a new method (switchSection) and call it on the expanded section to collapse it before expanding the new section. Note that you don't need expandedSections set anymore as you'll have only one section expanded.
NSUInteger expandedSection; /// Define this instead of expandedSections
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self tableView:tableView canCollapseSection:indexPath.section]) {
if (!indexPath.row) {
NSInteger section = indexPath.section;
BOOL currentlyExpanded = (expandedSection == section);
/// Collapse expanded section
if (expandedSection >= 0) {
[self switchSection:expandedSection];
}
/// Expand the new section if needed
if (!currentlyExpanded) {
[self switchSection:section];
}
}
}
}
- (void)switchSection:(NSInteger)section {
BOOL currentlyExpanded = (expandedSection == section);
NSInteger rows;
NSMutableArray *tmpArray = [NSMutableArray array];
if (currentlyExpanded) {
expandedSection = -1;
rows = [self tableView:tableView numberOfRowsInSection:section];
} else {
expandedSection = section;
rows = [self tableView:tableView numberOfRowsInSection:section];
}
for (int i = 1; i < rows; i++) {
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i inSection:section];
[tmpArray addObject:tmpIndexPath];
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (currentlyExpanded) {
[tableView deleteRowsAtIndexPaths:tmpArray withRowAnimation:UITableViewRowAnimationTop];
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
} else {
[tableView insertRowsAtIndexPaths:tmpArray withRowAnimation:UITableViewRowAnimationTop];
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
}
}

Changing the height of UITableViewCell on selection and deselection

I have a UITableView with cells , which on selected shows a popup with "Yes" or "No" confirmation ,When "Yes" is selected , the height of cell should be increased to 100 and its background image should change and if "No" is selected , nothing should happen and it should remain as a normal cell...
Similarly , if already selected cell is selected , again it shows up popup of "Yes" or "No" confirmation . If "Yes" is selected , the selected cell should convert into a normal cell with height 44 and background image is removed . If "No" is selected , then nothing happens and cell remains as a selected cell...
how to deal with height changes and background image changes in such condition ????
#import "TableViewController.h"
#interface TableViewController ()
#end
#implementation TableViewController
- (id)initWithStyleUITableViewStyle)style
{
self = [super initWithStyle:style];
if (self)
{
objects = [NSArray arrayWithObjects:#"One",#"Two",#"Three",#"Four",#"Five",#"Six",#"Seven",#"Eight",#"Nine",#" Ten",#"Eleven",#"Twelve",#"Thirteen", nil];
selectedIndexPath = [[NSIndexPath alloc] init];
}
return self;
}
- (NSInteger)numberOfSectionsInTableViewUITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section
{
// Return the number of rows in the section.
return [objects count];
}
- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell %d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseI dentifier:CellIdentifier];
cell.textLabel.text = [objects objectAtIndex:indexPath.row];
NSLog(#"cell description:- %#",[cell description]);
}
return cell;
}
- (CGFloat)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)indexPath
{
// if (indexPath == selectedIndexPath)
// {
// return 100 ;
// }
// if (indexPath == selectedIndexPath)
// {
// UITableViewCell *c = [self.tableView cellForRowAtIndexPath:indexPath];
// CGRect rect = c.frame ;
// rect.size.height = 100 ;
// c.frame = rect ;
// return 100;
// }
return 44;
}
-(void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath
{
if (selectedIndexPath == [self.tableView indexPathForSelectedRow])
{
UIAlertView *a = [[UIAlertView alloc] initWithTitle:#"DeSelect" message:#"DeSelect ??" delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"Yes", nil ];
[a show];
}
else
{
UIAlertView *a = [[UIAlertView alloc] initWithTitle:#"Select" message:#"Select ??" delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"Yes", nil ];
[a show];
}
}
-(void)alertViewUIAlertView *)alertView clickedButtonAtIndexNSInteger)buttonIndex
{
if (selectedIndexPath == [self.tableView indexPathForSelectedRow])
{
if (buttonIndex==1)
{
selectedIndexPath = [NSIndexPath indexPathForRow:-1 inSection:-1];
// [self tableView:self.tableView heightForRowAtIndexPath:selectedIndexPath];
//[self.tableView reloadData];
//[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:selectedIndexPath] withRowAnimation:UITableViewRowAnimationFade];
//[self.tableView beginUpdates];
//[self.tableView endUpdates];
[self showForIndexPath:selectedIndexPath];
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
}
else
{
if (buttonIndex==1)
{
selectedIndexPath = [self.tableView indexPathForSelectedRow];
//[self tableView:self.tableView heightForRowAtIndexPath:selectedIndexPath];
//[self.tableView reloadData];
//[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:selectedIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
// [self.tableView beginUpdates];
// [self.tableView endUpdates];
[self showForIndexPath:selectedIndexPath];
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
}
}
-(void)showForIndexPathNSIndexPath *)indexpath
{
UITableViewCell *c = [self.tableView cellForRowAtIndexPath:indexpath];
CGRect rect = c.frame ;
rect.size.height = 100 ;
c.frame = rect ;
//code to move the cells downward when a cell is selected
for(int i = indexpath.row ; i < [objects count];i++)
{
NSindexpath *row = [NSIndexPath indexPAthforRow:i+1 in Section:0];
UITableViewCell *tmp = [self.tableView cellforindexpath:row];
CGRect frame = tmp.frame;
CGpoint origin = frame.origin;
CGFloat y = origin.y ;
y = y+56; //(100-44)
origin.y = y ;
frame.origin = origin ;
tmp.frame = frame ;
}
NSLog(#"Cell %d :- %# indexpath:- %#",indexpath.row,[c description],indexpath);
}
P.S. -- You can see the attempts I have made to achieve the task by looking at the code that is commented
Keep a flag for yes or no and a index for selected row number.
-(CGFloat)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ( flag == 1 && indexpath.row == index )
return 50;
else
return 30;
}
apply your logic of image also similarly when you create cell
please post the code what you have tried.
This link may help you.
Can you animate a height change on a UITableViewCell when selected?.
Use popup button index to change variable value and use that variable in
-(CGFloat)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//change tablecell height based on variable value
}
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
}
}
Create two tableViewCell for the both casses. Then in your buttonTouch method, remove your previous cell using
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
And then add newCell using:
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

UITableView add row below selected row

I had a working tableview where I could double-tap a cell and it would add an 'action' cell below which had four buttons programmed on it.
Today I added alphabetic sections to the tableview and a section index and I can no longer get this functionality to work.
I've added a whole range of NSLogs to the code to try and find the problem and I can't, it seems to be trying to add a row in the same section and one row further down than the cell tapped, so I'm not sure what the problem is. Would anyone have any idea what I'm doing wrong?
If anyone can shed any light on this I would be hugely appreciative. (And apologies if my code is cumbersome or hard to follow, I'm new to this so feel free to suggest what I can improve!)
- (void)viewDidLoad
{
//I start with an array of objects, so I created two arrays; one containing the first letters of each Name, and a list of the number of objects that start with each of those names.
nameIndex = [[NSMutableArray alloc] init];
nameIndexCount = [[NSMutableDictionary alloc]init];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [nameIndex count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.actionRowIndexPath) {
//Returns first letter of the section
NSString *alphabet = [nameIndex objectAtIndex:section];
//Returns the number of entries starting with that letter
NSString *numberofRows = [nameIndexCount objectForKey:alphabet];
int intNumberOfRows = ([numberofRows integerValue] + 1);
return intNumberOfRows;
} else {
NSString *alphabet = [nameIndex objectAtIndex:section];
NSString *numberofRows = [nameIndexCount objectForKey:alphabet];
int intNumberOfRows = [numberofRows integerValue];
return intNumberOfRows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"newTableViewCell"];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"newTableViewCell"];
}
//Configure the cell
UIImageView *imageView = [[UIImageView alloc]initWithFrame:cell.frame];
UIImage *image = [UIImage imageNamed:#"LightGrey.png"];
imageView.image = image;
if ([indexPath isEqual:self.actionRowIndexPath]) {
// Four UIButtons coded programmatically
} else {
Contact *p = [[[ContactStore sharedStore]allContacts]objectAtIndex:totalNumberOfRows];
NSString *firstAndLastName = [NSString stringWithFormat:#"%# %#", [p firstName], [p lastName]];
indexPath = [self modelIndexPath:indexPath];
cell.backgroundView = imageView;
// [cell.imageView setImage:smallThumbnailImage];
[cell.imageView setImage:[p thumbnail]];
[[cell textLabel]setBackgroundColor:[UIColor clearColor]];
[[cell textLabel]setText:firstAndLastName];
[[cell detailTextLabel]setBackgroundColor:[UIColor clearColor]];
[[cell detailTextLabel]setText:[p phoneNumber]];
totalNumberOfRows = totalNumberOfRows + 1;
}
return cell;
}
#pragma mark - Action Row Support
-(NSIndexPath *)modelIndexPath: (NSIndexPath *)indexPath
{
if (self.actionRowIndexPath == nil) {
return indexPath;
}
if ([indexPath row] > [self.actionRowIndexPath row]) {
return [NSIndexPath indexPathForRow:([indexPath row] - 1) inSection:indexPath.section];
}
return indexPath;
}
- (void)handleDoubleTap:(UITapGestureRecognizer *)recognizer {
NSLog(#"Double tap");
CGPoint p = [recognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];
NSIndexPath *pathToDelete = self.actionRowIndexPath;
_selectedIndexPath = [self modelIndexPath:_selectedIndexPath];
//Is the user deselecting current row?
if (_actionRowIndexPath) {
[self.tableView deselectRowAtIndexPath:_selectedIndexPath animated:NO];
self.selectedIndexPath = nil;
self.actionRowIndexPath = nil;
} else {
//Selecting a new row
self.selectedIndexPath = indexPath;
self.actionRowIndexPath= [NSIndexPath indexPathForRow:([indexPath row] + 1) inSection:[indexPath section]];
}
[self.tableView beginUpdates];
if (pathToDelete) {
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:pathToDelete] withRowAnimation:UITableViewRowAnimationAutomatic];
}
if (self.actionRowIndexPath) {
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:self.actionRowIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
[self.tableView endUpdates];
}
After you get the IndexPath you need to get the IndexPath.row and IndexPath.section and accordingly you need to add the object into your array at the desired index. For example: if you double tap the 2nd row in the 1st section then you need to add the object at index 4 of the array corresponding to the 1st section and then reload table. The cell would be added to the 3rd index of the 1st section.

Automatically load more articles in TableView

I have TableView and in this TableView I'm printing articles. Now user must click in navigation bar item to update(download more articles from web over json). I want and I think that is better that when user scroll to bottom that automaticaly shows loading cell and start to loading or getting more articles from web.
My questions are next:
How to put that extra cell in which would appear loading indicator
and loading text How to automatically get more articles?
This functionality is in iphone app "App Store" but with click load more items.
Maybe is better to put button load more articles?
All examples and suggestions are welcome.
Thank's for help
This one is easy it's add an UITablecell at the end to load more items.
//
// TableViewController.m
// PartialTable
//
// Created by Abizer Nasir on 07/07/2011.
//
#import "TableViewController.h"
#define kNumberOfItemsToAdd 8
#implementation TableViewController
#synthesize items;
// Mark: -
// Mark: Set up and tear down
- (id)init {
// New designated initialiser
if (!(self = [super initWithStyle:UITableViewStyleGrouped])) {
return nil; // Bail!
}
numberOfItemsToDisplay = kNumberOfItemsToAdd; // Show 10 items at startup
return self;
}
- (id)initWithStyle:(UITableViewStyle)style {
// Call out to the new designated initialiser
return [self init];
}
- (void)dealloc {
[items release];
[super dealloc];
}
#pragma mark - View lifecycle
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (numberOfItemsToDisplay == [items count]) {
return 1;
}
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return numberOfItemsToDisplay;
} else {
return 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"ItemCell";
// If the indexPath is less than the numberOfItemsToDisplay, configure and return a normal cell,
// otherwise, replace it with a button cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.section == 0) {
cell.textLabel.text = [items objectAtIndex:indexPath.row];
cell.textLabel.textAlignment = UITextAlignmentLeft;
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:17.f];
} else {
cell.textLabel.text = [NSString stringWithFormat:NSLocalizedString(#"Next %d items", #"The text to display to load more content"), kNumberOfItemsToAdd];
cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.textLabel.textColor = [UIColor colorWithRed:0.196f green:0.3098f blue:0.52f alpha:1.f];
cell.textLabel.font = [UIFont boldSystemFontOfSize:14.f];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 1) {
NSUInteger i, totalNumberOfItems = [items count];
NSUInteger newNumberOfItemsToDisplay = MIN(totalNumberOfItems, numberOfItemsToDisplay + kNumberOfItemsToAdd);
NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
for (i=numberOfItemsToDisplay; i<newNumberOfItemsToDisplay; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
numberOfItemsToDisplay = newNumberOfItemsToDisplay;
[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
[indexPaths release];
if (numberOfItemsToDisplay == totalNumberOfItems) {
[tableView deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationTop];
}
[tableView endUpdates];
// Scroll the cell to the top of the table
if (newNumberOfItemsToDisplay < totalNumberOfItems) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 200000000), dispatch_get_main_queue(), ^(void){
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
});
[tableView deselectRowAtIndexPath:indexPath animated:YES];
} else {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 200000000), dispatch_get_main_queue(), ^(void){
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:totalNumberOfItems-1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
});
}
}
}
#end
Q1: add 1 more cell for load more status:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return cellsCount <= 0 ? 0 : cellsCount + 1;
}
and create load more cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == cellsCount) {
if (loadMoreCell == nil) {
self.loadMoreCell = [[LoadMoreTableCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"LoadMoreCellIdentifier"];
}
return loadMoreCell;
}
...
}
You can custom LoadMoreTableCell as you want.
Q2:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
float offset = (scrollView.contentOffset.y - (scrollView.contentSize.height - scrollView.frame.size.height));
if (offset >= 0 && offset <= 5) {
[self loadMoreData];
}
}
I also do like this thing in my app,
Here you can use AsyncImage class which is useful to download image with url in background so your tableview scroll smooth..
here two link hope this help you for download image in background...
1.https://github.com/Koolistov/Image-Cache
2.https://github.com/rs/SDWebImage
2nd thing you want to download at bottom to another data and cell then here if you use 1 condition then its work which you like...
in cellForRowAtIndexPath tableview Delegate Method
if(indexPath.row==[yourArray count]-1){
.....do somthing which you like.....(Reload table with your json or another data)
}
here only simple logic other wise better idea is put button at your last row...
Hope this help you..
:)

how to check mark all the names in tableview

i am displaying an array with names in tableview.
selected row will be indicated with check,and finely i am getting selected names into a list.
my code for that is
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.sourceArray count];;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[cell.textLabel setText:[self.sourceArray objectAtIndex:indexPath.row]];
if ([self.selectedArray containsObject:[agentemails objectAtIndex:indexPath.row]]){
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else
{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.selectedArray containsObject:[agentemails objectAtIndex:indexPath.row]]){
[self.selectedArray removeObjectAtIndex:[self.selectedArray indexOfObject:[agentemails objectAtIndex:indexPath.row]]];
}
else
{
[self.selectedArray addObject:[agentemails objectAtIndex:indexPath.row]];
}
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
now i need to place a button select-all when ever use click on it i need to check all the names in the table how can i done can any one please help me.
Thank u in advance.
Try putting a BOOL in your header, then doing this:
-(IBAction)buttonPressed:(id)sender{
myBoolean = YES;
[tableView reloadData];
myBoolean = NO;
}
Then, in the cellForRowAtIndexPath method, just add this:
if(myBoolean){
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
- (IBAction)selectAll:(id)sender
{
if(myBoolean)
{
for (NSInteger s = 0; s < self.iTable.numberOfSections; s++)
{
for (NSInteger r = 0; r < [self.iTable numberOfRowsInSection:s]; r++)
{
[[self.iTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] setAccessoryType:UITableViewCellAccessoryNone];
}
}
myBoolean = NO;
}
else
{
for (NSInteger s = 0; s < self.iTable.numberOfSections; s++)
{
for (NSInteger r = 0; r < [self.iTable numberOfRowsInSection:s]; r++)
{
[[self.iTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] setAccessoryType:UITableViewCellAccessoryCheckmark];
}
}
myBoolean = YES;
}
}