UITableView is a subclass of UIScrollView. Therefore, UITableView can respond to rolling events.
To use UITableView, You need to enable the Controller to implement the UITableViewDataSource protocol or let the Controller inherit the UITableViewController. It has implemented the UITableViewDataSource and UITableViewDelegate proxy protocols.
Using UITableViewDataSource "required methods" is commonly known as configuring data sources.
// How many rows are displayed in each group
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section
And
// What is displayed for each row? By default, when the simulator is started, only the visible cell content data is displayed. When a new cell enters the field of view, it is called to create a new Cell, which is automatically released when it is invisible.
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath
1. Associate the tableView data source with the controller during loading.
-(Void) viewDidLoad
{
[SuperviewDidLoad];
Self. tableView. dataSource = self;
}
Optional UITableViewDataSource
// If you want to change tableView, there are several groups to implement the following method:
-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView; // only one group is supported by default.
// What title does the section group display?
-(NSString *) tableView :( UITableView *) tableViewtitleForHeaderInSection :( NSInteger) section;
// What is the ending title of the section group?
-(NSString *) tableView :( UITableView *) tableViewtitleForFooterInSection :( NSInteger) section;
// Partial refresh table reloadRowsAtIndexPaths
NSIndexPath * path = [NSIndexPathindexPathForRow: row inSection: 0];
[Self. tableViewreloadRowsAtIndexPaths: @ [path] withRowAnimation: UITableViewRowAnimationBottom];
// Reload data. This method can be used to load more data.
[Self. tableViewreloadData];
// Call the above method
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath
Method to assign a value again
// Return the string data displayed in the index bar on the right
-(NSArray *) sectionIndexTitlesForTableView :( UITableView *) tableView;
UITableView proxy protocol UITableViewDelegate generally processes tableView events. In the proxy protocol, UITableViewDelegate is used.
It needs to be set during loading
-(Void) viewDidLoad
{
Self. tableView. delegate = self;
}
// Set the height of each row
-(CGFloat) tableView :( UITableView *) tableViewheightForRowAtIndexPath :( NSIndexPath *) indexPath;
// When a row is selected
-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath
{
// Obtain the row to be selected
NSInteger * rowx = indexPath. row;
}
// Triggered when the table is dragged.
-(Void) scrollViewWillBeginDragging :( UIScrollView *) scrollView;
// View displayed in each group of headers
-(UIView *) tableView :( UITableView *) tableView viewForHeaderInSection :( NSInteger) section
UITableView common attributes
// Add a control to the table header.
Self. tableView. tableHeaderView = [UIButtonbuttonWithType: UIButtonTypeContactAdd];
// Add a control at the end of the table
Self. tableView. tableFooterView = [[UISwitchalloc] init];
// Set the Row Height of a table.
Self. tableView. rowHeight = 60;
// Set the Row Height of each group of headers
Self. tableView. sectionHeaderHeight = 44;
// Set the background view of the table
Self. tableView. backgroundView = nil;
// Set the background color of the table.
Self. tableView. backgroundColor = [UIColorcolorWithRed: 0.9 green: 0.9 blue: 0.9 alpha: 1];
// The table cannot be selected.
Self. tableView. allowsSelection = NO;
UITableView Optimization
Methods To be optimized
// This method is called whenever a cell enters the field of view, when the cell is invisible, it is automatically released. Therefore, there is always only one cell on the screen, so we need to reuse the memory of the cell as follows:
-(UITableViewCell *) tableView :( UITableView *) tableViewcellForRowAtIndexPath :( NSIndexPath *) indexPath
{
StaticNSString * ID = @ "hero ";
// 1. Search for reusable cells in the cache pool with a single identifier
// Dequeue: column (Search)
UITableViewCell * cell = [tableViewdequeueReusableCellWithIdentifier: ID];
// 2. If no cell can be recycled
If (cell = nil ){
Cell = [[UITableViewCellalloc] initWithStyle: UITableViewCellStyleSubtitlereuseIdentifier: ID];
// NSLog (@ "------ cell -- % d", indexPath. row cannot be found in the cache pool );
}
// 3. Set new data for the cell
// Retrieve the Model
Hero * hero = self. heros [indexPath. row];
// Set the cell data
Cell. textLabel. text = hero. name;
Cell. detailTextLabel. text = hero. intro;
Cell. imageView. image = [UIImageimageNamed: hero. icon];
Return cell;
}
2. Using the default UItableViewCell will greatly affect the performance. The strange thing is that using a custom View instead of a predefined UItableViewCell will be faster.
UITableView static Cell
By default, we drag a dynamic Cell from the design interface.
The static Cell can be dragged directly on the cell to customize the cell.