IOS开发复习笔记(4)-TableView

总结几个TableView常用的代码

1.初始化方面

static string CellIndetifier=@"cellIndetifier";
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
     return 1;//表中段数
}
-(NSInterger)tableView:(UITableVIew *)tableView numberOfRowsInSection:(NSInteger)section{
    retrun 0;//每个段中的单元格数量
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:cellIdentifier];
    }
   cell.textLabel.text=@”showNSString”;//表中文字内容
    UIImage *image=[UIImage imageNamed:@”picname.png”];
   cell.imageView.image=image;//添加图片
  cell.textLabel.font=[UIFont boldSystemFontOfSize:10];//设置字体大小
    // Configure the cell.
    return cell;
} //配置单元格

通过委托为每行的列表设置高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   // return 70;//简单的操作
  //return indexPath.row//为每行设置不同高度
}

在TableViewCell中添加子视图如Lable,继承UITableViewCell类

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

2.tableviewcell的样式有很那些呢:

UITableViewCellDefault样式:左边一个显示图片的imageView,一个标题textLabel,没有detailTextLabel。

UITableViewCellStyleSubtitle:左边一个显示图片的imageView,上边一个主标题textLabel,一个副标题detailTextLabel。主标题字体大且加黑,副标题字体小在主标题下边。

UITableViewCellStyleValue1:左边一个显示图片的imageView,左边一个主标题textLabel,右边一个副标题detailTextLabel,主标题字体比较黑。

UITableViewCellStyleValue2:左边一个主标题textLabel字体偏小,挨着右边一个副标题detailTextLabel,字体大且加黑。

通过委托设置缩进的级别,关键词indentation 为缩进,缩排的意思

-(NSInteger) tableView:(UITableView *)tableView indentationLevelForAtIndexPath:(NSIndexPath *)indexPath
{
    return num;//num是你要缩进的级别
}

3.tableViewCell的选择处理

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    return indexPath;//返回选择的行数,由于是will所以是发生在选择行之前,可以增加一些如高亮操作,或者无法选中的功能
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//在选中该行后进行的操作,可以使用indexPath.row作为索引进行取数
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
//取消该行的操作,比如选了其他行,注意didselect和didDeselect区别,初学者可能经常没注意到
}

4.使用自定义的Cell类

UITableView *tableView=(id)[self.view viewWithTag:1];//话说这是另一种连接的方法?
[tableView regsiterClass:[BIDNameCell class] forCellReuseIndentifier:@"cellresusename"]
//BIDNameCell为自定义的Cell类,tableView是你声明的表视图

5.