简体   繁体   中英

Adding bottom border for default section header of UITableView in Xamarin.iOS

In the table view I am having few sections, I am using the default section header of the table view, I simply set the title and height using the following methods.

public override string TitleForHeader(UITableView tableView, nint section)
{
    return sectionHeaders[Convert.ToInt32(section)];
}

public override nfloat GetHeightForHeader(UITableView tableView, nint section)
{
    return 70;
}

I want to set bottom border for the section header.

I searched and tried this answer but it simply gives a transparent header with red border, and there is no header title which I set in the TitleForHeader method. Is there any way to set bottom border for the default section header.

You can override GetViewForHeader method and create your own view and customise it as per your requirement instead of default view like this:

public override UIView GetViewForHeader(UITableView tableView, nint section)
{
    var headerView = new UIView(new CGRect(0, 0, (float)tableView.Bounds.Width, 70));

    var label = new UILabel(new CGRect(15, 35, (float)tableView.Bounds.Width - 100, 15));
    label.TextColor = UIColor.Gray;
    label.Font = UIFont.FromName("Helvetica", 12f);
    label.Text = @"Sample Title"; //sectionHeaders[Convert.ToInt32(section)];
    headerView.AddSubview(label);

    var bottomBorder = new UIView(new CGRect(0, 69, (float)tableView.Bounds.Width, 1));
    bottomBorder.BackgroundColor = UIColor.Red;
    headerView.AddSubview(bottomBorder);

    return headerView;
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM