Mobile Section and Cell of UITableView in IOS development (28)

Source: Internet
Author: User

1 Preface
Today, let's learn how to move Section and Cell in the UITableView control.

2. code example
ZYViewController. h

 

[Plain] # import <UIKit/UIKit. h>
 
@ Interface ZYViewController: UIViewController <UITableViewDelegate, UITableViewDataSource>
 
@ Property (nonatomic, strong) UITableView * myTableView;
@ Property (nonatomic, strong) NSMutableArray * arrayOfSections; // Data Source
 
 
@ End

# Import <UIKit/UIKit. h>

@ Interface ZYViewController: UIViewController <UITableViewDelegate, UITableViewDataSource>

@ Property (nonatomic, strong) UITableView * myTableView;
@ Property (nonatomic, strong) NSMutableArray * arrayOfSections; // Data Source


@ End
ZYViewController. m

 

[Plain] @ synthesize myTableView;
@ Synthesize arrayOfSections;
 
// Initialize the content of each Cell of the Data Source
-(NSMutableArray *) newSectionWithIndex :( NSUInteger) paramIndex withCellCount :( NSUInteger) paramCellCount {
NSMutableArray * result = [[NSMutableArray alloc] init];
NSUInteger counter = 0;
For (counter = 0; counter <paramCellCount; counter ++ ){
[Result addObject: [[NSString alloc] initWithFormat: @ "Section % lu Cell % lu", (unsigned long) paramIndex, (unsigned long) counter + 1];
}
Return result;
}
 
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Initialize the data source
ArrayOfSections = [[NSMutableArray alloc] init];
NSMutableArray * section1 = [self newSectionWithIndex: 1 withCellCount: 3];
NSMutableArray * section2 = [self newSectionWithIndex: 2 withCellCount: 3];
NSMutableArray * section3 = [self newSectionWithIndex: 3 withCellCount: 3];
[ArrayOfSections addObject: section1];
[ArrayOfSections addObject: section2];
[ArrayOfSections addObject: section3];
MyTableView = [[UITableView alloc] initWithFrame: self. view. bounds style: UITableViewStyleGrouped];
Self. myTableView. autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// Add a proxy
MyTableView. delegate = self;
MyTableView. dataSource = self;
// Add the right button
Self. navigationItem. rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle: @ "MoveSection" style: UIBarButtonItemStylePlain target: self action: @ selector (movewSection1ToSection3)]
// Add the left button
Self. navigationItem. leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle: @ "MoveCell" style: UIBarButtonItemStylePlain target: self action: @ selector (timer)];
[Self. view addSubview: myTableView];

}
// Number of lines in a Section
-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {
NSInteger result = 0;
If ([tableView isEqual: myTableView]) {
Result = (NSInteger) [self. arrayOfSections count];
}
Return result;
}
// Number of Cell lines in each Section
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {
NSInteger result = 0;
If ([tableView isEqual: myTableView]) {
If ([arrayOfSections count]> section ){
NSMutableArray * sectionArray = [self. arrayOfSections objectAtIndex: section];
Result = (NSInteger) [sectionArray count];
}
}
Return result;
}
// Set the content of each Cell line
 
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {
UITableViewCell * result = nil;
If ([tableView isEqual: myTableView]) {
Static NSString * CellIdentifier = @ "CellIdentifier ";
Result = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
}
NSMutableArray * sectionArray = [self. arrayOfSections objectAtIndex: indexPath. section];
Result. textLabel. text = [sectionArray objectAtIndex: indexPath. row];
Return result;
}
// Move the Section of the first index to the last position.
-(Void) movewSection1ToSection3 {
NSMutableArray * section1 = [self. arrayOfSections objectAtIndex: 0];
[Self. arrayOfSections removeObject: section1];
[Self. arrayOfSections addObject: section1];

[MyTableView moveSection: 0 toSection: 2];
}
// Swap the first Cell and the second Cell in the first Section
-(Void) moveCell1InSectionToCell2InSection1 {
NSMutableArray * section1 = [self. arrayOfSections objectAtIndex: 0];
NSString * cell1InSection1 = [section1 objectAtIndex: 0];
[Section1 removeObject: cell1InSection1];
[Section1 insertObject: cell1InSection1 atIndex: 1];
NSIndexPath * sourceIndexPath = [NSIndexPath indexPathForRow: 0 inSection: 0];
NSIndexPath * destinationIdexPath = [NSIndexPath indexPathForRow: 1 inSection: 0];
[Self. myTableView moveRowAtIndexPath: sourceIndexPath toIndexPath: destinationIdexPath];
}
// Move the second Cell of the first Section to the position before the first Cell of the second Section
-(Void) moveCell2InSectionToCellInSection2 {
NSMutableArray * section1 = [arrayOfSections objectAtIndex: 0];
NSMutableArray * section2 = [arrayOfSections objectAtIndex: 1];

NSString * cell2InSection1 = [section1 objectAtIndex: 1];
[Section1 removeObject: cell2InSection1];
[Section2 insertObject: cell2InSection1 atIndex: 0];

NSIndexPath * sourceIndexPath = [NSIndexPath indexPathForRow: 1 inSection: 0];
NSIndexPath * destinationIndexPath = [NSIndexPath indexPathForRow: 0 inSection: 1];

[MyTableView moveRowAtIndexPath: sourceIndexPath toIndexPath: destinationIndexPath];
}

