IOS learning notes-Table view 2 (edit)

Source: Internet
Author: User

The editable attribute of the table view, that is, the operation on every unit UITableViewCell of the table. There are five methods in total, four of which are in the UITableViewDataSource protocol, and the last one is in the UITableViewDelegate protocol.

Add the protocol to the. h file and create the object:

@ Interface LinViewController: UIViewController
 
  
// Create a table view object @ property (retain, nonatomic) UITableView * mTableView; // create an array object to store table View data @ property (retain, nonatomic) NSMutableArray * mArray; @ end
 
Add the implementation method to the. m file:

@ Implementation LinViewController // rewrite the initialization method. The original method is in AppDeledate. in the m file-(id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) else {if (self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil]) {// set the navigation bar name self. navigationItem. title = @ "Homepage"; // set the right button in the navigation bar and associate it with UIBarButtonItem * pButtonItem = [[UIBarButtonItem alloc] initWithTitle: @ "BeginEdit" style: UIBarButtonItemStylePlain targe T: self action: @ selector (tableEdit :)]; // Add the button to the navigation bar self. navigationItem. rightBarButtonItem = pButtonItem;} return self;} // The method corresponding to the button on the navigation bar to implement name switching-(void) tableEdit :( id) sender {// three-object operation, set the title of the button based on whether the table view can be edited [sender setTitle: [self. mTableView isEditing]? @ "BeginEdit": @ "EndEdit"]; // sets the editing status of the table view, switching between editable and uneditable [self. mTableView setEditing :! [Self. mTableView isEditing];} // release the created object-(void) dealloc {[_ mTableView release]; [_ mArray release]; [super dealloc];} // view loading-(void) viewDidLoad {[super viewDidLoad]; // initialize the table view object self. mTableView = [[UITableView alloc] initWithFrame: self. view. frame style: UITableViewStylePlain]; // sets the delegate object and the delegate self of the table view. mTableView. dataSource = self; self. mTableView. delegate = self; // Add the table View to the current view. view addSubview: self. mTableView]; // initialize the array object self. mArray = [NSMutableArray arrayWithCapacity: 5]; // set the element in the array to the string type for (int I = 0; I <5; I ++) {NSString * pStr = [NSString stringWithFormat: @ "% d", I]; [self. mArray addObject: pStr] ;}# pragma mark --- UITableVeiwDataSource --- table view protocol-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {// obtain the number of rows of the table to be loaded in the table view. return [self. mArray count];}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {// set a static string for tag, (static to prevent the creation of local variables multiple times, improving efficiency and saving memory) static NSString * identifer = @ "identifer"; // create a cell object (using the mechanism that indicates that the image can be reused) UITableViewCell * Cell = [tableView dequeueReusableCellWithIdentifier: identifer]; // if the cell is empty, create a new Cell if (nil = Cell) {Cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: identifer];} // obtain the number of rows in the current table view NSInteger row = [indexPath row]; // pass the corresponding elements in the array to the text Cell of the view. textLabel. text = [self. mArray objectAtIndex: row]; return Cell;} // sets the edit attribute of the table. There are five methods: // sets whether the current title can be edited. The first method is BOOL) tableView :( UITableView *) tableView canEditRowAtIndexPath :( NSIndexPath *) indexPath {if ([indexPath row] = 0) {return NO;} return YES ;} # pragma mark --- UITableViewDelegate protocol method. This method is used to determine the editing mode. You can use this method to edit the table view, including deleting and inserting the table view, and the second method (UITableViewCellEditingStyle) tableView :( UITableView *) tableView editingStyleForRowAtIndexPath :( NSIndexPath *) indexPath {if ([indexPath row] % 2) {return rows;} return UITableViewCellEditingStyleDelete;} // determine how the table view is edited, method 3-(void) tableView :( UITableView *) tableView commitEditingStyle :( UITableViewCellEditingStyle) editingStyle forRowAtIndexPath :( NSIndexPath *) indexPath {// determine whether the editing mode is to delete if (editingStyle = response) {// remove the element [self. mArray removeObjectAtIndex: [indexPath row]; // The table view starts to update [self. mTableView beginUpdates]; // retrieves array elements and deletes [self. mTableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationLeft]; // The table view terminates the update [self. mTableView endUpdates];} else if (editingStyle = UITableViewCellEditingStyleInsert) {// Add a new element [self. mArray insertObject: @ "New Cell" atIndex: [indexPath row]; // update [self. mTableView beginUpdates]; // retrieves array elements and inserts [self. mTableView insertRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationBottom]; // The table view terminates the update [self. mTableView endUpdates] ;}// you can set the dynamic attribute of the number of rows in the table view. The fourth method is (BOOL) tableView :( UITableView *) tableView canMoveRowAtIndexPath :( NSIndexPath *) indexPath {if ([indexPath row] = 2) {return NO;} return YES;} // edit the fifth method of moving the table view-(void) tableView :( UITableView *) tableView moveRowAtIndexPath :( NSIndexPath *) sourceIndexPath toIndexPath :( NSIndexPath *) destinationIndexPath {// get the row NSInteger fromRow = [sourceIndexPath row]; // obtain the row to be moved to NSInteger toRow = [destinationIndexPath row]; // obtain the object id object corresponding to the subscript before moving in the array = [self. mArray objectAtIndex: fromRow]; // Delete [self. mArray removeObjectAtIndex: fromRow]; // insert [self. mArray insertObject: object atIndex: toRow];}-(void) didReceiveMemoryWarning {[super didreceivemorywarning];} @ end



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.