自定義UITableViewCell

一、建立xib代理

二、建立一個繼承UITableViewCell的控制器code

三、xib和控制器綁定起來。blog

四、用 NSBundle加載繼承

五、設置自適應高度。it

#pragma mark 多少條
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 6;
}


#pragma mark 加載cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellID=@"blogcell";
    UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell==nil) {
        
        cell=[[[NSBundle mainBundle]loadNibNamed:@"BlogCell" owner:self options:nil] lastObject];
      
    }
    
    UILabel * label=[cell viewWithTag:2];
    label.text=@"大灰狼";
    
    return cell;


}

#pragma mark 自適應高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [self tableView:_tableView cellForRowAtIndexPath:indexPath];
    
    return cell.frame.size.height;
}


最後前面別忘記引入UITableViewDataSourceDelegate和UITableViewDelegate 並設置代理io

相關文章
相關標籤/搜索