簡體   English   中英

Header和第一個單元格之間的分離 - 在簡單的UITableView中

[英]Separation between Header and first cell — In plain UITableView

我有一張普通風格的桌子,只有一個部分。 我有一個實現的viewForHeaderInSection:在節頭中添加自定義視圖。

我無法在表格節標題視圖和第一個單元格之間看到分隔線。 [見附圖]

替代文字

我究竟做錯了什么?

正如Jeremy在他的回答中所說,iOS不會在頁眉/頁腳的上方/下方添加分隔符; 你可以使用UIView自己創建一條線。

以下是將標准查看分隔符視圖添加到標題視圖的代碼:

CGRect sepFrame = CGRectMake(0, headerView.frame.size.height-1, 320, 1);
seperatorView = [[[UIView alloc] initWithFrame:sepFrame] autorelease];
seperatorView.backgroundColor = [UIColor colorWithWhite:224.0/255.0 alpha:1.0];
[headerView addSubview:seperatorView];

如果您試圖使它看起來像普通的表格視圖單元格,您可能還需要在標題視圖的頂部添加一個。

自定義頁眉和頁腳在其下方/上方不包含分隔符。 您需要在自定義視圖中自己實現分隔符(或切換到分組樣式,即使使用自定義頁眉/頁腳,也會在其上方和下方顯示組的輪廓)。

如果你想只在表頭和表第一行之間給出空間,那么你可以使用

tableView:heightForHeaderInSection:(NSInteger)section方法中tableView:heightForHeaderInSection:(NSInteger)section

if(section ==0)
    return 3; // (space u want to give between header and first row);

return 10; //(ur section header height)

tableView:viewForHeaderInSection:(NSInteger)section方法中tableView:viewForHeaderInSection:(NSInteger)section

  UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 3)];
  headerView.backgroundColor = [UIColor clearColor];   // use your own design

  return headerView;

通過在tableView:numberOfRowsInSection:返回+1現有行數,向要添加分隔符的部分添加一個額外的“隱藏”行tableView:numberOfRowsInSection: . 然后添加以下方法:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ( indexPath.section == sectionOfHiddenRow && indexPath.row == indexOfHiddenRow )
        return 0.f;
    else
        return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}

如果您希望分隔符位於節的頂部(標題之后),則indexOfHiddenRow將為0 如果你想要它在一個部分的底部(在頁腳之前)它將是[self tableView:tableView numberOfRowsInSection:sectionOfHiddenRow] - 1

現在在tableView:cellForRowAtIndexPath:里面,只需為隱藏行返回[UITableViewCell new] (它不會顯示,所以不需要設置框架或任何東西)。 您可能需要在UITableViewDataSourceUITableViewDelegate方法中進行一些-1索引調整,但它可以工作(在iOS 7中測試), 保證一致的樣式(不需要繪制自己的“假”分隔符 - 這是一個真正的系統繪制UITableView分隔符)。

我用一些分隔符方法(在Swift中)擴展了UITableViewCell。 有了它們,我可以在標題中添加分隔符或從常規單元格中刪除它們。 我希望它可以幫助一些人。

public extension UITableViewCell
{
    func addSeparator(y: CGFloat, margin: CGFloat, color: UIColor)
    {
        let sepFrame = CGRectMake(margin, y, self.frame.width - margin, 0.7);
        let seperatorView = UIView(frame: sepFrame);
        seperatorView.backgroundColor = color;
        self.addSubview(seperatorView);
    }

    public func addTopSeparator(tableView: UITableView)
    {
        let margin = tableView.separatorInset.left;

        self.addSeparator(0, margin: margin, color: tableView.separatorColor!);
    }

    public func addBottomSeparator(tableView: UITableView, cellHeight: CGFloat)
    {
        let margin = tableView.separatorInset.left;

        self.addSeparator(cellHeight-2, margin: margin, color: tableView.separatorColor!);
    }

    public func removeSeparator(width: CGFloat)
    {
        self.separatorInset = UIEdgeInsetsMake(0.0, width, 0.0, 0.0);
    }

}

在標題視圖和第一行之間添加一個分隔符: - 為了查看委托方法中的標題,添加一個子視圖self.separator // @ property(非原子,強)UIImageView *分隔符;

- (CGFloat)tableView:(UITableView *)tableView
heightForHeaderInSection:(NSInteger)section {

return 41; 
}


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

self.headerView = [[UIView alloc] init];
self.headerView.backgroundColor = [UIUtils colorForRGBColor:TIMESHEET_HEADERVIEW_COLOR];

self.separator = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"seperator.png"]];
self.separator.frame = CGRectMake(0,40,self.view.frame.size.width,1);
[self.headerView addSubview:self.separator];
return self.headerView;

}

暫無
暫無

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

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