- How to make TableView show data
- Setting the data source object
self.tableView.dataSource = self;
- Data source object to comply with protocol
@interface ViewController () <UITableViewDataSource>@end
- Implementing a Data source method
// 多少组数据- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;// 每一组有多少行数据- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;// 每一行显示什么内容- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;// 每一组的头部- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;// 每一组的尾部- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
Common settings for TableView
Set the height of each row of cells
Self.tableView.rowHeight = 100;
Set the height of each group of headers
Self.tableView.sectionHeaderHeight = 50;
Set the height of each set of tails
Self.tableView.sectionFooterHeight = 50; --each line of the > set is fixed
Set the split Line color
Self.tableView.separatorColor = [UicolorRedcolor];
Set the split line style
Self.tableView.separatorStyle= Uitableviewcellseparatorstylenone;
Setting the header control
Self.tableView.tableHeaderView= [[Uiswitch alloc] init];
Set footer controls
Self.tableView.tableFooterView = [UIButtonButtonwithtype:uibuttontypecontactadd];
Set the color of the index text on the right
Self.tableView.sectionIndexColor = [UicolorRedcolor];
Set the background color of the index text on the right
Self.tableView.sectionIndexBackgroundColor = [UicolorBlackcolor];
Common settings for Tableviewcell
Set the indicator style to the right
Cell. accessorytype= Uitableviewcellaccessorydisclosureindicator;
Set the indicator control to the right
Cell. Accessoryview= [[Uiswitch alloc] init];
Set the cell's selected style
Cell. Selectionstyle= Uitableviewcellselectionstylenone;
Backgroundview Priority > BackgroundColor
Set Background color
Cell. backgroundcolor = [UicolorRedcolor];
//Set background view--(customizable background image)
UIView*BG = [[UIViewalloc] init];
BG. backgroundcolor = [UicolorBluecolor];
Cell. Backgroundview= BG;
Set the selected background view
UIView*selectedbg = [[UIViewALLOC] init];
selectedbg. backgroundcolor = [UicolorPurplecolor];
Cell. Selectedbackgroundview= selectedbg;
Proxy method
/**
* Called when a row is selected (click)
*/
-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath
{
Xmgwine *wine = Self.winearray[indexpath.row];
NSLog (@ "clicked:%@", wine.name);
NSLog (@ "Selected:%zd", Indexpath.row);
}
/**
* Called when a row is unchecked
*/
-(void) TableView: (UITableView *) TableView Diddeselectrowatindexpath: (Nsindexpath *) Indexpath
{
NSLog (@ "Unchecked:%zd", Indexpath.row);
}/** * In each set of header settings control */-(UIView *) TableView: (UITableView *) TableView viewforheaderinsection: (nsinteger) Section
{
return [UIButton Buttonwithtype:uibuttontypeinfodark];
}/** * Set Control */-(UIView *) at the tail of each group TableView: (UITableView *) TableView viewforfooterinsection: (nsinteger) Section
{
return [[Uiswitch alloc] init];
}/** * can be judged by different groups setting different heights */-(cgfloat) TableView: (UITableView *) TableView heightforheaderinsection: (nsinteger) Section
{
if (section = = 0) return 20;
if (section = = 1) return 50;} /** * Returns the height of each cell (set different heights) */
-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath
{
if (indexpath.row% 2 = = 0) {
return 50;
} else {
return 100;
}}
Knowledge Point Review-tableview display data common settings