UITableView簡單的使用
1.UITableViewDataSource 數(shù)據(jù)源方法
必須實(shí)現(xiàn)的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return 1;
}
返回1行row冰更。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@“cell”];
return cell;
}
創(chuàng)建cell并返回給controller
initWithStyle:一共有四種風(fēng)格昂勒,分別是default,value1,value2,subtitle,具體樣式可以自己去Xcode嘗試。
reuseIdentifier:給創(chuàng)建的cell綁定一個(gè)標(biāo)記奠衔,方便以后重調(diào)用。
幾個(gè)可選方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
返回sections數(shù)量痊夭,如果不調(diào)用脏里,默認(rèn)返回1;
- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;
根據(jù)傳入的identifier返回cell。
UITableViewCell *cell = [UITableViewCell dequeueReusableCellWithIdentifier:@"cell"];
2.UITableViewDelegate 代理方法
幾個(gè)簡單方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;//根據(jù)indexPah.row返回的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;//根據(jù)section返回header高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;//根據(jù)section 返回footer高度