Multi-select mode for Tabelview

Source: Internet
Author: User

@interface Viewcontroller () <UITableViewDelegate,UITableViewDataSource>

@property (Nonatomic,strong) Nsmutablearray *datasourcearray;

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

Self.datasourcearray = [Nsmutablearray arraywithobjects:@ "A", @ "B", @ "C", @ "D", @ "E", @ "F", @ "G", @ "H", @ "I", @ "J", @ "K", @ "L", @ "M", nil];

UITableView *tableview = [[UITableView alloc]initwithframe:cgrectmake (0, 0, Self.view.frame.size.width, self.view.frame.size.height) Style:uitableviewstyleplain];

Tableview.delegate= self;

Tableview.datasource = self;

Open Edit State

[TableView Setediting:yes Animated:yes];

[Self.view Addsubview:tableview];

}

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section

{

return self.dataSourceArray.count;

}

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath

{

static NSString *cellidentifier = @ "Cell";

UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];

if (!cell)

{

cell = [[UITableViewCell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier];

}

Cell.textLabel.text = Self.datasourcearray[indexpath.row];

return cell;

}

Each of these two parameters is written in its own state, and the two are written together as the front circled multi-select state

-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) Indexpath

{

return Uitableviewcelleditingstyledelete | Uitableviewcelleditingstyleinsert;

}

Click cell Select to add something to your data source array

-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath {

}

This is the thing that cancels the click of the cell to delete your data source

-(void) TableView: (UITableView *) TableView Diddeselectrowatindexpath: (Nsindexpath *) indexpath{

}

Delete your cell and your data source items according to the above two methods remember to refresh the table

The effect of the implementation

Multi-select mode for Tabelview

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.