Tuesday, 31 December 2013

GridView in iOS.

As you guys have collection view , you can use it as grid.
Then you think what I have made to use for?

So, I am presenting advantage of my GridView below:--

1)First of all it is compatible with ios 5.0 + , on the other hand , UICollectionView doen't work in iOS 5.0.
2)It automatically adjust cell blocks according to string size and extend width of the grid.
3)It allows you to define cell type as if you want to give input or keep it as simple label.
4)Also allows to perform calculation like
    Add,
    Multiply
    Subtract
    Devide
    and Mod Operation on cell.
5)Row click action returns row data in the format of NSDictionary.
6)Support landscape as well as portrait mode.
7)Made for both iPhone and iPad.




1) First of all you need to download library I have put on the shared link

Download GridViewLib Here

2) After you have downloaded it,Import both "include" and "libGridViewTable.a" to your project and check copy to folder option while adding.After importing it would look like image below.
  


3)Don't forget to add some framework:
     i)QuartzCore Framework
     ii)CoreGraphics Framework


4) Go to Project->Build Setting -> Search Header Path
    and set path to $SOURCE_ROOT/include
    and "Always Search User Paths" to YES as shown below



5) Now you have successfully added the library , you can use it by just importing "GridViewTable.h" file and use its DataSource and Delegate Methods.

Here is demo how I have used in header and Implementation File.

//
// MytestViewController.h
// MyLibTest
//
// Created by Prince Kumar Sharma on 31/12/13.
// Copyright (c) 2013 Prince Kumar Sharma. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "GridViewTable.h"
@interface MytestViewController : UIViewController<GridTableDatasource,GridTableDelegate>
{
GridViewTable *gbTable;
NSMutableArray *dataArray;
}
@end


//
// MytestViewController.m
// MyTest
//
// Created by Prince Kumar Sharma on 02/01/14.
// Copyright (c) 2014 Prince Kumar Sharma. All rights reserved.
//
#import "MytestViewController.h"
@interface MytestViewController ()
@end
@implementation MytestViewController
- (void)viewDidLoad
{
NSArray *headerArray=[[NSArray alloc] initWithObjects:@" -ID- ",@"-Customer Name-",@" Designation ",@" --State-- ",@" -----Address Of Customer---- ",@" --Contact No-- ",@" --Code-- ",@"--total--",nil];
NSArray* firstRow=@[@"234",@"Rohit Sharma",@"Pilot",@"Rajasthan",@"HN-47,Bundi",@"8976565434",@"91",@"0"];
NSArray* secondRow=@[@"578",@"Prakash Aneja",@"Engineer",@"Haryana",@"HN-78,Yamuna Nagar,Jagadhari",@"8976564543",@"12",@"0"];
NSArray *thirdRow=@[@"786",@"Prince Kumar",@"Sofware Engineer",@"Haryana",@"HN-161,Nayapura",@"8976564543",@"32",@"0"];
NSArray *fourthRow=@[@"767",@"Ankit Sharma",@"Civil Engineer",@"Delhi",@"HN-78,Chandani Chowk",@"43238976564",@"98",@"0"];
NSArray *fifth=@[@"767",@"Kush Saxena",@"Chemical Engineer",@"Kolkata",@"HN-67,MG Road",@"7865897656",@"40",@"0"];
NSArray *sixth=@[@"433",@"Piyush Kaushik",@"Mechanical Engineer",@"Chicago",@"HN-763,Sri Apartment",@"2323897656",@"98",@"0"];
dataArray=[[NSMutableArray alloc] initWithArray:@[firstRow,secondRow,thirdRow,fourthRow,fifth,sixth]];
gbTable=[[GridViewTable alloc] initWithFrame:CGRectMake(0, 0, 320, 44) headerArray:headerArray];
gbTable.gtDelegate=self;
gbTable.gtDatasource=self;
gbTable.autoresizingMask=UIViewAutoresizingFlexibleWidth;
[self.view addSubview:gbTable];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)viewDidAppear:(BOOL)animated
{
[gbTable reloadListWithHandler:^(NSInteger gridHeight__) {
NSLog(@"height is %i",gridHeight__);
}];
}
-(NSInteger)numberOfRowsForGridViewTable:(GridViewTable*)gridViewTable
{
return [dataArray count];
}
-(NSArray*)cellsForRowAtIndex:(int)rowIndex
{
return dataArray[rowIndex];
}
-(NSInteger)numberOfCellsForRow:(int)rowIndex
{
return [dataArray[rowIndex] count];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return YES;
}
-(CellType)gridViewTable:(GridViewTable*)gridViewTable cellTypeForCellIndex:(int)cellIndex inRow:(int)row
{
if (cellIndex==4) {
return CTText;
}
if (cellIndex==5 || cellIndex==6) {
return CTNumber;
}
return CTLabel;
}
-(NSArray*)gridViewTable:(GridViewTable*)gridViewTable cellIndexArrayForCalculationInRow:(int)row
{
return @[@"5",@"6"];
}
-(NSInteger)gridViewTable:(GridViewTable*)gridViewTable destinationCellIndexForCalculationInRow:(int)row
{
return 7;
}
-(GridOperation)gridViewTable:(GridViewTable*)gridViewTable operationForCalculationInRow:(int)row
{
return Multiply;
}
-(UIColor*)gridViewTable:(GridViewTable*)gridViewTable colorForCellAtIndex:(int)cellIndex inRow:(int)row
{
if (cellIndex==0) {
return [UIColor yellowColor];
}
if (cellIndex==3) {
return [UIColor orangeColor];
}
return [UIColor clearColor];
}
-(UIColor*)gridViewTable:(GridViewTable *)gridViewTable colorForRowAtIndex:(int)row
{
if (row%2==0) {
return [UIColor grayColor];
}else{
return [UIColor lightGrayColor];
}
}
-(UIColor*)colorForHeader
{
return [UIColor darkGrayColor];
}
-(void)gridViewTable:(GridViewTable*)gridViewTable didSelectRowAtIndex:(NSInteger)row WithSelectedData:(NSDictionary*)selectedRowData
{
NSLog(@"selected data at index : %d is : %@",row,selectedRowData);
}
-(void)inputEditingAtPosition:(NSValue*)point
{
int currentYPos=[point CGPointValue].y;
NSLog(@"current y pos is %i",currentYPos);
}
-(void)summationdidCalculated:(NSDictionary*)summation_
{
NSLog(@"summation dictionary is %@",summation_);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end


No comments:

Post a Comment