Ui3_uitableviewdelete (multiple selection)

Source: Internet
Author: User

  appdelegate.m//  Ui3_uitableviewdelete (multiple selection)////  Created by zhangxueming on 15/7/14.//  Copyright (c ) 2015 zhangxueming. All rights reserved.//#import "AppDelegate.h" #import "ViewController.h" @interface appdelegate () @end @implementation appdelegate-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (NSDictionary *) launchoptions {    //Override point for customization after application launch.    Viewcontroller *root = [[Viewcontroller alloc] init];    Uinavigationcontroller *nav = [[Uinavigationcontroller alloc] initwithrootviewcontroller:root];    Self.window.rootViewController = nav;    [Self.window makekeyandvisible];        return YES;}

viewcontroller.h//Ui3_uitableviewdelete (multiple selection)////Created by zhangxueming on 15/7/14.//Copyright (c) 2015 ZHANGX Ueming. All rights reserved.//#import <UIKit/UIKit.h> @interface Viewcontroller:uiviewcontroller < Uitableviewdatasource, uitableviewdelegate> @end////viewcontroller.m//ui3_uitableviewdelete (multiple selection)////Created by Zhangxueming on 15/7/14.//Copyright (c) 2015 zhangxueming.    All rights reserved.//#import "ViewController.h" @interface Viewcontroller () {UITableView *_tableview;    Data source Nsmutablearray *_datalist; The data to be deleted Nsmutablearray *_removelist;}    @end @implementation viewcontroller-(void) viewdidload {[Super viewdidload];    Additional setup after loading the view, typically from a nib.    [Self creatdatasource]; [Self creatui];}    Create data source-(void) creatdatasource{_datalist = [Nsmutablearray array];    _removelist = [Nsmutablearray array];    Nsmutablearray *boys = [Nsmutablearray array];     for (int i=0; i<20; i++) {   NSString *name = [NSString stringwithformat:@ "boy:%d", i+1];    [Boys Addobject:name];        } [_datalist Addobject:boys];    Nsmutablearray *grils = [Nsmutablearray array];        for (int i= 0; i<10; i++) {nsstring *name = [NSString stringwithformat:@ "gril:%d", i+1];    [Grils Addobject:name]; } [_datalist addobject:grils];}        Create ui-(void) creatui{_tableview = [[UITableView alloc] InitWithFrame:self.view.frame Style:uitableviewstyleplain];    Set proxy _tableview.delegate = self;        _tableview.datasource = self;    Set tail view UIView *footerview = [[UIView alloc] Initwithframe:cgrectmake (0,0, Self.view.frame.size.width, 44)];    Footerview.backgroundcolor = [Uicolor Cyancolor];    _tableview.tablefooterview = Footerview;    NSLog (@ "view =%@", _tableview.tablefooterview);    Set Head view UIView *headerview = [[UIView alloc] Initwithframe:cgrectmake (0, 0, Self.view.frame.size.height, 44)];    Headerview.backgroundcolor = [Uicolor Yellowcolor]; _taBleview.tableheaderview = Headerview;    Add btn UIButton *btn = [UIButton Buttonwithtype:uibuttontypesystem];    Btn.frame =cgrectmake (0, footerview.frame.size.width-100,44);    [Btn settitle:@ "Remove" forstate:uicontrolstatenormal];    [Btn addtarget:self Action: @selector (Removedata) forcontrolevents:uicontroleventtouchupinside];        [Footerview ADDSUBVIEW:BTN];            Set Edit button Self.navigationItem.leftBarButtonItem = Self.editbuttonitem; [Self.view Addsubview:_tableview];}    Enter edit state-(void) setediting: (BOOL) editing animated: (BOOL) animated{[Super setediting:editing Animated:yes]; [_tableview setediting:editing animated:yes];} editing-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: ( Nsindexpath *) indexpath{//Set insert and delete to automatically enter multi-select status return uitableviewcelleditingstyledelete| Uitableviewcelleditingstyleinsert;} Delete data-(void) removedata{for (int i=0; i<2; i++) {Nsarray *array = [[_data]List objectatindex:i] copy];        Nsinteger rows = Array.count;            for (int j=0; j<rows; J + +) {NSString *obj = [array objectatindex:j];            if ([_removelist containsobject:obj]) {[[_datalist objectatindex:i] removeobject:obj]; }}} [_tableview reloaddata];} #pragma mark---uitableview---//return partition number-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{return _dat Alist.count;} Returns the number of rows per partition-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return [_ DataList Objectatindex:section] count];} Return uitableviewcell-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *)    indexpath{static NSString *cellid = @ "Cell";    UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellid];    if (!cell) {cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellid]; } cell.accessOrytype = Uitableviewcellaccessorydisclosureindicator;    Cell.textLabel.text = [[_datalist objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; return cell;} Checked state-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{if (Tableview.ed        iting) {NSString *obj = [[_datalist objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; if (![        _removelist Containsobject:obj]) {[_removelist addobject:obj]; }}}//End Check state-(void) TableView: (UITableView *) TableView Diddeselectrowatindexpath: (Nsindexpath *) indexpath{if (table        view.editing) {NSString *obj = [[_datalist objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];        if ([_removelist containsobject:obj]) {[_removelist removeobject:obj];    }}}-(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end

Ui3_uitableviewdelete (multiple selection)

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.