有时,需要创建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;
}