UITableView
- 什么是UITableView
- UITableView的作用
- 如何使用UITableView
- 設置
<UITableViewDataSource>
- 設置一共幾組數據
- 每組數據多少行
- 每行顯示的內
- 設置
實現有那些方式:
方式一代碼:
#import "ViewController.h"
# UITableView
* 什么是UITableView
* UITableView的作用
* 如何使用UITableView
1. 設置當前控制器成為數據源
2. 設置<UITableViewDataSource>
3. 設置一共幾組數據
4. 每組數據多少行
5. 每行顯示的內
* 實現有那些方式:
#### 方式一代碼:
@interface ViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//UIVTableView
/**
tableView 有兩種樣式:(設置tableView的樣式,要在創建一刻決定Plain或Grouped)
1.UITableViewStylePlain
2.UITableViewStyleGrouped
**/
//設置當前控制器成為數據源
self.tableView.dataSource = self;
}
#pragma mark -
#pragma mark - UITableViewDataSource
//1. 告訴tableView一共幾組數據
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 4;
}
//2. 每組數據多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section == 0) {
return 2;
} else if (section == 1){
return 6;
} else if (section == 2){
return 6;
}else {
return 1;
}
}
//3. tableView每行顯示的內容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//indexPath.section 第幾組
//indexPath.row 第幾行
UITableViewCell *cell = [[UITableViewCell alloc] init];
if (indexPath.section == 0) {
if (indexPath.row == 0) {
cell.textLabel.text = @"通用";
}else if (indexPath.row == 1){
cell.textLabel.text = @"稳私";
}
}else if (indexPath.section == 1){
if (indexPath.row == 0) {
cell.textLabel.text = @"facebook";
}else if (indexPath.row == 1){
cell.textLabel.text = @"google";
}else if (indexPath.row == 2){
cell.textLabel.text = @"baidu";
}
} else {
cell.textLabel.text = [NSString stringWithFormat:@"第%ld組,第%ld行",indexPath.section,indexPath.row];
}
return cell;
}
@end