【iOS學習筆記 15-12-19】自定義cell側滑按鈕(UIButton)

     平常中所用的tableViewCell,左滑後,編輯狀態只有一個刪除按鈕,現在很多APP上面都自定義實現了cell側滑自定義編輯btn,比如qq,微信側滑後有“消息置頂”、“標記爲未讀”等。

     之前在cocoachina上看到SWTableViewCell,效果寫的很好,可以支持左滑和右滑自定義btn,當時只是收藏了,留着以後備用。後來在玩手機qq的時候發現這個玩意蠻實用,於是想自己寫一個試試看。於是自己按照理解實現了和qq的cell側滑類似的效果。

     原理很簡單,就是把自定義的btn在cell初始化的時候傳給cell(重寫的初始化方法)。然後把btn按順序佈置在cell的右端(這個沒什麼問題)。最後自定義一個SCContentView繼承於UIView,然後覆蓋在btn上面,添加pangesture手勢,根據手勢的滑動和SCContentView的狀態來進行判斷,簡單的實現動畫效果。

     cell的側滑效果如圖所示


安裝方法:

I 下載zip壓縮包,把SCSwipeTableViewCell文件夾拖到自己工程下面

import SCSwipeTableViewCell.h

在tableViewCell的delegate裏面實現自定義cell的側滑btn

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

    UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 55)]; btn1.backgroundColor = [UIColor redColor]; [btn1 setTitle:@"delete" forState:UIControlStateNormal]; UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 55)]; btn2.backgroundColor = [UIColor greenColor]; [btn2 setTitle:@"add" forState:UIControlStateNormal]; btnArr = [[NSMutableArray alloc]initWithObjects:btn1,btn2, nil];

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, self.view.frame.size.width-20, 55)]; label.text = [NSString stringWithFormat:@"swipeCell test row %ld",(long)indexPath.row];

    static NSString *cellIdentifier = @"Cell"; SCSwipeTableViewCell *cell = (SCSwipeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell = [[SCSwipeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell" withBtns:btnArr tableView:_tableView]; cell.delegate = self; }

    [cell.SCContentView addSubview:label]; return cell; }

II 在pod 上search SCSwipeTableViewCell直接安裝

       不足之處還請大家多多指教!

       Github 下載地址:https://github.com/MonkeyS914/SCSwipeTableViewCell



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章