TableView基本使用

来源:互联网 发布:js判断map是否包含key 编辑:程序博客网 时间:2024/06/10 02:49

TableView基本使用

基本步奏

  • 1设置数据源
    self.tableview.dataSource = self;
  • 2遵守协议
@interface ViewController () <UITableViewDataSource>@property (weak, nonatomic) IBOutlet UITableView *tableview;
  • 3实现方法
    有几组
    每组有几行
    每行设置什麽 内置了那些控件,自己点击command查找

/**
* 不是必须实现的方法
*
* @param tableView
*
* @return tabeleview多少分组
*/
“`objc
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}

/** *  必须实现的方法 * *  @param tableView *  @param section * *  @return 告诉tableview每个分组有多少行 */ ```objc- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    if (section == 0) {        return 2;    }    else if (section == 1)    {        return 3;    }    else    {        return 4;    }}

/**
* 对分组每行的内容进行设置
*
* @param tableView
* @param indexPath
*
* @return (UITableViewCell *)
*/

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell * cell = [[UITableViewCell alloc] init];    if (indexPath.row == 0) {        cell.textLabel.text = @"通用";    }    else if(indexPath.row == 1)    {        cell.textLabel.text = @"隐私";    }    else if (indexPath.row == 2)    {        cell.textLabel.text = @"必须";    }    else    {        cell.textLabel.text = @"得更新";    }    return cell;}

效果图:
运行效果

出现的问题图:
这里写图片描述
这里写图片描述
这里写图片描述

0 0
原创粉丝点击