Wednesday, November 21, 2012

Making oriented enable bookshelves type TableView


Today I have written about how to make bookshelves look like UITableView
Initialise TableView
Give IBOutLate to UITableView and also give reference of delegate and datasource of tableview
Add several Images which name in Array
Initialize Array for table view
01titlesArray = [[NSArray alloc] initWithObjects:@"People"@"Things",@"Fruits"nil];
02peopleArray = [[NSArray alloc] initWithObjects:@"Gardener.png",@"Plumber.png"@"BusinessWoman.png"@"BusinessMan.png"@"Chef.png",@"Doctor.png"nil];
03thingsArray = [[NSArray alloc] initWithObjects:@"StopWatch.png",@"TrashCan.png"@"Key.png"@"Telephone.png"@"ChalkBoard.png",@"Bucket.png"nil];
04fruitsArray = [[NSArray alloc] initWithObjects:@"Pineapple.png",@"Orange.png"@"Apple.png"nil];
05categoryArray = [[NSMutableArray alloc] initWithObjects:@"Bollywood",@"GollyWood",@"Dhollywood"@"HollyWood",@"TV Searials"@"Sports",@"Solo",@"New"nil];
06_arFiltered = [[NSMutableArray alloc] init];
07[_arFiltered addObjectsFromArray:categoryArray];
08 
09arrays = [[NSArray alloc] initWithObjects:peopleArray, thingsArray, fruitsArray, nil];
10self.countries = [NSDictionary dictionaryWithObjects:arrays forKeys:titlesArray];
11self.tableView.rowHeight = 250;
12[self.tableView reloadData];
Now define Delegate Method and make Table View
001- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
002{
003return [self.countries count];
004}
005 
006- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
007{
008return [[self.countries allKeys] objectAtIndex:section];
009}
010 
011- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
012{
013//    NSString *continent = [self tableView:tableView titleForHeaderInSection:section];
014//    return [[self.countries valueForKey:continent] count];
015return 1;
016}
017 
018- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
019static NSString *CellIdentifier = @"CountryCell";
020 
021UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
022if (cell == nil) {
023cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
024 
025//    NSString *continent = [[self.countries allKeys] objectAtIndex:indexPath.row];
026NSString *continent1 = [self tableView:tableView titleForHeaderInSection:indexPath.section];
027NSLog(@"Contine............%@", continent1);
028int k = [[self.countries valueForKey:continent1] count];
029UIScrollView *previewScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, cell.contentView.frame.size.width, 250)];
030previewScrollView.backgroundColor = [UIColor clearColor];
031previewScrollView.pagingEnabled = TRUE;
032previewScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
033[previewScrollView setContentSize:CGSizeMake(250*k, 60.0)];
034previewScrollView.showsHorizontalScrollIndicator = YES;
035NSLog(@"cell.contentView.frame.size.widt  %f",cell.contentView.frame.size.width);
036NSLog(@"K %@   %d",continent1,  k);
037for(int i=0 ; i<k; i++ ){
038//            UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(150.0*i, 0, 150.0, 250)];
039//            headerLabel.font = [UIFont boldSystemFontOfSize:14.0];
040//            headerLabel.backgroundColor = [UIColor clearColor];
041//            NSLog(@"%@",[[self.countries valueForKey:continent1] objectAtIndex:i]);
042//            headerLabel.text = [[self.countries valueForKey:continent1] objectAtIndex:i];
043//            [previewScrollView addSubview:headerLabel];
044UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(250.0*i, 10, 200, 250)];
045imageView.image = [UIImage imageNamed:[[self.countries valueForKey:continent1] objectAtIndex:i]];
046imageView.contentMode = UIViewContentModeCenter;
047[previewScrollView addSubview:imageView];
048 
049UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
050btn.frame = CGRectMake(250.0*i, 10, 200, 250);
051NSLog(@"%@", [NSString stringWithFormat:@"%d%d",indexPath.section,i]);
052btn.tag = [[NSString stringWithFormat:@"%d%d",indexPath.section,i] intValue];
053[btn addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchDown];
054[previewScrollView addSubview:btn];
055}
056 
057[[cell contentView] insertSubview:previewScrollView atIndex:0];
058// [cell addSubview:previewScrollView];
059 
060}
061return cell;
062}
063-(void)btnTapped:(id)sender
064{
065int tag = [sender tag];
066NSLog(@"Tag %d", tag);
067 
068[detailView removeFromSuperview];
069detailView = [[UIView alloc] init];
070detailView.frame = CGRectMake(360, 360, 0, 0);
071detailView.backgroundColor = [UIColor redColor];
072UIImageView *bgImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
073bgImage.backgroundColor = [UIColor grayColor];
074bgImage.image = [UIImage imageNamed:@"frnt-page.png"];
075[detailView addSubview:bgImage];
076 
077[self.view addSubview:detailView];
078 
079// pop up transaction
080[UIView beginAnimations:nil context:nil];
081[UIView setAnimationDuration:0.5];
082[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:detailView cache:YES];
083[self.view addSubview:detailView];
084[detailView setFrame:CGRectMake(150,150,360,360)];
085bgImage.frame = CGRectMake(0, 0, 360, 360);
086[UIView commitAnimations];
087 
088// Flop Transaction
089//    [UIView beginAnimations:nil context:@"flipTransitionToBack"];
090//    [UIView setAnimationDuration:1.2];
091//    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:detailView cache:YES];
092//    [detailView removeFromSuperview];
093//    [self.view addSubview:detailView];
094//    [UIView commitAnimations];
095 
096//    detailView.transform = CGAffineTransformIdentity;
097//    [UIView beginAnimations:nil context:NULL];
098//    [UIView setAnimationDuration:1.5];
099//    detailView.transform = CGAffineTransformMakeScale(1.0, 1.0);
100//    [UIView commitAnimations];
101 
102}
Give Orientation to YES
1- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
2 
3return YES;
4}
It’s Look Like
Table
Vertical View
Table View
Horixontal View

Cheers!!!

No comments: