1

我正尝试用边框,部分页眉和单元格创建UITableview。我正在使用xib文件。 UITableview和使用xib文件创建的单元格,其中“viewForHeaderInSection”用于区域标题,但是当我尝试将边框设置为UITableview时,它隐藏了区域和单元格。 enter image description hereUITableview边框隐藏部分单元格

- (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的边框,但与全断面头部和单元,难道有谁知道如何实现的呢?

+0

能否请您分享您的代码? – Kampai

+0

做一件事。我不知道这是正确的答案。但试试吧。增加边框高度的大小并在章节标题底部设置标签。我们可以自定义节标题。一个代表方法就在那里。 – phani

+0

你可以做更多的事情来避免这个问题。将tableView的边框颜色设置为背景颜色或tableView的父视图,并根据边框宽度填充左边距。这样,你可以避免这个问题。 – Surjeet

回答

0

你可以尝试和假冒你想要的行为。除了使用边框,您还必须在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

0

你的边框宽度边框实在是太多了,你有什么要补充的是 在细胞档案图片 你应该设置个人资料图片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)