[英]UITableView - gap between main header and section header
在UITableView
上工作,在主tableview和每個部分中都有標題。 但是,我們在這兩個標題之間出現了一個奇怪的差距。 我們已經嘗試了其他SO答案中給出的解決方案,例如:
self.edgesForExtendedLayout
tableView.contentInset
self.automaticallyAdjustsScrollViewInsets = false
但是沒有運氣!
我整理了發生的情況的屏幕截圖:
問題是紅色和綠色標題之間的差距,任何幫助,我們都非常感謝,如果需要的話,很樂意分享更多代碼片段。 突出顯示的區域在視圖調試器中匹配,因此我已經確認這不是在任何地方添加額外填充的問題。
一些相關的代碼:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionHeader = ShareScreenSectionHeaderView()
let title = viewData.sections[section].sectionTitle
let subtitle = viewData.sections[section].sectionSubtitle
sectionHeader.update(title: title, subtitle: subtitle)
return sectionHeader
}
為表格視圖設置的標題是UIView
,使用tableHeaderView
屬性設置
這是代碼片段
@property (weak, nonatomic) IBOutlet UITableView *tblLoad;
@property (strong, nonatomic) IBOutlet UIView *lbltblHeader;
- (void)viewDidLoad {
[super viewDidLoad];
self.tblLoad.tableHeaderView = self.lbltblHeader;
self.tblLoad.delegate = self;
self.tblLoad.dataSource= self;
[self.tblLoad reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section ==0) {
return 5;
}
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *v =[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 20)];
v.backgroundColor = UIColor.greenColor;
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 200, 30) ];
lbl.text = [NSString stringWithFormat:@"I am the %ld Section ",section];
lbl.textColor = UIColor.blackColor;
[v addSubview:lbl];
return v;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifier = @"CellReuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] ;
//you can customize your cell here because it will be used just for one row.
}
cell.textLabel.text = [NSString stringWithFormat:@"I am the %ld Cell ",(long)indexPath.row];
cell.textLabel.textColor = UIColor.whiteColor;
cell.backgroundColor = UIColor.lightGrayColor;
return cell;
}
我已經創建了一個新的測試項目來測試您的場景,但是我無法復制,因為您可以看到headerview和section header之間沒有間隙。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.