簡體   English   中英

UITableview邊框隱藏部分單元格

[英]UITableview border hide section cell

我正在嘗試使用border,section header和cell創建UITableview。 我正在使用xib文件。 使用xib文件創建UITableview和單元格,其中“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邊框但是有完整的節頭和單元格,有誰知道如何實現它?

你可以嘗試偽造你想要的行為。 除了使用邊界,你將不得不使用UIView上的所有四個角加UITableViewCell與每個相應的高度/寬度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上的邊框

你的borderWidth太多你要添加的是你在Cell中設置個人資料圖片你應該設置個人資料圖片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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM