iOS uitableview table View (2)

Source: Internet
Author: User

TableView Edit

1, let TableView in the editing state

2. Specify tableview which lines can be edited

3. Specify the style of TableView edit (add, delete)

4, editing completed (first manipulate the data source, and then modify the UI)

TableView Mobile

1, let TableView in the editing state

2. Specify TableView which rows can be moved

3. Mobile Complete

4. Monitor the mobile process to achieve limited cross-region movement

to edit or move, let the tableview into the editing state first. Edit end or Move end, modify the data in the array or dictionary before changing the UI.

Uitableviewcontroller is packaged with various delegate and DataSource to improve our development speed.



<span style= "Font-size:18px;color: #330033;" > #import "RootViewController.h" #import "RootView.h" @interface Rootviewcontroller () <uitableviewdatasource, uitableviewdelegate>{Uitableviewcelleditingstyle _style;} @property (Nonatomic,strong) Rootview *myview; @property (nonatomic,strong) Nsmutablearray *grouparray;@ End@implementation rootviewcontroller-(ID) initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *)    nibbundleornil{self = [super Initwithnibname:nibnameornil Bundle:nibbundleornil];    if (self) {self.myview = [[Rootview alloc] Initwithframe:[uiscreen mainscreen].bounds]; } return self;} -(void) loadview{self.view = Self.myview;}    -(void) viewdidload{[Super Viewdidload];    Self.grouparray = [Nsmutablearray array];    Set proxy self.myview.TableView.dataSource = self;    Set proxy self.myview.TableView.delegate = self;    [Self addaction]; }-(void) addaction{//Create a Barbutton button (delete button) Uibarbuttonitem *deletebutton = [[Uibarbuttonitem AllOC] initwithtitle:@ "delete" style: (uibarbuttonitemstyledone) target:self action: @selector (Daletebutton:)];    Self.navigationItem.leftBarButtonItem = DeleteButton; Uibarbuttonitem *addbutton = [[Uibarbuttonitem alloc] initwithtitle:@ "Add" style: (uibarbuttonitemstyledone) Target:    Self action: @selector (AddButton:)];    Self.navigationItem.rightBarButtonItem = AddButton;        Create a dummy data for (int i = 0;i < 3;i++) {Nsmutablearray *temparray = [Nsmutablearray array];        for (int j = 0;j < 5;j++) {[Temparray addobject:[nsstring stringwithformat:@ "group%d,%d person", i,j]];    } [Self.grouparray Addobject:temparray];        }//NSLog (@ "%@", self.grouparray[0]); }//Edit Status//delete button-(void) Daletebutton: (Uibarbuttonitem *) sender{//When the "delete" button is clicked, trigger the event, give him an edit state (delete state) _style = UITableView    Celleditingstyledelete;    A bool receives the edit state at this time bool flag = self.myview.TableView.editing;    Set Edit Status [Self.myview.TableView Setediting:!flag Animated:yes]; }//Add button-(void) AddButton: (Uibarbuttonitem *) sender{_style = Uitableviewcelleditingstyleinsert;    BOOL flag = self.myview.TableView.editing;    [Self.myview.TableView Setediting:!flag Animated:yes]; }/*-----------------------Uitableviewdatasource-----------------------------*/// The following methods are to follow the Uitableviewdatasource protocol, so first set the agent in Viewdidload//set a few partitions-(Nsinteger) Numberofsectionsintableview: ( UITableView *) tableview{return self.groupArray.count;} Set a partition with a few lines-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return [ Self.grouparray[section] count];}    Set cell-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{//Logo    static NSString *cell_id = @ "cell_id";    Create cell UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cell_id]; Determine if there is no cell in the reuse pool, if not, create if (nil = = cell) {cell = [[UITableViewCell alloc] Initwithstyle: (uitableviewcellsty leValue1) reuseidentifier:cell_id];       }//Set the title for cell Cell.textLabel.text = Self.grouparray[indexpath.section][indexpath.row];        Set subtitle for cell cell.detailTextLabel.text = @ "I won the lottery"; return cell;} Specifies which rows can be edited-(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (Nsindexpath *) indexpath{//    For example, let a single line be in the edited state if (indexpath.row% 2) {return NO; } return YES; Edit style (add, delete)-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: ( Nsindexpath *) indexpath{return _style;} Edit Complete-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle    Forrowatindexpath: (Nsindexpath *) indexpath{//When the Edit status is add state if (Editingstyle = = Uitableviewcelleditingstyledelete)    {//delete (which row of which partition is determined) [Self.grouparray[indexpath.section]removeobjectatindex:indexpath.row];    Refresh data [TableView Reloaddata];    Refresh data animation//[TableView Deleterowsatindexpaths:@[indexpath] withrowanimation: (Uitableviewrowanimationfade)]; }    else if (Editingstyle = = Uitableviewcelleditingstyleinsert) {NSString *string = @ "Test data";                Modify the data source (implement add) [Self.grouparray[indexpath.section]insertobject:string atindex:indexpath.row+1];        Modify UI//[TableView reloaddata];        [TableView Insertrowsatindexpaths:@[indexpath] withrowanimation: (uitableviewrowanimationtop)];        Add data to the display to the Add line below nsindexpath *indes = [Nsindexpath indexpathforrow:indexpath.row+1 inSection:indexPath.section];            Refresh the data to drive the picture effect [TableView insertrowsatindexpaths:@[indes] withrowanimation: (uitableviewrowanimationtop)]; }}/*-------------------------------tableview move----------------------------------------*///Specify which is moved-(BOOL) TableView: (UITableView *) TableView Canmoverowatindexpath: (Nsindexpath *) indexpath{return YES; Move complete-(void) TableView: (UITableView *) TableView Moverowatindexpath: (Nsindexpath *) Sourceindexpath Toindexpath: (  Nsindexpath *) destinationindexpath{      Record id obj = Self.grouparray[sourceindexpath.section][sourceindexpath.row];    remove [Self.grouparray[sourceindexpath.section]removeobjectatindex:sourceindexpath.row];    Add [Self.grouparray[destinationindexpath.section]insertobject:obj AtIndex:destinationIndexPath.row];        Update data source [TableView Reloaddata]; }//Restricted Mobile area-(nsindexpath*) TableView: (UITableView *) TableView Targetindexpathformovefromrowatindexpath: (Nsindexpath *) Sourceindexpath Toproposedindexpath: (Nsindexpath *) proposeddestinationindexpath{//First determine if the source and destination groupings are the same, if (Sourceinde    Xpath.section! = proposeddestinationindexpath.section) {//If different, returns the starting position of the source return sourceindexpath;    } else {//if same, returns the location of the destination return proposeddestinationindexpath; }}</span>

iOS uitableview table View (2)

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.