在使用静态单元格的时候自定义header/footer view

来源:互联网 发布:怎么下载photoshop软件 编辑:程序博客网 时间:2024/06/05 17:43
今天在storyboard上创建静态单元格制作界面的时候遇到了一些小问题,一些注意点记录一下:

1、数据源/代理方法中也需要放回组数/行数才能显示你在sb上自定义的静态单元格。

2、数据源/代理方法中放回的组数/行数不能超过在sb上定义的静态单元格的组数和行数。

3、想要自定义Header/Footer View的话:
 直接在sb上设置header/footer view的内容,如下图,如果没有设置的话,代理方法
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
是不会被调用的。

在这个方法中采用以下方式创建的话,sb中设置的title会直接设置给创建的header view的textLabel,并且用代码重新设置的话不起作用。但是别的属性可以重新设置。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    staticNSString *ID =@"header";
   
UITableViewHeaderFooterView *header = [tableViewdequeueReusableHeaderFooterViewWithIdentifier:ID];
   
if (header ==nil) {
        header = [[
UITableViewHeaderFooterViewalloc]initWithReuseIdentifier:ID];
        header.
textLabel.text = @"啦啦啦啦啦";//这个设置不起作用
        header.
detailTextLabel.text = @"细节细节细节细节细节";//这个设置完看起来也有问题
        header.backgroundView = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"img"]];//可以设置背景图片
    }
    return header;
}

要自定义header view的话,使用以下方法:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
   
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];//这边可以获得在sb中设置的header的内容
    if (sectionTitle ==nil) {
       
returnnil;
    }
   
   
UIButton *btn = [[UIButtonalloc]init];
    [btn
setBackgroundImage:[UIImageimageNamed:@"img"]forState:UIControlStateNormal];
    [btn
setTitle:sectionTitleforState:UIControlStateNormal];
    btn.
frame =CGRectMake(20,0,302,64);
   
   
UIView *view = [[UIViewalloc]init];
    [view
addSubview:btn];
   
   
return view;
}
0 0
原创粉丝点击