Sunday, December 23, 2012

Tutorial: Adding a Drop Shadow To Last Row of UITableView


//
//  UITableViewCell+DropShadow.h
//  TableViewCellDropShadow
//
//  Created by Kurry L Tran on 12/23/12.
//  Copyright (c) 2012 Kurry L Tran. All rights reserved.
//
 
#import <UIKit/UIKit.h>
 
@interface UITableViewCell (DropShadow)
-(void)addDropShadow;
-(void)removeDropShadow;
@end
//
//  UITableViewCell+DropShadow.m
//  TableViewCellDropShadow
//
//  Created by Kurry L Tran on 12/23/12.
//  Copyright (c) 2012 Kurry L Tran. All rights reserved.
//
 
#import "UITableViewCell+DropShadow.h"
#import <QuartzCore/QuartzCore.h>
 
@implementation UITableViewCell (DropShadow)
 
-(void)addDropShadow
{
  self.layer.shadowOffset = CGSizeMake(0, 10);
  self.layer.shadowColor = [[UIColor blackColor] CGColor];
  self.layer.shadowRadius = 3;
  self.layer.shadowOpacity = .75;
  CGRect shadowFrame = self.layer.bounds;
  CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:shadowFrame].CGPath;
  self.layer.shadowPath = shadowPath;
}
 
-(void)removeDropShadow
{
  self.layer.shadowOpacity = 0;
}
@end

Example Usage: 

// Customize the appearance of table view cells.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
       cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

  NSDate *object = _objects[indexPath.row];

  cell.textLabel.text = [object description];
  NSInteger numberOfSections = [self numberOfSectionsInTableView:tableView];
  NSInteger numberOfRows = [self tableView:tableView numberOfRowsInSection:(numberOfSections-1)];

  if (indexPath.section == (numberOfSections - 1) && indexPath.row == (numberOfRows-1))
    [cell addDropShadow];
  else
   [cell removeDropShadow];

  return cell;
}

- (void)viewWillAppear:(BOOL)animated

{
  NSInteger numberOfSections = [self numberOfSectionsInTableView:self.tableView];
  NSInteger numberOfRows = [self tableView:self.tableView numberOfRowsInSection:(numberOfSections-1)];
  NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(numberOfRows -1) inSection:(numberOfSections-1)];
  UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  [cell addDropShadow];
}










Sample Project : http://bit.ly/W10XCU

No comments :