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)