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

之前想在cell左滑的時(shí)候添加更多的按鈕而不是只有‘刪除’按鈕如下所示,貌似不是一件簡單的事个曙。但是現(xiàn)在只要實(shí)現(xiàn)幾個(gè)方法就行了锈嫩。

代碼寫的比較垃圾,重在理解這個(gè)知識(shí)垦搬。呼寸。

具體代碼:

//
//  TableViewController.m
//  ios8_tableview(左滑添加按鈕)
//
//  Created by mudy on 15/8/21.
//  Copyright (c) 2015年 mudy. All rights reserved.
//



#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)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.

//這個(gè)方法并沒有走,但是也不能刪掉這個(gè)方法

- (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{
//設(shè)置刪除按鈕
UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
    //要先刪除數(shù)據(jù)源里的數(shù)據(jù)
    [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];
}];

//設(shè)置收藏按鈕
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];
}];


//設(shè)置置頂按鈕

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末猴贰,一起剝皮案震驚了整個(gè)濱河市等舔,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌糟趾,老刑警劉巖慌植,帶你破解...
    沈念sama閱讀 206,482評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異义郑,居然都是意外死亡蝶柿,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,377評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門非驮,熙熙樓的掌柜王于貴愁眉苦臉地迎上來交汤,“玉大人,你說我怎么就攤上這事劫笙≤皆” “怎么了?”我有些...
    開封第一講書人閱讀 152,762評(píng)論 0 342
  • 文/不壞的土叔 我叫張陵填大,是天一觀的道長戒洼。 經(jīng)常有香客問我,道長允华,這世上最難降的妖魔是什么圈浇? 我笑而不...
    開封第一講書人閱讀 55,273評(píng)論 1 279
  • 正文 為了忘掉前任寥掐,我火速辦了婚禮,結(jié)果婚禮上磷蜀,老公的妹妹穿的比我還像新娘召耘。我一直安慰自己,他們只是感情好褐隆,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,289評(píng)論 5 373
  • 文/花漫 我一把揭開白布污它。 她就那樣靜靜地躺著,像睡著了一般庶弃。 火紅的嫁衣襯著肌膚如雪轨蛤。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,046評(píng)論 1 285
  • 那天虫埂,我揣著相機(jī)與錄音祥山,去河邊找鬼。 笑死掉伏,一個(gè)胖子當(dāng)著我的面吹牛缝呕,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播斧散,決...
    沈念sama閱讀 38,351評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼供常,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了鸡捐?” 一聲冷哼從身側(cè)響起栈暇,我...
    開封第一講書人閱讀 36,988評(píng)論 0 259
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎箍镜,沒想到半個(gè)月后源祈,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,476評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡色迂,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,948評(píng)論 2 324
  • 正文 我和宋清朗相戀三年香缺,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片歇僧。...
    茶點(diǎn)故事閱讀 38,064評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡图张,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出诈悍,到底是詐尸還是另有隱情祸轮,我是刑警寧澤,帶...
    沈念sama閱讀 33,712評(píng)論 4 323
  • 正文 年R本政府宣布侥钳,位于F島的核電站适袜,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏慕趴。R本人自食惡果不足惜痪蝇,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,261評(píng)論 3 307
  • 文/蒙蒙 一鄙陡、第九天 我趴在偏房一處隱蔽的房頂上張望冕房。 院中可真熱鬧躏啰,春花似錦、人聲如沸耙册。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,264評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽详拙。三九已至帝际,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間饶辙,已是汗流浹背蹲诀。 一陣腳步聲響...
    開封第一講書人閱讀 31,486評(píng)論 1 262
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留弃揽,地道東北人脯爪。 一個(gè)月前我還...
    沈念sama閱讀 45,511評(píng)論 2 354
  • 正文 我出身青樓,卻偏偏與公主長得像矿微,于是被迫代替她去往敵國和親痕慢。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,802評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫涌矢、插件掖举、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,024評(píng)論 4 62
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 171,510評(píng)論 25 707
  • 今年暑假,有幸成為三節(jié)課課程運(yùn)營實(shí)習(xí)生的一員娜庇。我的本碩專業(yè)其實(shí)和互聯(lián)網(wǎng)沒有半毛錢關(guān)系塔次,由于想以小語種作為工具,嘗試...
    BessieLin閱讀 697評(píng)論 0 1
  • 老家村里有一個(gè)弟弟國慶名秀,印象中是一個(gè)受他家環(huán)境的影響俺叭,穿著邋遢,說話高談闊論泰偿,做事有頭無尾熄守、很不讓人信服的人。 這...
    善下歸海閱讀 283評(píng)論 0 1
  • 38 賈蓉 這是一個(gè)苦逼的人耗跛,自己的媳婦兒和老爹睡裕照,還無能為力。第六回劉姥姥進(jìn)榮國府出現(xiàn)调塌,一出現(xiàn)我就覺得我好像明白...
    深巷梨花閱讀 250評(píng)論 0 1