UITableView

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.frame = CGRectMake( 0, 0, 375, 667);
    
    UITableView *baseTableView = [[UITableView alloc]initWithFrame:CGRectMake( 0, 30, 375, 667) style:UITableViewStyleGrouped];
    
    baseTableView.delegate = self;
    baseTableView.dataSource = self;
    
    [self.view addSubview: baseTableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//设置有多少个Section 组 默认是1组
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

//设置每一个单元格的头信息
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    switch ( section) {
        case 0:
            return @"第一组";
            break;
        case 1:
            return @"第二组";
        case 2:
            return @"第三组";
            
        default:
            break;
    }
    
    return @"";
}

//设置每一个单元格的尾信息
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return @"hello!";
}

//点击某单元格之后响应的事件
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog( @"------ %li", [indexPath row] );
    return indexPath;
}

//设置每一组 有多少个单元格
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  30;
}

//绘制Cell 一般为绘制单元格的数据 如 图片 文字 等
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //定义一个单元格在缓存里的别名
    static NSString *identifer = @"findCell";
    
    //在tableview找可复用的单元格
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: identifer];
    
    if( cell == nil ){
        //把某个单元格存入缓存 并用别名存放 方便查找
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: identifer];
    }
    
    cell.frame = CGRectMake( 0, ([indexPath row]+1) * 300, 375, 100);
    
    //单元格索引 从0开始
    NSInteger row = [indexPath row];
    //设置主标题文字信息
    cell.textLabel.text =[NSString stringWithFormat: @"这是第%li行",row+1];
    //设置子标题文字信息
    cell.detailTextLabel.text= @"没有什么大惊小怪~";
    //设置子标题字体、字体大小
    cell.detailTextLabel.font = [UIFont fontWithName:@"Arial" size:10];

    //选中的样式
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    
    //设置图片信息
    cell.imageView.image = [UIImage imageNamed:@"3.png"];
    
    //cell.accessoryType = UITableViewCellAccessoryNone;//cell没有任何的样式
    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//cell的右边有一个小箭头,距离右边有十几像素;
    //cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//cell右边有一个蓝色的圆形button;
    //cell.accessoryType = UITableViewCellAccessoryCheckmark;//cell右边的形状是对号;
    cell.accessoryType = UITableViewCellEditingStyleDelete;
    
    return cell;
}

//设置每一个单元格的行高
- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
相关文章
相关标签/搜索