【问题标题】:UITableview border hide section cellUITableview 边框隐藏部分单元格
【发布时间】:2017-04-10 06:09:36
【问题描述】:

我正在尝试创建带有边框、节标题和单元格的 UITableview。我正在使用 xib 文件。 UITableview 和使用 xib 文件创建的单元格,其中“viewForHeaderInSection”用于节标题,但是当我尝试将边框设置为 UITableview 时,它隐藏了节和单元格。

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

self.tableView.delegate = (id) self;
self.tableView.dataSource = (id) self;
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
self.tableView.layer.borderWidth = 20.0;
self.tableView.layer.borderColor = [UIColor colorWithRed:204/255.0 green:204/255.0 blue:204/255.0 alpha:1.0f].CGColor;

}

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

NSDictionary *dict = [[NSDictionary alloc] initWithDictionary:[self.array objectAtIndex:section] copyItems:YES];

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 5, tableView.frame.size.width, 50)];
view.backgroundColor = [UIColor colorWithRed:81/255.0 green:190/255.0 blue: 168/255.0 alpha:1.0f];

UIView *seperatoreView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 10)];
seperatoreView.backgroundColor = [UIColor colorWithRed:204/255.0 green:204/255.0 blue:204/255.0 alpha:1.0f];

[view addSubview:seperatoreView];

UILabel *numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
numberLabel.backgroundColor = [UIColor clearColor];
numberLabel.text = [NSString stringWithFormat:@"%@",[dict objectForKey:@"key"]];
numberLabel.textColor = [UIColor whiteColor];
[numberLabel setFont:[UIFont boldSystemFontOfSize:17.0f]];

[view addSubview:numberLabel];

NSDateFormatter *formate = [[NSDateFormatter alloc] init];
[formate setDateFormat:@"yyyy-MM-dd"];
NSDate *SDate = [formate dateFromString:[dict objectForKey:@"sDateKey"]];
NSDate *EDate = [formate dateFromString:[dict objectForKey:@"eDateKey"]];

[formate setDateFormat:@"MMM d"];
NSString *StartDateStr = [formate stringFromDate:SDate];
NSString *EndDateStr = [formate stringFromDate:EDate];

if([StartDateStr isEqual:[NSNull null]]){
    StartDateStr = @"";

}
if([EndDateStr isEqual:[NSNull null]]){

    EndDateStr = @"";
}

UILabel *weekDateLabel = [[UILabel alloc] initWithFrame:CGRectMake(view.frame.size.width-140, 10, 150, 30)];
weekDateLabel.backgroundColor = [UIColor clearColor];
weekDateLabel.text = [NSString stringWithFormat:@"%@ - %@",StartDateStr,EndDateStr];
weekDateLabel.textColor = [UIColor whiteColor];
[weekDateLabel setFont:[UIFont boldSystemFontOfSize:17.0f]];

[view addSubview:weekDateLabel];

return view;
}

我想要 UITableview 边框,但有完整的部分标题和单元格,有人知道如何实现吗?

【问题讨论】:

  • 你能分享你的代码吗?
  • 做一件事。我不知道这是正确的答案。但只是尝试一下。增加边框高度的大小并在部分标题的底部设置标签。我们可以自定义节标题。有一种委托方法。
  • 您可以再做一件事来避免这个问题。根据您的边框宽度将 tableView 的边框颜色设置为背景颜色或 tableView 的 parentView 和左填充。这样就可以避免这个问题。
  • 您将边框宽度设置为 20 px,这会隐藏高度为 50 px 的节标题的顶部区域。实际上,您将看到部分标题的 30 像素。您应该增加部分高度并为子视图添加高度偏移 20 px 或减小边框宽度以查看您放置的子视图

标签: ios objective-c uitableview tableviewcell uitableviewsectionheader


【解决方案1】:

您可以尝试伪造您想要的行为。您必须使用在UITableViewCell 的所有四个角上添加的UIView,而不是使用边框,每个UIView 具有适当的高度/宽度和灰色作为背景颜色。现在在您的cellForRow 中,您必须检查单元格的位置,并根据该位置隐藏/取消隐藏顶部和底部UIView(左侧和右侧UIView 将始终可见)。像这样的:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    .....
    if(indexPath.row == 0){

        [cell.topBorderView setHidden: NO];
        [cell.bottomBorderView setHidden: YES];

    }else if(indexPath.row == dataSourceArray.count - 1){

        [cell.topBorderView setHidden: YES];
        [cell.bottomBorderView setHidden: NO];

    }else{

        [cell.topBorderView setHidden: YES];
        [cell.bottomBorderView setHidden: YES];
    }

    return cell;
} 

当然还有去掉整个UITableView的边框

【讨论】:

    【解决方案2】:

    您的borderWidth 太多了您必须添加的是 用于单元格中的个人资料图片 您应该将配置文件图片 imageView 设置为左​​边框宽度的增量。

    let margin_left =  BorderWidth  + space_from_left. 
    ProfileImageView.frame = CGRect(x: margin_left,y:Y,width:yourRequiredWidth,  height:requiredHeight)
    

    与节标题标签相同

     let margin_left =  BorderWidth  + space_from_left. 
        headerLabel.frame = CGRect(x: margin_left,y:Y, width:yourRequiredWidth,  height:requiredHeight)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-10
      • 1970-01-01
      • 1970-01-01
      • 2014-12-13
      • 1970-01-01
      相关资源
      最近更新 更多