UITableView的簡單使用
直接先上一段簡單UITableView的使用代碼。
#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>//添加協議
{
//聲明tableView和數據源
UITableView *table;
NSMutableArray *dataSource;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self createDataSource];
[self createTableView];
}
//創建數據源
- (void)createDataSource{
//初始化數據源數組
dataSource = [NSMutableArray arrayWithCapacity:0];
//循環e給數據源中添加元素
for (int i = 0; i < 20; i++) {
NSString *string = [NSString stringWithFormat:@"第%d行", i+1];
[dataSource addObject:string];
}
//刷新視圖
[table reloadData];
}
//創建tableView視圖
- (void)createTableView{
//創建並設置位置大小和風格形式
/*
typedef NS_ENUM(NSInteger, UITableViewStyle) {
UITableViewStylePlain, // 無分組樣式
UITableViewStyleGrouped // 分組樣式
};
*/
table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
//設置代理
table.delegate = self;
table.dataSource = self;
//設置縱橫滑塊不顯示
table.showsVerticalScrollIndicator = NO;
table.showsHorizontalScrollIndicator = NO;
[self.view addSubview:table];
}
#define mark --UITableViewDelegate
//設置列表中每個元素的行高,在非動態行高的情況下設置固定值。動態行高的情況需要計算。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 64;
}
#define mark --UITableViewDataSource
/*
//在樣式爲UITableViewStyleGrouped(分組樣式)的時候需要實現下面的設置分組數量的代理方法,有幾個分組設置返回值爲幾。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
*/
//設置列表中元素的個數 一般設置爲數據源中元素的個數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return dataSource.count;
}
//創建tableView中的每一個cell,這裏使用複用機制。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.textLabel.text = dataSource[indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"點擊了第%d行", indexPath.row);
}
@end
UITableView介紹
UITableView的代理協議有兩個:UITableViewDelegate, UITableViewDataSource。
UITableView的樣式有兩種:UITableViewStylePlain(無分組樣式)、UITableViewStyleGrouped( 分組樣式)
UITableView是繼承自UIScrollView的,所以屬性在這裏就不多說了,重點說一下代理方法。
UITableView的代理方法:
UITableViewDataSource協議的常用代理方法:
1、設置分組數量:這個方法在設置樣式爲UITableViewStylePlain時不需要實現,默認爲1個分組
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
2、設置每組中列表數量:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
3、設置列表中每個元素(cell):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
4、設置分組的頭標題:
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
5、設置分組的尾標題:
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
若頭尾標題不同意需要根據section單獨去設置。
UITableViewDelegate協議的常用代理方法:
1、設置行高:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
2、設置分組的頭標題視圖高度:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
3、設置分組的尾標題視圖高度:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
4、自定義分組的頭視圖:
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
5、自定義分組的尾視圖:
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
6、點擊cell:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
UITableView的使用通常需要進行復雜的設置和複雜的計算,比如動態的cell高度,比如動態修改,比如動態插入,比如刪除和編輯。但是無論多複雜,都是在這些代理方法中進行設置,所以萬變不離其宗。