1

I am trying to load limited items from Plist in a collection view. In my plist, I have 30 items but I want to show only 3 or 4 at a time and on button click want to show next 3. any idea how to figure this out ?

2
  • You mean something like pagination ??
    – M.Alatrash
    Oct 8, 2014 at 12:52
  • kind of pagination, actually I want to show limited items and on click event collection view load next items. Oct 8, 2014 at 14:37

2 Answers 2

1

Try this,

@interface ViewController ()
 {
 int currentPage;
 int pagelimit;
 }

- (void)viewDidLoad
{
currentPage=0;
pagelimit=3;
 [self reloadData];

} 

 -(IBAction)nextBtnClicked:(id)sender;
{
      NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:NSLocalizedString(@"plistName", nil) ofType:@"plist"]];
currentPage=currentPage+3;
pagelimit=pagelimit+3;
if (currentPage>[arr count])
{
    currentPage=currentPage-3;
    pagelimit=pagelimit-3;
    return;

}
 [self reloadData];

}

 -(IBAction)previousBtnClicked:(id)sender;
{
      NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:NSLocalizedString(@"plistName", nil) ofType:@"plist"]];   
currentPage=currentPage-3;
pagelimit=pagelimit-3;
if (currentPage<[arr count]) {
    currentPage=currentPage+3;
    pagelimit=pagelimit+3;
    return;

}
[self reloadData];

}

-(void)reloadData
{
NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:NSLocalizedString(@"plistName", nil) ofType:@"plist"]];
[dataArray removeAllObjects];
for (int i=currentPage; i<pagelimit; i++) {

    [dataArray addObject:[arr objectAtIndex:i]];
}
[collectionview reloadData];
}
3
  • Thats work pretty fine but 1 thing more to figure out. the method. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath as we already remove all objects in reload method, so the data in cell not populated Oct 8, 2014 at 14:25
  • in reload method we are adding new data into array and then reload collectionview..
    – Kalpesh
    Oct 9, 2014 at 5:43
  • I definitely done that way before if I have reputation of 15. restriction from stackoverflow. Oct 9, 2014 at 11:28
0

You can convert .plist files to a NSArray, and from there, you can specifically ask for the first [0] in the sequence, the seventh [6], or even the thirtieth [29].

As far asking for the first three items, use code similar to this

-(void)getThree {
NSString *path = [[NSBundle mainBundle] pathForResource:@"list" ofType:@"plist"];
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];

NSString *first = [array objectAtIndex:(0)%array.count];
NSString *second = [array objectAtIndex:(1)%array.count];
NSString *third = [array objectAtIndex:(2)%array.count];

NSLog(@"%@ %@ %@", first, second, third); }

Note:

The [0] is always the first item in the set, because they like to start with 0.

So basically whatever the number of the item in the set is, subtract 1 and put it in between brackets.

1
  • in this way, we need a lot of variables. suppose we have 500 items in plist, then what to do ? Oct 8, 2014 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.