1 Preface
If we are not satisfied with the ancillary view provided by the ios sdk, We can customize it and indent it.
2. code example
ZYUITableViewController. h:
[Plain]
# Import <UIKit/UIKit. h>
@ Interface ZYUITableViewController: UIViewController <UITableViewDelegate, UITableViewDataSource> // Add a proxy
@ Property (nonatomic, strong) UITableView * myTableView;
@ End
# Import <UIKit/UIKit. h>
@ Interface ZYUITableViewController: UIViewController <UITableViewDelegate, UITableViewDataSource> // Add a proxy
@ Property (nonatomic, strong) UITableView * myTableView;
@ End
ZYUITableViewController. m:
[Plain] view plaincopyprint? @ Synthesize myTableView;
-(Id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) nibBundleOrNil
{
Self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil];
If (self ){
// Custom initialization
}
Return self;
}
-(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;
//}
// The default value is 1 Section.
// Sets the number of rows to be rendered.
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {
Return 10;
}
// 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: @ "Cell % ld", (long) indexPath. row];
UIButton * button = [UIButton buttonWithType: UIButtonTypeRoundedRect]; // The initialization button
Button. frame = CGRectMake (0.0f, 0.0f, 150366f, 25366f); // set the size.
[Button setTitle: [NSString stringWithFormat: @ "Expand % ld", (long) indexPath. row] forState: UIControlStateNormal]; // set the button title and normal style
[Button addTarget: self action: @ selector (performExpand :) forControlEvents: UIControlEventTouchUpInside]; // Add a click event
Result. accessoryView = button;
Result. indentationLevel = indexPath. row; // indent level
Result. indentationWidth = 10.0f; // indent width
}
Return result;
}
-(Void) performExpand :( UIButton *) param {
NSLog (@ "U pressed % @", param. titleLabel. text); // obtain the button title
}
// Events triggered when a row is clicked
-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {
If ([tableView isEqual: myTableView]) {
NSLog (@ "% @", [NSString stringWithFormat: @ "Cell % ld is selected", (long) indexPath. row]);
}
}
// Events triggered when you click the button on the right
-(Void) tableView :( UITableView *) tableView accessoryButtonTappedForRowWithIndexPath :( NSIndexPath *) indexPath {
UITableViewCell * owenCell = [tableView cellForRowAtIndexPath: indexPath];
NSLog (@ "Cell Title = % @", owenCell. textLabel. text); // obtain the value of textLabel
}
@ Synthesize myTableView;
-(Id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) nibBundleOrNil
{
Self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil];
If (self ){
// Custom initialization
}
Return self;
}
-(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;
//}
// The default value is 1 Section.
// Sets the number of rows to be rendered.
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {
Return 10;
}
// 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: @ "Cell % ld", (long) indexPath. row];
UIButton * button = [UIButton buttonWithType: UIButtonTypeRoundedRect]; // The initialization button
Button. frame = CGRectMake (0.0f, 0.0f, 150366f, 25366f); // set the size.
[Button setTitle: [NSString stringWithFormat: @ "Expand % ld", (long) indexPath. row] forState: UIControlStateNormal]; // set the button title and normal style
[Button addTarget: self action: @ selector (performExpand :) forControlEvents: UIControlEventTouchUpInside]; // Add a click event
Result. accessoryView = button;
Result. indentationLevel = indexPath. row; // indent level
Result. indentationWidth = 10.0f; // indent width
}
Return result;
}
-(Void) performExpand :( UIButton *) param {
NSLog (@ "U pressed % @", param. titleLabel. text); // obtain the button title
}
// Events triggered when a row is clicked
-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {
If ([tableView isEqual: myTableView]) {
NSLog (@ "% @", [NSString stringWithFormat: @ "Cell % ld is selected", (long) indexPath. row]);
}
}
// Events triggered when you click the button on the right
-(Void) tableView :( UITableView *) tableView accessoryButtonTappedForRowWithIndexPath :( NSIndexPath *) indexPath {
UITableViewCell * owenCell = [tableView cellForRowAtIndexPath: indexPath];
NSLog (@ "Cell Title = % @", owenCell. textLabel. text); // obtain the value of textLabel
}
Running result:
Click console output at the first line:
10:56:21. 187 UITableViewTest1 [877: c07] Cell 0 is selected
Click the button on the second line to output the console:
10:56:23. 957 UITableViewTest1 [877: c07] U pressed Expand 1