Question

hi iam developing a social networking application using UICollectionview horizontal layout, when ever my app launches collection view loads up to 5 cells then if i swipe 5th cell then that time only 6th cell memory will allocates. same way swipe 6th cell to 7 th cell like that.So,how can i implement this process

thanks in advance for any advices

Was it helpful?

Solution

You need to add a swipe gesture recognizer to your collectionView:

UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
[collectionView addGestureRecognizer:swipeGesture];

Then, handle the swipe in handleSwipeGesture: to allocate the cell.

-(void) handleSwipeGesture:(UISwipeGestureRecognizer *) sender
{
...
}

You can make the swipe direction whatever you want to use, this one is configure for up.

That's pretty much all there is to it. Main thing is you don't want the direction to conflict with the scrolling direction swipe, as I don't think there is a clean way to deal with that. When you are scrolling horizontally, that is a swipe, so you would need to use up/down swipe direction.

To attach to the cell instead (to capture gesture on an individual cell) I do it most times for simple collectionviews in the following method:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ...
    UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    [cell addGestureRecognizer:longPressGestureRecognizer];
    ...
}

(This is a long press, but it works the same way). In the case of putting it in the cell, you'll need to put a tag on the cell then reference that tag int the handler to figure out which cell it came from:

Here's one for the above:

- (void) handleLongPress: (UILongPressGestureRecognizer *) sender 
{
    if (sender.state != UIGestureRecognizerStateBegan)
        return;

    CGPoint p = [sender locationInView: collectionView];
    NSIndexPath *indexPath = [collectionView indexPathForItemAtPoint:p];
    if (indexPath != nil)
    {
        UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
        if (cell)
    {
    int indexOfItem = cell.tag;
        NSLog(@"Selected item = %d", indexOfItem);
    }
}

At least something along this line...

OTHER TIPS

You can achieve this by implement the UIScrollViewDelegate method

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
      CGPoint offset = scrollView.contentOffset;
      CGRect bounds = scrollView.bounds;
      CGSize size = scrollView.contentSize;
      UIEdgeInsets inset = scrollView.contentInset;
      float y = offset.x + bounds.size.width - inset.right;
      float h = size.width;


      float reload_distance = 75; //distance for which you want to load more
     if(y > h + reload_distance) {
        // write your code getting the more data
        NSLog(@"load more rows");

      }
   }

in this when you go at the last cell of the collection then this scroll view delegate method will called. and you may change the distance vale according to your requirement.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top