UITableView Control for IOS development (23)

Source: Internet
Author: User

1 Preface
UITableVIew is divided into different rolling views, each part is divided into different rows, you can also create custom TableVIew rows. UITableView implements vertical scrolling of UIScrollView. You can set the height and number of rows of each row and the content of each row.

2. code example
ZYViewController. h:


[Plain]
# Import <UIKit/UIKit. h>
 
@ Interface ZYViewController: UIViewController <UITableViewDelegate, UITableViewDataSource> // Add a proxy
 
@ Property (nonatomic, strong) UITableView * myTableView;
 
@ End

# Import <UIKit/UIKit. h>

@ Interface ZYViewController: UIViewController <UITableViewDelegate, UITableViewDataSource> // Add a proxy

@ Property (nonatomic, strong) UITableView * myTableView;

@ End
ZYViewController. m:

 

[Plain]
@ Synthesize myTableView;
 
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Self. view. backgroundColor = [UIColor whiteColor];
MyTableView = [[UITableView alloc] initWithFrame: self. view. bounds style: UITableViewStylePlain]; // you can specify UITableViewStyleGrouped as the group mode.
Self. myTableView. delegate = self; // set the proxy to itself
MyTableView. dataSource = self; // set the data source to itself
Self. myTableView. autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; // ensure that the TablView can adjust the size correctly.
[Self. view addSubview: myTableView];

}
// Set the height of each row
-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {
CGFloat result = 20366f;
If ([tableView isEqual: self. myTableView]) {
// Result = 40366f;
Result = 80366f;
}
Return result;
}
// Allow the data source to indicate the number of sections of the Table that must be loaded to Table View.
-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {
NSInteger result = 0;
If ([tableView isEqual: myTableView]) {
Result = 3; // There are three sections in total
}
Return result;
}
// Set the number of rows displayed for each Section
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {
NSInteger result = 0;
If ([tableView isEqual: myTableView]) {
Switch (section ){
// If it is the first section, three rows of data are displayed.
Case 0:
Result = 3;
Break;
Case 1:
Result = 5;
Break;
Case 2:
Result = 8;
Break;
}
}
Return result;
}
// Data in each row
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {
UITableViewCell * result = nil;
If ([tableView isEqual: myTableView]) {
Static NSString * tableViewCellIdentifier = @ "MyCells"; // you can specify the Cell id.
Result = [tableView dequeueReusableCellWithIdentifier: tableViewCellIdentifier]; // return a reusable table view cell object through the identifier
If (result = nil ){
Result = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: tableViewCellIdentifier]; // initialize a table cell style and reused identifier, and return it to the caller.
}
// IndexPath. section indicates the index indexPath. row of the section indicates the index of the number of rows.
Result. textLabel. text = [NSString stringWithFormat: @ "Section % ld, Cell % ld", (long) indexPath. section, (long) indexPath. row];
}
Return result;
}

@ Synthesize myTableView;

-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Self. view. backgroundColor = [UIColor whiteColor];
MyTableView = [[UITableView alloc] initWithFrame: self. view. bounds style: UITableViewStylePlain]; // you can specify UITableViewStyleGrouped as the group mode.
Self. myTableView. delegate = self; // set the proxy to itself
MyTableView. dataSource = self; // set the data source to itself
Self. myTableView. autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; // ensure that the TablView can adjust the size correctly.
[Self. view addSubview: myTableView];

}
// Set the height of each row
-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {
CGFloat result = 20366f;
If ([tableView isEqual: self. myTableView]) {
// Result = 40366f;
Result = 80366f;
}
Return result;
}
// Allow the data source to indicate the number of sections of the Table that must be loaded to Table View.
-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {
NSInteger result = 0;
If ([tableView isEqual: myTableView]) {
Result = 3; // There are three sections in total
}
Return result;
}
// Set the number of rows displayed for each Section
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {
NSInteger result = 0;
If ([tableView isEqual: myTableView]) {
Switch (section ){
// If it is the first section, three rows of data are displayed.
Case 0:
Result = 3;
Break;
Case 1:
Result = 5;
Break;
Case 2:
Result = 8;
Break;
}
}
Return result;
}
// Data in each row
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {
UITableViewCell * result = nil;
If ([tableView isEqual: myTableView]) {
Static NSString * tableViewCellIdentifier = @ "MyCells"; // you can specify the Cell id.
Result = [tableView dequeueReusableCellWithIdentifier: tableViewCellIdentifier]; // return a reusable table view cell object through the identifier
If (result = nil ){
Result = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: tableViewCellIdentifier]; // initialize a table cell style and reused identifier, and return it to the caller.
}
// IndexPath. section indicates the index indexPath. row of the section indicates the index of the number of rows.
Result. textLabel. text = [NSString stringWithFormat: @ "Section % ld, Cell % ld", (long) indexPath. section, (long) indexPath. row];
}
Return result;
}

 

[Plain]
// Events triggered when a row is clicked
-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {
If ([tableView isEqual: myTableView]) {
NSLog (@ "% @", [NSString stringWithFormat: @ "Cell % ld in Section % ld is selected", (long) indexPath. row, (long) indexPath. section]);
}
}

// Events triggered when a row is clicked
-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {
If ([tableView isEqual: myTableView]) {
NSLog (@ "% @", [NSString stringWithFormat: @ "Cell % ld in Section % ld is selected", (long) indexPath. row, (long) indexPath. section]);
}
}
Running result:

 
 


Click the result displayed on the console when a row is clicked:


06:46:28. 021 UITableViewTest1 [425: c07] Cell 4 in Section 1 is selected

06:46:28. 869 UITableViewTest1 [425: c07] Cell 0 in Section 2 is selected

06:46:31. 012 UITableViewTest1 [425: c07] Cell 1 in Section 2 is selected

 

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.