关于UITableView段落模式 — 段头上的button问题

有时,需要创建UITableView的段落模式,当在段落模式下,在段头或段尾上设置button,怎么判断点击的是那个button呢?

········


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    static NSString *cellHeaderIdentifier = @"headCell";
    UITableViewHeaderFooterView *cellHead = [tableViewdequeueReusableHeaderFooterViewWithIdentifier:cellHeaderIdentifier];
    if(!cellHead) {
        cellHead = [[UITableViewHeaderFooterViewalloc] initWithReuseIdentifier:cellHeaderIdentifier];
    }
    UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];
    [button setTag:section];
    [button setTitle:[NSStringstringWithFormat:@"%ld",(long)section]forState:UIControlStateNormal];
    [button addTarget:selfaction:@selector(buttonto:)forControlEvents:UIControlEventTouchUpInside];
    [button setFrame:CGRectMake((tableView.frame.size.width - 65), 10,50, 30)];
    [cellHead addSubview:button];
    [cellHead.textLabelsetText:[NSStringstringWithFormat:@"Section: %ld",(long)section]];
    return cellHead;
}

- (void)buttonto:(UIButton *)btn{
    DLog(@"%ld",(long)btn.tag);
}



当UITableView的段头用xib创建的时候,我自己创建的UITableViewHeaderFooterView的类,再创建xib文件关联,但是在

viewForHeaderInSection 代理方法中老是加载崩溃,最后,在网上看到有的人说用UItableViewCell创建段头,我加以改正之后,发现确实可以,三四个小时,终于成功了······

这是代码:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    static NSString *HeaderIdentifier = @"headCell";
    ActivityPubHeadCell *headCell = [tableView dequeueReusableCellWithIdentifier:HeaderIdentifier];
    if(!headCell) {
        headCell = [[[NSBundle mainBundle] loadNibNamed:@"ActivityPubHeadCell"
                                                  owner:self
                                                options:nil] objectAtIndex:0];
    }
    [headCell.label setText:[NSString stringWithFormat:@"Section: %ld",(long)section]];
    headCell.view.backgroundColor = [UIColor orangeColor];
    return headCell;
}


运行图如下:





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值