gmoledina / GMGridView

A performant Grid-View for iOS (iPhone/iPad) that allows sorting of views with gestures (the user can move the items with his finger to sort them) and pinching/rotating/panning gestures allow the user to play with the view and toggle from the cellview to a fullsize display.
MIT License
2.3k stars 512 forks source link

Visible cells nice to have #43

Open johnnyd opened 12 years ago

johnnyd commented 12 years ago

It would be convenient for a public method on gridView that returns the currently visible cells. Right now I have resorted to making the itemSubviews method public instead of private. Anyone concur with me on this??

tonyarnold commented 12 years ago

Absolutely, I've run into this now so I'll see what I can do and push it back.

Maverick1st commented 12 years ago

You can also get the visible cells like this:

 CGPoint contentOffset = ((UIScrollView*)[[gridView subviews]objectAtIndex:0]).contentOffset;
 for (GMGridViewCell *view in [[[gridView subviews]objectAtIndex:0] subviews])
 {
     ...
 }

But everytime you enter sorting mode the cells order seems to change in the subviews array. So this is not best solution. I'll try to make itemSubviews public and see if its better for my purpose.

Maybe I'll create a getCellsInRange Method or something like that, where you can also get a smaller amount of cells than the visible ones. Could be useful for some fancy animations. :)

btw. How do i make code tags in a comment?

Bejil commented 10 years ago

I reached to get them like this :

Add @property (nonatomic) int index; to GMGridViewCell.h and @synthesize index; to his .m file:

Then in -(GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index add cell.index=index; before return cell;.

All you have to do now is adding a public method to GMGridView:

-(NSArray *)indexPathsForVisibleRows;
-(NSArray *)indexPathsForVisibleRows{

    NSMutableArray *lc_array = [[NSMutableArray alloc] init];

    for(int i=0;i<[self.subviews count];i++){
        if(CGRectIntersectsRect(self.bounds, [[self.subviews objectAtIndex:i] frame]) && [[self.subviews objectAtIndex:i] isKindOfClass:[GMGridViewCell class]]){
            GMGridViewCell *subview = (GMGridViewCell *)[self.subviews objectAtIndex:i];
            [lc_array addObject:[NSIndexPath indexPathForRow:subview.index inSection:0]];
        }
    }
    return lc_array;
}
ghost commented 10 years ago

The above solution "indexPathsForVisibleRows" for getting count of visible cells gives wrong results on orientation change. Unable to find the solution though.

Bejil commented 10 years ago

Sorry I haven't noticed that cause I'm reloading data on rotation

ghost commented 10 years ago

I too am reloading data on orientation change but the count comes wrong for me though.

Bejil commented 10 years ago

I can't reproduce your problem. I'm using this method in order to use apple's lazy technique for cell' images. I always get the same association image/cell, rotation doesn't alter anything.

ghost commented 10 years ago

My problem persists when GMGridView is scrolled to the end when the GMGridView is partially filled... The scenario is as follows : Datasource count = 44

Potrait mode = 2 columns * 7 rows = 14 cells Landscape mode = 3 columns * 5 rows = 15 cells

Potrait mode => page 1 = count is 1 to 14. ==> 14 cells => page 2 = count is 15 to 28. ==> 14 cells => page 3 = count is 29 to 42. ==> 14 cells => page 4 = count is 43 to 44. ==> 2 cells

Landscape mode => page 1 = count is 1 to 15. ==> 15 cells => page 2 = count is 16 to 30. ==> 15 cells => page 3 = count is 31 to 44. ==> 14 cells

Now when I scroll to the 3rd page in landscape mode and then change the orientation, I reach the to the 4th page but with count of 3rd page.

Also the [self.subViews count] gives me a different value each time the "indexPathsForVisibleRows" method is called. So I am considering "[[self itemSubviews] count]" as my counter for the for loop.

My grid view frame is ===> _gmGridView.frame=CGRectMake(10, 84, 1004, 565); for landscape

_gmGridView.frame=CGRectMake(10, 84, 748, 820); for potrait

Also the item cell size is as follows