美文网首页
UITableView简单的使用

UITableView简单的使用

作者: vanadia | 来源:发表于2016-08-31 01:13 被阅读0次

UITableView简单的使用

1.UITableViewDataSource 数据源方法

必须实现的方法

- (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;
}

创建cell并返回给controller

initWithStyle:一共有四种风格,分别是default,value1,value2,subtitle,具体样式可以自己去Xcode尝试。

reuseIdentifier:给创建的cell绑定一个标记,方便以后重调用。

几个可选方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

返回sections数量,如果不调用,默认返回1;

- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;

根据传入的identifier返回cell。

UITableViewCell *cell = [UITableViewCell dequeueReusableCellWithIdentifier:@"cell"];

2.UITableViewDelegate 代理方法

几个简单方法

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;//根据indexPah.row返回的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;//根据section返回header高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;//根据section 返回footer高度

相关文章

网友评论

      本文标题:UITableView简单的使用

      本文链接:https://www.haomeiwen.com/subject/sksqettx.html