Today we will talk about how to give pagination behaviour to our UITableView.
We are going to populate UITableView with 2 arrays and it will have 2 UIButtons. 1 is for “next” and the other one is “previous”. UItableView will show only 10 rows at a time.
Its logic is that we have 4 arrays. 2 arrays have complete data for both columns and 2 arrays will b small arrays having only 10 values at a time. on the action of “next” or “previous” button, 10 next or previous values will be added in the small arrays. and then we will populate those arrays in UITableView.
Given below method is used to handle this functionality
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
-(void)prepareArraysForTableView:(int)sender{ [smallArray1 removeAllObjects]; [smallArray2 removeAllObjects]; if (sender==1) { if ([array1 count]>totalValues+9) { for (int i=totalValues; i<=totalValues+9; i++) { [smallArray1 addObject:[array1 objectAtIndex:i]]; [smallArray2 addObject:[array2 objectAtIndex:i]]; NSLog(@"total %i, i %i", totalValues, i); } totalValues = totalValues+9; }else{ if ([array1 count]>0) { for (int j=totalValues; j<[array1 count]; j++) { [smallArray1 addObject:[array1 objectAtIndex:j]]; [smallArray2 addObject:[array2 objectAtIndex:j]]; } } } }else{ if ([array1 count]>10) { if (totalValues>=9) { for (int i=totalValues-9; i<=totalValues; i++) { [smallArray1 addObject:[array1 objectAtIndex:i]]; [smallArray2 addObject:[array2 objectAtIndex:i]]; NSLog(@"total %i, i %i", totalValues, i); } totalValues = totalValues-9; }else{ if ([array1 count]>0) { for (int j=totalValues; j<10; j++) { [smallArray1 addObject:[array1 objectAtIndex:j]]; [smallArray2 addObject:[array2 objectAtIndex:j]]; } } } }else{ for (int j=totalValues; j<[array1 count]; j++) { [smallArray1 addObject:[array1 objectAtIndex:j]]; [smallArray2 addObject:[array2 objectAtIndex:j]]; } } } [myTableView reloadData]; } |
here “prev” button have a tag zero and tag of ”next” button is 1;
you may download the sample code from here NextPrevTableView
Happy coding and Mind Your Code