1.如何利用UITableView展示數據
設置數據源對象(一般是控制器)
self.tableView.dataSource = self;
數據源對象需要遵守協議->UITableViewDataSource
@interface ViewController ()<UITableViewDataSource>
@end
實現數據源協議裡面的方法
//設置一共多少組
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
}
//設置每組多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section{
}
//設置每行顯示的內容(UITableView每行都是UITableViewCell)
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
}
//設置頭部顯示的標題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:
(NSInteger)section{
}
//設置尾部顯示的標題
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:
(NSInteger)section{
}
2.UITableView常見的設置
// 設置tableView每行cell的高度(默認是44)
self.tableView.rowHeight = 80;
// 設置tableView每一組頭部的高度
self.tableView.sectionHeaderHeight = 50;
// 設置tableView每一組尾部的高度
self.tableView.sectionFooterHeight = 50;
// 设置分割线的颜 ,如果设置[UIColor clearColor]隐藏分割线
self.tableView.separatorColor = [UIColor redColor];
// 设置分割线的样式
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// 设置表头
self.tableView.tableHeaderView = [[UISwitch alloc] init] ;
// 设置表尾
self.tableView.tableFooterView = [UIButton buttonWithType:UIButtonTypeContactAdd];
// 设置索引条上 字颜
self.tableView.sectionIndexColor = [UIColor redColor];
// 设置索引条的背景颜
self.tableView.sectionIndexBackgroundColor = [UIColor blackColor];
3.UITableViewCell的常見設置
cell.accessoryView = [[UISwitch alloc] init];
// 设置cell右边的指 样式(accessoryView优先级 > accessoryType)
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// 设置cell的背景view
// backgroundView优先级 > backgroundColor
UIView *bg = [[UIView alloc] init];
bg.backgroundColor = [UIColor blueColor];
cell.backgroundView = bg;
// 设置cell的背景颜
cell.backgroundColor = [UIColor redColor];
// 设置cell选中的背景view
UIView *selectbg = [[UIView alloc] init];
selectbg.backgroundColor = [UIColor purpleColor];
cell.selectedBackgroundView = selectbg;
// 设置cell选中的样式
cell.selectionStyle = UITableViewCellSelectionStyleNone;
4. UITableView代理方法
//監聲UITableViewCell
//當選中某一行cell就會調用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"didSelectRowAtIndexPath--選中第%ld組,第%ld行",indexPath.section,indexPath.row);
}
// 當取消選中某一行cell就會調用
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"didDeselectRowAtIndexPath--取消第%ld組,第%ld行",indexPath.section,indexPath.row);
}
//返回每組顯示的頭部控件
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [[UIView alloc]init ];
view.frame = CGRectMake(100, 100, 100, 100);
view.backgroundColor = [UIColor redColor];
return view;
}
//返回每一組的頭部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 200;
}
/**
* 返回每一组的尾部高度
*/
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//{
//
//}
/**
* 返回每一行cell的高度
*/
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
// if (indexPath.row % 2 == 0) {
// return 100;
// } else {
// return 50;
// }
//}
5. UITableView的性能優代
asfasdf fasdfnkasdf kjasdklfjalsfd asdf,nkjasdfn asdfasdf``
//監聲UITableViewCell
//當選中某一行cell就會調用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"didSelectRowAtIndexPath--選中第%ld組,第%ld行",indexPath.section,indexPath.row);
}
// 當取消選中某一行cell就會調用
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"didDeselectRowAtIndexPath--取消第%ld組,第%ld行",indexPath.section,indexPath.row);
}
//返回每組顯示的頭部控件
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [[UIView alloc]init ];
view.frame = CGRectMake(100, 100, 100, 100);
view.backgroundColor = [UIColor redColor];
return view;
}
//返回每一組的頭部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 200;
}
/**
* 返回每一组的尾部高度
*/
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//{
//
//}
/**
* 返回每一行cell的高度
*/
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
// if (indexPath.row % 2 == 0) {
// return 100;
// } else {
// return 50;
// }
//}
asjkhfkasjf nakjasdf
6. asdlkjalksjdg
nakjhadkjhgds
sadfkhjkjsdfg
sdklfhkajdsgh
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [[UIView alloc]init ];
view.frame = CGRectMake(100, 100, 100, 100);
view.backgroundColor = [UIColor redColor];
return view;
}
//返回每一組的頭部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 200;
}
/**
* 返回每一组的尾部高度
*/
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//{
//
//}
/**
* 返回每一行cell的高度
*/
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
// if (indexPath.row % 2 == 0) {
// return 100;
// } else {
// return 50;
// }