#import "LPGridView.h"
#import "LPTableHeader.h"
#import "LPTableViewCell.h"
#define kIndexTableWidth 100.0
#define kGridTableWidth 500.0
@implementation LPViewController
@synthesize gridView;
@synthesize dataSet;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
}
return self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// 그리드뷰 추가.
self.gridView = [[LPGridView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 460)];
self.gridView.delegateObj = self;
self.gridView.indexTableWidth = kIndexTableWidth;
self.gridView.gridTableWidth = kGridTableWidth;
[self.view addSubview:self.gridView];
[self.gridView setNeedsDisplay];
// 데이터 로드.
NSString *path = [[NSBundle mainBundle] pathForResource:@"SampleData" ofType:@"plist"];
self.dataSet = [[NSMutableArray alloc] initWithContentsOfFile:path];
[self.gridView relaodGridView];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#pragma mark - 스크롤뷰 델리게이트
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 상하 스크롤할 때는 두 개의 테이블이 동 시에 움직여야 하기 때문에...
if ([scrollView isEqual:self.gridView.indexTable])
{
self.gridView.gridTable.contentOffset = self.gridView.indexTable.contentOffset;
}
else if ([scrollView isEqual:self.gridView.gridTable])
{
self.gridView.indexTable.contentOffset = self.gridView.gridTable.contentOffset;
}
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.dataSet count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.gridView.indexTable)
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
cell.contentView.backgroundColor = (indexPath.row & 1) ? [UIColor darkGrayColor]: [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.font = [UIFont systemFontOfSize:15];
cell.textLabel.text = [[self.dataSet objectAtIndex:indexPath.row] objectForKey:@"regDate"];
return cell;
}
else if (tableView == self.gridView.gridTable)
{
static NSString *CellIdentifier = @"Cell";
LPTableViewCell *cell = (LPTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"LPTableViewCell" owner:self options:nil];
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (LPTableViewCell *)currentObject;
break;
}
}
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
// Configure the cell...
cell.contentView.backgroundColor = (indexPath.row & 1) ? [UIColor darkGrayColor]: [UIColor whiteColor];
cell.nameLabel.text = [[self.dataSet objectAtIndex:indexPath.row] objectForKey:@"name"];
cell.priceLabel.text = [[self.dataSet objectAtIndex:indexPath.row] objectForKey:@"price"];
cell.discountRateLabel.text = [[self.dataSet objectAtIndex:indexPath.row] objectForKey:@"discountRate"];
cell.inventoryLabel.text = [[self.dataSet objectAtIndex:indexPath.row] objectForKey:@"inventory"];
cell.createDateLabel.text = [[self.dataSet objectAtIndex:indexPath.row] objectForKey:@"createDate"];
return cell;
}
return nil;
}
#pragma mark - Table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 25;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (tableView == self.gridView.indexTable)
{
LPTableHeader *header = [[LPTableHeader alloc] initWithFrame:CGRectMake(0.0, 0.0, kIndexTableWidth, 25) andTitles:[NSArray arrayWithObjects:@"등록일자", nil]];
return header;
}
else if (tableView == self.gridView.gridTable)
{
LPTableHeader *header = [[LPTableHeader alloc] initWithFrame:CGRectMake(0.0, 0.0, kGridTableWidth, 25) andTitles:[NSArray arrayWithObjects:@"이름", @"가격", @"한인율", @"재고", @"생산일자", nil]];
return header;
}
return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.gridView.indexTable)
{
[self.gridView.gridTable selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
else if (tableView == self.gridView.gridTable)
{
[self.gridView.indexTable selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
@end