@ Synthesize myTableView;
@ Synthesize arrayOfSections;

// Initialize the content of each Cell of the Data Source
-(NSMutableArray *) newSectionWithIndex :( NSUInteger) paramIndex withCellCount :( NSUInteger) paramCellCount {
NSMutableArray * result = [[NSMutableArray alloc] init];
NSUInteger counter = 0;
For (counter = 0; counter <paramCellCount; counter ++ ){
[Result addObject: [[NSString alloc] initWithFormat: @ "Section % lu Cell % lu", (unsigned long) paramIndex, (unsigned long) counter + 1];
}
Return result;
}

-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Initialize the data source
ArrayOfSections = [[NSMutableArray alloc] init];
NSMutableArray * section1 = [self newSectionWithIndex: 1 withCellCount: 3];
NSMutableArray * section2 = [self newSectionWithIndex: 2 withCellCount: 3];
NSMutableArray * section3 = [self newSectionWithIndex: 3 withCellCount: 3];
[ArrayOfSections addObject: section1];
[ArrayOfSections addObject: section2];
[ArrayOfSections addObject: section3];
MyTableView = [[UITableView alloc] initWithFrame: self. view. bounds style: UITableViewStyleGrouped];
Self. myTableView. autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// Add a proxy
MyTableView. delegate = self;
MyTableView. dataSource = self;
// Add the right button
Self. navigationItem. rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle: @ "MoveSection" style: UIBarButtonItemStylePlain target: self action: @ selector (movewSection1ToSection3)]
// Add the left button
Self. navigationItem. leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle: @ "MoveCell" style: UIBarButtonItemStylePlain target: self action: @ selector (timer)];
[Self. view addSubview: myTableView];

}
// Number of lines in a Section
-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {
NSInteger result = 0;
If ([tableView isEqual: myTableView]) {
Result = (NSInteger) [self. arrayOfSections count];
}
Return result;
}
// Number of Cell lines in each Section
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {
NSInteger result = 0;
If ([tableView isEqual: myTableView]) {
If ([arrayOfSections count]> section ){
NSMutableArray * sectionArray = [self. arrayOfSections objectAtIndex: section];
Result = (NSInteger) [sectionArray count];
}
}
Return result;
}
// Set the content of each Cell line

-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {
UITableViewCell * result = nil;
If ([tableView isEqual: myTableView]) {
Static NSString * CellIdentifier = @ "CellIdentifier ";
Result = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
}
NSMutableArray * sectionArray = [self. arrayOfSections objectAtIndex: indexPath. section];
Result. textLabel. text = [sectionArray objectAtIndex: indexPath. row];
Return result;
}
// Move the Section of the first index to the last position.
-(Void) movewSection1ToSection3 {
NSMutableArray * section1 = [self. arrayOfSections objectAtIndex: 0];
[Self. arrayOfSections removeObject: section1];
[Self. arrayOfSections addObject: section1];

[MyTableView moveSection: 0 toSection: 2];
}
// Swap the first Cell and the second Cell in the first Section
-(Void) moveCell1InSectionToCell2InSection1 {
NSMutableArray * section1 = [self. arrayOfSections objectAtIndex: 0];
NSString * cell1InSection1 = [section1 objectAtIndex: 0];
[Section1 removeObject: cell1InSection1];
[Section1 insertObject: cell1InSection1 atIndex: 1];
NSIndexPath * sourceIndexPath = [NSIndexPath indexPathForRow: 0 inSection: 0];
NSIndexPath * destinationIdexPath = [NSIndexPath indexPathForRow: 1 inSection: 0];
[Self. myTableView moveRowAtIndexPath: sourceIndexPath toIndexPath: destinationIdexPath];
}
// Move the second Cell of the first Section to the position before the first Cell of the second Section
-(Void) moveCell2InSectionToCellInSection2 {
NSMutableArray * section1 = [arrayOfSections objectAtIndex: 0];
NSMutableArray * section2 = [arrayOfSections objectAtIndex: 1];

NSString * cell2InSection1 = [section1 objectAtIndex: 1];
[Section1 removeObject: cell2InSection1];
[Section2 insertObject: cell2InSection1 atIndex: 0];

NSIndexPath * sourceIndexPath = [NSIndexPath indexPathForRow: 1 inSection: 0];
NSIndexPath * destinationIndexPath = [NSIndexPath indexPathForRow: 0 inSection: 1];

[MyTableView moveRowAtIndexPath: sourceIndexPath toIndexPath: destinationIndexPath];
}
Running result:

 
 


Click the button on the right:

 
 


Click the button on the left to see the effect:

 


 


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.