自定义Table View Cell左右滑动的使用

来源:互联网 发布:打开页面就执行js 编辑:程序博客网 时间:2024/06/11 21:55

Apple 通过 iOS 7 的邮件(Mail)自带应用中介绍了一种新的用户界面操作方案,向左或右滑动以显示一个有着多个操作的菜单。接下来将会向你展这样一个 Table View Cell。如果你还不知道一个可滑动的 Table View Cell 效果,那么看看 Apple 自带的邮件应用吧:


前几天公司项目一个项目刚好要做这样一个效果,于是找我找了很多dome都不行,没办法只能自己写了,不过我也是改装了别人的毕竟项目比较急,一时半会要自己完全去写不太可能。Table View Cell 的滑动效果,说实话,我研究了一天多的时间,其实不为别的,只为脱离storyboard(故事版),个人不习惯用storyboard,觉得不太灵活。下面贴代码:


第一步:添加 SWTableViewCellDelegate然后创建你的Table View Cell 

#import <UIKit/UIKit.h>#import "BaseTableViewController.h"#import "SWTableViewCell.h"@interface BeaconMsgList : BaseTableViewController <UITableViewDataSource,UITableViewDelegate,SWTableViewCellDelegate>@property (nonatomic, strong) UITableView *beaconlist;@end<span style="color:#3366ff;"></span>

第二步:实现SWTableViewCellDelegate方法

#define HEIGHT self.view.frame.size.height#define WIDTH self.view.frame.size.width#import "BeaconMsgList.h"@interface BeaconMsgList ()@property (nonatomic) BOOL useCustomCells;@end@implementation BeaconMsgList- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.        [self setBackNavButton];    [self setNavTitle:@"sales & store events"];    [self.view setBackgroundColor:[UIColor whiteColor]];    self.automaticallyAdjustsScrollViewInsets = NO;        [self initUI];}/* *创建UI * */- (void) initUI{    _beaconlist = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) style:UITableViewStylePlain];    _beaconlist.dataSource = self;    _beaconlist.delegate = self;    [self.view addSubview:_beaconlist];           UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40)];    UILabel *label = [[UILabel alloc]  initWithFrame:CGRectMake(5, 10, WIDTH, 30)];    label.textColor = [UIColor blueColor];    label.font = [UIFont systemFontOfSize:15];    label.text = @"Reminder of Oakvile Place Promotions";    [view addSubview:label];        [_beaconlist setTableHeaderView:view];}#pragma mark - Table view data source- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {        return 1;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 105;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {        return 4;}- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {//    if (editingStyle == UITableViewCellEditingStyleInsert) {//        //[_objects removeObjectAtIndex:indexPath.row];//        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];//    } else {//        NSLog(@"Unhandled editing style! %@", indexPath);//    }}- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {    return NO;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static NSString *cellIdentifier = @"Cell";        SWTableViewCell *cell = (SWTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];        if (cell == nil) {                cell = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];        cell.rightUtilityButtons = [self rightButtons];        cell.delegate = self;        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;        cell.selectionStyle = 0;        UIImageView *icon = [[UIImageView alloc] init];        icon.frame = CGRectMake(10, 5, 40, 30);        icon.tag = 'i';        [cell.contentView addSubview:icon];                UILabel *title = [[UILabel alloc] init];        title.frame = CGRectMake(CGRectGetMaxX(icon.frame)+5, 0, WIDTH-icon.frame.size.width-35, 30);        title.tag = 't';        [cell.contentView addSubview:title];                UILabel *details = [[UILabel alloc] init];         details.frame = CGRectMake(CGRectGetMaxX(icon.frame)+5, CGRectGetMaxY(title.frame)-5, WIDTH-icon.frame.size.width-35, 50);        details.tag = 'd';        [cell.contentView addSubview:details];                UILabel *text = [[UILabel alloc] init];         text.frame = CGRectMake(CGRectGetMaxX(icon.frame)+5, CGRectGetMaxY(details.frame), WIDTH-icon.frame.size.width-35, 30);        text.tag = 'e';        [cell.contentView addSubview:text];            }           UIImageView *icon = (UIImageView*)[cell viewWithTag:'i'];    icon.image = [UIImage imageNamed:@"57.png"];        UILabel *title  = (UILabel*)[cell viewWithTag:'t'];    title.font = [UIFont systemFontOfSize:12];        UILabel *details  = (UILabel*)[cell viewWithTag:'d'];    details.font = [UIFont systemFontOfSize:12];    details.lineBreakMode = YES;    details.numberOfLines = 0;    details.textColor = [UIColor lightGrayColor];        UILabel *text  = (UILabel*)[cell viewWithTag:'e'];    text.font = [UIFont systemFontOfSize:12];    text.textColor = [UIColor blackColor];        title.text = @"Free Burger with Purchase of Drink";    details.text = @"Buy any snakk drink and get your chose of teen burger.juset present the coupon to redeem.";    text.text = @"Vialid until clocse of business March 8";        return cell;}#pragma mark Tableviewcell 点击查看详情- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{   }- (NSArray *)rightButtons{    NSMutableArray *rightUtilityButtons = [NSMutableArray new];    [rightUtilityButtons sw_addUtilityButtonWithColor:     [UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0]                                                title:@"More"];    [rightUtilityButtons sw_addUtilityButtonWithColor:     [UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f]                                                title:@"Delete"];        return rightUtilityButtons;}#pragma mark - SWTableViewDelegate- (void)swipeableTableViewCell:(SWTableViewCell *)cell scrollingToState:(SWCellState)state{    switch (state) {        case 0:            NSLog(@"utility buttons closed");            break;        case 1:            NSLog(@"left utility buttons open");            break;        case 2:            NSLog(@"right utility buttons open");            break;        default:            break;    }}- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerLeftUtilityButtonWithIndex:(NSInteger)index{    switch (index) {        case 0:            NSLog(@"left button 0 was pressed");            break;        case 1:            NSLog(@"left button 1 was pressed");            break;        case 2:            NSLog(@"left button 2 was pressed");            break;        case 3:            NSLog(@"left btton 3 was pressed");        default:            break;    }}- (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index{    switch (index) {        case 0:        {            NSLog(@"More button was pressed");            UIAlertView *alertTest = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"More more more" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles: nil];            [alertTest show];                        [cell hideUtilityButtonsAnimated:YES];            break;        }        case 1:        {            // Delete button was pressed            UIAlertView *alertTest = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"Delete Delete Delete" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles: nil];            [alertTest show];                        [cell hideUtilityButtonsAnimated:YES];            break;        }        default:            break;    }}- (BOOL)swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:(SWTableViewCell *)cell{    // allow just one cell's utility button to be open at once    return YES;}- (BOOL)swipeableTableViewCell:(SWTableViewCell *)cell canSwipeToState:(SWCellState)state{    switch (state) {        case 1:            // set to NO to disable all left utility buttons appearing            return YES;            break;        case 2:            // set to NO to disable all right utility buttons appearing            return YES;            break;        default:            break;    }        return YES;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end


我做出的效果如下图,这是在实际项目中的应用不方便给出工程。

       

最后想要dome的请留言,我可以单独做一个dome,如果有更好的希望和大家可以多多交流。



0 0
原创粉丝点击