UITableViewCell the method of adding multiple buttons (ios8+) and the use of Uirefreshcontrol (ios6+) when left sliding.

Source: Internet
Author: User

It doesn't seem like a simple thing to add more buttons to the cell when it's left, rather than just the ' delete ' button as shown below. But now just implement a few methods.

Code to write the comparison of garbage, heavy in understanding this knowledge.

。

Specific code:

//

Tableviewcontroller.m

Ios8_tableview (left slide Add button)

//

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 a 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 was 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's 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 the can be recreated.

}

-(void) Refreshcontrol: (ID) sender{

[Nsthread sleepfortimeinterval:5.0];//refresh for 5 seconds

Self.refreshControl.attributedTitle = [[nsattributedstring alloc]initwithstring:@ "refresh 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 the 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 don't want the specified item to be editable.

return YES;

}

Override to support editing the table view.

This method does not go, but it cannot be erased.

-(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{

Set Delete button

Uitableviewrowaction *deleterowaction = [uitableviewrowaction rowactionwithstyle: uitableviewrowactionstyledestructive title:@ "Delete" handler:^ (uitableviewrowaction *action, NSIndexPath *indexPath) {

To delete data from the data source first

[Self.datasource RemoveObjectAtIndex:indexPath.row];

[Self.tableview Deleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationautomatic];

Uialertview *alertview = [[uialertview alloc]initwithtitle:@ ' delete ' message:@ ' delete succeeded ' delegate:self cancelButtonTitle:@ ' OK "Otherbuttontitles:nil, nil";

[Alertview show];

}];

Set Favorite button

Uitableviewrowaction *collection = [uitableviewrowaction rowactionwithstyle:uitableviewrowactionstyledefault title: @ "Collection" handler:^ (uitableviewrowaction *action, Nsindexpath *indexpath) {

Uialertview *alertview = [[Uialertview alloc]initwithtitle:@ ' Favorites ' message:@ ' collection succeeded ' delegate:self cancelButtonTitle:@ ' OK "Otherbuttontitles:nil, nil";

[Alertview show];

[Self.tableview Reloaddata];

}];

Set the sticky button

Uitableviewrowaction *toprowaction = [Uitableviewrowaction rowactionwithstyle:uitableviewrowactionstyledefault title:@ "pinned" 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 don't want the item to be re-orderable.

return YES;

}

/*

#pragma mark-navigation

In a storyboard-based application, you'll 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

UITableViewCell the method of adding multiple buttons (ios8+) and the use of Uirefreshcontrol (ios6+) when left sliding.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.