Cell operations in UITableView in iOS Learning

Source: Internet
Author: User

Next, let's take a look at the simple use of Table View in iOS. Here we will focus on Cell operations in UITableView, including tag, move, delete, and insert.

For the sake of simplicity, directly from the original code, code: http://download.csdn.net/detail/totogo2010/4361870

To perform data operations, change the immutable array in the code to variable:

NSArray * list-> NSMutableArray * list

1. Mark the Cell.

The effect is as follows:

Open a project,

-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath.

Add code

-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {// NSString * rowString = [self. list objectAtIndex: [indexPath row]; // UIAlertView * alter = [[UIAlertView alloc] initWithTitle: @ "selected row information" message: rowString delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil, nil]; // [alter show]; UITableViewCell * cell = [tableView cellForRowAtIndexPath: indexPath]; if (cell. accessoryType = UITableViewCellAccessoryNone) {cell. accessoryType = UITableViewCellAccessoryCheckmark;} else {cell. accessoryType = UITableViewCellAccessoryNone;} [tableView deselectRowAtIndexPath: indexPath animated: YES];}

Tag has four effects:

UITableViewCellAccessoryCheckmark
UITableViewCellAccessoryDetailDisclosureButton
UITableViewCellAccessoryDisclosureIndicator
UITableViewCellAccessoryNone

You can try it on your own.

2. delete cells

To move or delete rows, you must start the table editing mode. The setEditing: animated: method is used.

Open xib and generate Table IBoutlet ing tableView;

Add in viewDidload

[Self. tableViewsetEditing: YES];

This is to start the running program,

In editable mode, the deleted icon is displayed by default.

Code for deletion:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {    NSUInteger row = [indexPath row];    if (editingStyle == UITableViewCellEditingStyleDelete) {        [self.list removeObjectAtIndex:row];         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]                         withRowAnimation:UITableViewRowAnimationAutomatic];     }}

The editingStyle parameter is UITableViewCellEditingStyleDelete.

The row deletion method has a constant: UITableViewRowAnimationAutomatic, which indicates the effect of deletion. Similar constants include:
UITableViewRowAnimationAutomatic
UITableViewRowAnimationTop
UITableViewRowAnimationBottom
UITableViewRowAnimationLeft
UITableViewRowAnimationRight
UITableViewRowAnimationMiddle
UITableViewRowAnimationFade
UITableViewRowAnimationNone

Open the constant name to see the effect.

If this is run, you can delete one of the Cell lines.

3. Mobile Cell

Add the following code:

3.1 remove the default deleted icon first

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {     return UITableViewCellEditingStyleInsert; } 

3.2 return whether the current Cell can be moved

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {     return YES; }

3.3 perform the mobile operation

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {    NSUInteger fromRow = [sourceIndexPath row];     NSUInteger toRow = [destinationIndexPath row];         id object = [self.list objectAtIndex:fromRow];     [self.list removeObjectAtIndex:fromRow];     [self.list insertObject:object atIndex:toRow]; }

Run the program:

How to move it? Do not move any part of the row. You must hold down the icon on the leftmost bar to move it.

4. Insert cell:

4.1 Insert and delete operations are similar.

-(Void) tableView :( UITableView *) tableView commitEditingStyle:

(UITableViewCellEditingStyle) editingStyle forRowAtIndexPath :( NSIndexPath *) indexPath

Add UITableViewCellEditingStyleInsert judgment

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {    NSUInteger row = [indexPath row];    if (editingStyle == UITableViewCellEditingStyleDelete) {        [self.list removeObjectAtIndex:row];         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]                         withRowAnimation:UITableViewRowAnimationAutomatic];     }else if(editingStyle == UITableViewCellEditingStyleInsert ){        NSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath,nil];        [self.list insertObject:@"inset new Cell" atIndex:row];        [tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationMiddle];    }}

4.2 modify the icon to insert a style.

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {     return UITableViewCellEditingStyleInsert; } 

Run, click the plus icon,

Done!

Example code: http://download.csdn.net/detail/totogo2010/4398669

Copyright Disclaimer: This article will be published at http://blog.csdn.net/totogo2010/, and you will be welcomed to enjoy the transfer and sharing. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you.


Related Article

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.