Andy-original breeze. For more information, see. Thank you.
1. UITableView Initialization
UITableView tableview = [[UITableView alloc] initWithFrame: CGRectMake (0, 0,320,420)];
[Tableview setDelegate: self];
[Tableview setDataSource: self];
[Self. view addSubview: tableview];
[Tableview release];
(1) The UITableView must be implemented during UITableView initialization. in the H file, you must inherit the UITableViewDelegate and UITableViewDataSource, implement the three UITableView data source methods, and set its delegate to self. This is not directly inherited from the UITableViewController implementation method.
(2) inherit the UITableViewController directly when XCODE generates a project. It will help you automatically write the required methods of UITableView.
(3) UITableView inherits from UIScrollView.
2. UITableView Data Source
(1) UITableView is a data source that relies on external resources to fill in content for the new table unit. This data source can provide table cells based on the index path. In UITableView, the index path is the object of NSIndexPath, you can select segments or branches, that is, section and row in our code.
(2) UITableView has three core methods that must be implemented:
-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView;
This method can display our data in segments or in a single list. As shown in the following figure, segments are displayed on the left and a single list is displayed on the right:
-(NSInteger) tableView :( UITableView *) tableViewnumberOfRowsInSection :( NSInteger) section;
This method returns the number of rows for each segment. You can use switch to return different rows for different segments. If it is a single list, you can directly return a single function you want.
-(UITableViewCell *) tableView :( UITableView *) tableViewcellForRowAtIndexPath :( NSIndexPath *) indexPath;
This method returns every cell we call. It is determined by the section and row of the index path.
3. Delegate method of UITableView
Delegation is used to respond to user interaction actions. For example, to update data from a drop-down list and select a row of cells, there is a lot of this method in UITableView for us to choose from.
(1) delegation Methods
// Set the number of sections
-(NSArray *) sectionIndexTitlesForTableView :( UITableView *) tableView {
Return TitleData;
}
// Set the Title displayed for each section
-(NSString *) tableView :( UITableView *) tableViewtitleForHeaderInSection :( NSInteger) section {
Return @ "Andy-Qingfeng ";
}
// Specify the number of partitions. The default value is 1.
-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {
Return 2;
}
// Specify the number of rows in each partition. The default value is 1.
-(NSInteger) tableView :( UITableView *) tableViewnumberOfRowsInSection :( NSInteger) section {
}
// Set the cell called by each row
-(UITableViewCell *) tableView :( UITableView *) tableViewcellForRowAtIndexPath :( NSIndexPath *) indexPath {
Static NSString * SimpleTableIdentifier = @ "SimpleTableIdentifier ";
UITableViewCell * cell = [tableViewdequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
If (cell = nil ){
Cell = [[[UITableViewCellalloc] initWithStyle: UITableViewCellStyleDefault
ReuseIdentifier: SimpleTableIdentifier] autoreler];
}
Cell. imageView. image = image; // The image when no cell is selected
Cell. imageView. highlightedImage = highlightImage; // The image after the cell is selected
Cell. text = @ "Andy-Qingfeng ";
Return cell;
}
// Set to indent UITableView rows
-(NSInteger) tableView :( UITableView *) tableViewindentationLevelForRowAtIndexPath :( NSIndexPath *) indexPath {
NSUInteger row = [indexPath row];
Return row;
}
// Set the height of each cell line
-(CGFloat) tableView :( UITableView *) tableViewheightForRowAtIndexPath :( NSIndexPath *) indexPath {
Return 40;
}
// Return the selected cell
NSIndexPath * ip = [NSIndexPath indexPathForRow: row inSection: section];
[TopicsTable selectRowAtIndexPath: ip animated: YESscrollPosition: UITableViewScrollPositionNone];
// Set the UITableView style
[TableView setSeparatorStyle: UITableViewCellSelectionStyleNone];
// Set the response event of the selected Cell
-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {
[TableView deselectRowAtIndexPath: indexPath animated: YES]; // The selected reversed color disappears immediately.
}
// Set the action performed by the selected row
-(NSIndexPath *) tableView :( UITableView *) tableViewwillSelectRowAtIndexPath :( NSIndexPath *) indexPath
{
NSUInteger row = [indexPath row];
Return indexPath;
}
// Set whether the del button is displayed in the active cell for processing in the deleted data.
-(BOOL) tableView :( UITableView *) tableView canEditRowAtIndexPath :( NSIndexPath *) indexPath {
}
// Set the editing status when deleting
-(Void) tableView :( UITableView *) tableView commitEditingStyle :( UITableViewCellEditingStyle) editingStyle
ForRowAtIndexPath :( NSIndexPath *) indexPath
{
}
// Add an index table to the right
-(NSArray *) sectionIndexTitlesForTableView :( UITableView *) tableView {
}
(2) Others
// Select the cell color. The following options are available in the official document:
Typedef enum {
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle
// Cell button format on the right
Typedef enum {
UITableViewCellAccessoryNone, // don't show any accessory view
UITableViewCellAccessoryDisclosureIndicator, // regular chevron. doesn't track
UITableViewCellAccessoryDetailDisclosureButton, // blue button w/chevron. tracks
UITableViewCellAccessoryCheckmark // checkmark. doesn' t track
} UITableViewCellAccessoryType
// Whether to add a line break
Typedef enum {
UITableViewCellSeparatorStyleNone,
UITableViewCellSeparatorStyleSingleLine
} UITableViewCellSeparatorStyle
// Change the line feed color
TableView. separatorColor = [UIColor blueColor];
4. UITableViewCell
Each row in the table represents a UITableViewCell. You can use images, text, and auxiliary icons to customize your own UITableViewCell. You can customize the following cell model or appstore model.
UITableViewCell provides three selectable attributes for each Cell, as shown below:
L textLabel: Enter the text
L detailTextLable: a slightly detailed subtitle
L imageView: displays the image of your cell. It can be loaded using UIImage.
Finally, I will give you an official demo to learn more. If you don't understand it, I will ask you some questions about the problems that will occur in the UITableView application in the next lesson, such as user-defined, reuse cells and sort data in cells. You are welcome to make a brick.
Additional code: http://www.bkjia.com/uploadfile/2011/1130/20111130025401502.zip