UITableView简单的使用

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高度
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容