ZKRevealingTableViewController.h
#import <UIKit/UIKit.h> #import "ZKRevealingTableViewCell.h" @interface ZKRevealingTableViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource, ZKRevealingTableViewCellDelegate> @property (nonatomic, retain) ZKRevealingTableViewCell *currentlyRevealedCell; @end
ZKRevealingTableViewController.m
#import "ZKRevealingTableViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface ZKRevealingTableViewController () {
ZKRevealingTableViewCell *_currentlyRevealedCell;
}
@property (nonatomic, retain) NSArray *objects;
@end
@implementation ZKRevealingTableViewController
@synthesize objects;
@dynamic currentlyRevealedCell;
- (void)viewDidLoad {
[super viewDidLoad];
self.objects = [NSArray arrayWithObjects:@"Right", @"Left", @"Both", @"None", nil];
self.tableView = (UITableView *)self.view;
self.tableView.rowHeight = 52.0f;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
}
#pragma mark - Accessors
- (ZKRevealingTableViewCell *)currentlyRevealedCell {
return _currentlyRevealedCell;
}
- (void)setCurrentlyRevealedCell:(ZKRevealingTableViewCell *)currentlyRevealedCell {
if (_currentlyRevealedCell == currentlyRevealedCell)
return;
[_currentlyRevealedCell setRevealing:NO];
if (_currentlyRevealedCell)
[_currentlyRevealedCell autorelease];
[self willChangeValueForKey:@"currentlyRevealedCell"];
_currentlyRevealedCell = [currentlyRevealedCell retain];
[self didChangeValueForKey:@"currentlyRevealedCell"];
}
#pragma mark - ZKRevealingTableViewCellDelegate
- (BOOL)cellShouldReveal:(ZKRevealingTableViewCell *)cell {
return YES;
}
- (void)cellDidReveal:(ZKRevealingTableViewCell *)cell {
self.currentlyRevealedCell = cell;
}
- (void)cellDidBeginPan:(ZKRevealingTableViewCell *)cell {
if (cell != self.currentlyRevealedCell)
self.currentlyRevealedCell = nil;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
self.currentlyRevealedCell = nil;
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return (section == 0) ? @"Bounce" : @"No Bounce";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ZKRevealingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
cell = [[[ZKRevealingTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease];
cell.delegate = self;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
}
cell.textLabel.text = [self.objects objectAtIndex:indexPath.row];
cell.direction = (ZKRevealingTableViewCellDirection)indexPath.row;
cell.shouldBounce = (BOOL)!indexPath.section;
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
if (row % 2 == 0) {
cell.backgroundColor = [UIColor whiteColor];
} else {
cell.backgroundColor = [UIColor colorWithRed:0.892 green:0.893 blue:0.892 alpha:1.0];
}
cell.contentView.backgroundColor = cell.backgroundColor;
}
@end
效果图: