UITableViewCell左滑的時候添加多個按鈕的方法(iOS8+)以及UIRefreshControl(iOS6+)的使用。

以前想在cell左滑的時候添加更多的按鈕而不是隻有‘刪除’按鈕以下所示,貌似不是一件簡單的事。可是如今只要實現幾個方法就好了。ios

代碼寫的比較垃圾,重在理解這個知識。。app

ide

具體代碼:this

//atom

//  TableViewController.mspa

//  ios8_tableview(左滑添加按鈕)orm

//blog

//  Created by mudy on 15/8/21.ci

//  Copyright (c) 2015年 mudy. All rights reserved.rem

//

 

#import "TableViewController.h"

 

@interface TableViewController ()

@property (nonatomic,strong)NSMutableArray *dataSource;

 

@end

 

@implementation TableViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    self.dataSource = [NSMutableArray array];

    self.dataSource = [@[@"mudy1",@"mudy2",@"mudy3",@"mudy4",@"mudy5",@"mudy6"]mutableCopy];

    // Uncomment the following line to preserve selection between presentations.

    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.

    self.navigationItem.rightBarButtonItem = self.editButtonItem;

    

    self.refreshControl = [UIRefreshControl new];

    self.refreshControl.tintColor = [UIColor redColor];

    [self.refreshControl addTarget:self action:@selector(refreshControl:) forControlEvents:UIControlEventValueChanged];

    

    

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]

                                               initWithTarget:self action:@selector(longPressGestureRecognized:)];

    [self.tableView addGestureRecognizer:longPress];

}

 

- (IBAction)longPressGestureRecognized:(UILongPressGestureRecognizer *)sender {

    UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;

    UIGestureRecognizerState state = longPress.state;

    

    CGPoint location = [longPress locationInView:self.tableView];

    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];

    

    

    static UIView       *snapshot = nil;        ///< A snapshot of the row user is moving.

    static NSIndexPath  *sourceIndexPath = nil; ///< Initial index path, where gesture begins.

    

    switch (state) {

        case UIGestureRecognizerStateBegan: {

            if (indexPath) {

                sourceIndexPath = indexPath;

                

                UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

                

                // Take a snapshot of the selected row using helper method.

                snapshot = [self customSnapshotFromView:cell];

                

                // Add the snapshot as subview, centered at cell's center...

                __block CGPoint center = cell.center;

                snapshot.center = center;

                snapshot.alpha = 0.0;

                [self.tableView addSubview:snapshot];

                [UIView animateWithDuration:0.25 animations:^{

                    

                    // Offset for gesture location.

                    center.y = location.y;

                    snapshot.center = center;

                    snapshot.transform = CGAffineTransformMakeScale(1.05, 1.05);

                    snapshot.alpha = 0.98;

                    

                    // Black out.

                    cell.backgroundColor = [UIColor blackColor];

                } completion:nil];

            }

            break;

        }

            

            

        case UIGestureRecognizerStateChanged: {

            CGPoint center = snapshot.center;

            center.y = location.y;

            snapshot.center = center;

            

            // Is destination valid and is it different from source?

            if (indexPath && ![indexPath isEqual:sourceIndexPath]) {

                

                // ... update data source.

                [self.dataSource exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row];

                

                // ... move the rows.

                [self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath];

                

                // ... and update source so it is in sync with UI changes.

                sourceIndexPath = indexPath;

            }

            break;

        }

            

            

        default: {

            // Clean up.

            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:sourceIndexPath];

            [UIView animateWithDuration:0.25 animations:^{

                

                snapshot.center = cell.center;

                snapshot.transform = CGAffineTransformIdentity;

                snapshot.alpha = 0.0;

                

                // Undo the black-out effect we did.

                cell.backgroundColor = [UIColor whiteColor];

                

            } completion:^(BOOL finished) {

                

                [snapshot removeFromSuperview];

                snapshot = nil;

                

            }];

            sourceIndexPath = nil;

            break;

        }

    }

    

    

}

 

 

- (UIView *)customSnapshotFromView:(UIView *)inputView {

    

    UIView *snapshot = [inputView snapshotViewAfterScreenUpdates:YES];

    snapshot.layer.masksToBounds = NO;

    snapshot.layer.cornerRadius = 0.0;

    snapshot.layer.shadowOffset = CGSizeMake(-1.0, 0.0);

    snapshot.layer.shadowRadius = 1.0;

    snapshot.layer.shadowOpacity = 0.4;

    

    return snapshot;

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

-(void)refreshControl:(id)sender{

    [NSThread sleepForTimeInterval:5.0];//刷新5秒鐘

    self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"刷新ing"];

    

    [self.tableView reloadData];

    [self.refreshControl endRefreshing];

    NSLog(@"really?");

}

 

#pragma mark - Table view data source

 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    

    // Return the number of sections.

    return 1;

}

 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    

    // Return the number of rows in the section.

    return self.dataSource.count;

}

 

 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    cell.textLabel.text = [NSString stringWithFormat:@"%@",self.dataSource[indexPath.row]];

    // Configure the cell...

    

    return cell;

}

 

 

 

// Override to support conditional editing of the table view.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

    // Return NO if you do not want the specified item to be editable.

    return YES;

}

 

 

 

// Override to support editing the table view.

//這個方法並無走,可是也不能刪掉這個方法

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        

    } else if (editingStyle == UITableViewCellEditingStyleInsert) {

        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view

    }

}

 

 

 

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{

    //設置刪除按鈕

    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

        //要先刪除數據源裏的數據

        [self.dataSource removeObjectAtIndex:indexPath.row];

        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"刪除" message:@"刪除成功" delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil];

        [alertView show];

        

    }];

    

    

    //設置收藏按鈕

    UITableViewRowAction *collection = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"收藏" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

        

        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"收藏" message:@"收藏成功" delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil];

        [alertView show];

        [self.tableView reloadData];

        

    }];

    

    

    //設置置頂按鈕

    UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置頂" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

        topRowAction.backgroundColor = [UIColor yellowColor];

        [self.dataSource exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];

        NSIndexPath *first = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];

//        NSIndexPath *f =[NSIndexPath inde]

        [tableView moveRowAtIndexPath:indexPath toIndexPath:first];

    }];

    

    deleteRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];

    topRowAction.backgroundColor = [UIColor blueColor];

    deleteRowAction.backgroundColor = [UIColor grayColor];

    collection.backgroundColor = [UIColor brownColor];

    return @[deleteRowAction,collection,topRowAction];

    

}

 

 

// Override to support rearranging the table view.

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {

    

    [tableView moveRowAtIndexPath:fromIndexPath toIndexPath:toIndexPath];

    

}

 

 

 

// Override to support conditional rearranging of the table view.

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {

    // Return NO if you do not want the item to be re-orderable.

    return YES;

}

 

 

/*

 #pragma mark - Navigation

 

 // In a storyboard-based application, you will often want to do a little preparation before navigation

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

 // Get the new view controller using [segue destinationViewController].

 // Pass the selected object to the new view controller.

 }

 */

 

@end

相關文章
相關標籤/搜索