1

I want to add data or cells in UITableview when we scrolled to the last cell of the UITableView. In other words, as we have seen in many iPhone apps, that when we reached to the last cell of the UITableview the more cells get added to it at runtime. I want to achieve that functionality in my project.

Means Add some more cells to the UITableview when we reached to the previously added last cell.

Please tell me the sample code or any tutorial for it.

Sanchit Paurush
  • 6,114
  • 17
  • 68
  • 107

5 Answers5

2

I think that there are 2 questions here. First, how to detect the end of tableVeiw? Second, how to add cells dynamically? First question, I think it can be done by observing the value of content offset of scrollView or current indexPath of tableView cell. The content offset of scrollView can be observed by following method of UIScrollViewDelegate. The content offset is a property of scrollView.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
   CGFloat currentOffset = scrollView.contentOffset;

   // Detect scrolling to bottom by this offset value.
   ....
}

The index of cell may be decided by the method of UITableViewDataSource.

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath

Second question can be overwhelmed by the methods of UITableView.

  • beginUpdates
  • endUpdates
  • insertRowsAtIndexPaths:withRowAnimation:
  • deleteRowsAtIndexPaths:withRowAnimation:
  • insertSections:withRowAnimation:
  • deleteSections:withRowAnimation:

I remember the sample codes in this official document will teach how to use above methods. They don't reload all cells, but careless operations will result in crash easily.

AechoLiu
  • 17,522
  • 9
  • 100
  • 118
1

If you have data stored in an array, then you would definitely have the count of how many cells you want. Use the below code for your help

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return [array count];}

i believe this would have helped you.

0

Populate the tableView by using an array (as you should always do). When the delegate method cellForRowAtIndexPath ask your for the last cell, repopulate your array with new (more) data and make a [tableView reloadData].

yinkou
  • 5,756
  • 2
  • 24
  • 40
  • But it will reload all the table data. I want to add data to the previously stored data in the UITableView – Sanchit Paurush Jul 25 '11 at 10:33
  • that will result in that. If you are using a mutableArray to store the tableView content just simply add the new data to it. When you call [tableView reloadData] it will simply recognize that there is more data aviable now and it will add the additional cells. – yinkou Jul 25 '11 at 10:41
0

you just want to add data in table?

Then, first you may need to add one more view, modal view. You should write some specification about what you want to add in this view.

Next you send a message to existing data array(Assume you use array)

[ExistingArrayData addObject:TemporalilyNewObject];

And you can sort the data by using NSSortDescriptor.

NSSortDescriptor *nameSorter = [[NSSortDescriptor alloc] initWithKey:KEY_VALUE ascending:YES selector:@selector(caseInsensitiveCompare:)];
[ExistingArrayData sortUsingDescriptors:[NSArray arrayWithObject:nameSorter]];

In this case, this method sorts the data by KEY_VALUE, ascending.

Finally, you shoule add this code in RootView's viewWillAppear method.

[self.tableView reloadData];

This code notifies app of change of data.

manutd
  • 564
  • 9
  • 22
0
    -(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

        int i=0;

        i=indexPath.row;

        int count=[urTablearray count];

        if (count == (++i)) {

                //do ur stuff here

                     here add object to urTablearray

                     [urTablearray addObject:@"vijay"];


        }


    }

Note: urTablearray is NSMutableArray type not NSArray