سؤال

كيف يمكنني تغيير لون رأس المقطع في UITableView?

تحرير:على الإجابة المقدمة من قبل DJ-S ينبغي النظر في نظام iOS 6 وما فوق.الجواب المقبول هو خارج التاريخ.

هل كانت مفيدة؟

المحلول

نأمل أن هذا الأسلوب من UITableViewDelegate بروتوكول الحصول على أنك بدأته:

الهدف-C:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
  UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
  if (section == integerRepresentingYourSectionOfInterest)
     [headerView setBackgroundColor:[UIColor redColor]];
  else 
     [headerView setBackgroundColor:[UIColor clearColor]];
  return headerView;
}

سويفت:

func tableView(_ tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView!
{
  let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 30))
  if (section == integerRepresentingYourSectionOfInterest) {
    headerView.backgroundColor = UIColor.redColor()
  } else {
    headerView.backgroundColor = UIColor.clearColor()
  }
  return headerView
}

تحديث 2017:

سويفت 3:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
    {
        let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 30))
        if (section == integerRepresentingYourSectionOfInterest) {
            headerView.backgroundColor = UIColor.red
        } else {
            headerView.backgroundColor = UIColor.clear
        }
        return headerView
    }

محل [UIColor redColor] مع أيهما UIColor كنت أود.قد ترغب أيضا في ضبط الأبعاد headerView.

نصائح أخرى

هذا هو السؤال القديم, ولكن أعتقد أن الجواب يحتاج إلى تحديث.

هذه الطريقة لا تنطوي على تحديد وخلق الخاصة بك عرض مخصصة.في دائرة الرقابة الداخلية 6 و حتى يمكنك بسهولة تغيير لون الخلفية و لون النص من خلال تحديد

-(void)tableView:(UITableView *)tableView 
    willDisplayHeaderView:(UIView *)view 
    forSection:(NSInteger)section

القسم مندوب طريقة

على سبيل المثال:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    // Background color
    view.tintColor = [UIColor blackColor];

    // Text Color
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    [header.textLabel setTextColor:[UIColor whiteColor]];

    // Another way to set the background color
    // Note: does not preserve gradient effect of original header
    // header.contentView.backgroundColor = [UIColor blackColor];
}

مأخوذة من موقعي هنا:https://happyteamlabs.com/blog/ios-how-to-customize-table-view-header-and-footer-colors/

سويفت 3 / 4

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
    view.tintColor = UIColor.red
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.textColor = UIColor.white
}

وفيما يلي كيفية تغيير لون النص.

UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width - 10, 18)] autorelease];
label.text = @"Section Header Text Here";
label.textColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.75];
label.backgroundColor = [UIColor clearColor];
[headerView addSubview:label];

ويمكنك القيام بذلك إذا كنت تريد رأس مع لون مخصص:

[[UITableViewHeaderFooterView appearance] setTintColor:[UIColor redColor]];

وهذا الحل يعمل كبيرة منذ دائرة الرقابة الداخلية 6.0.

الحل التالي يعمل سويفت 1.2 مع دائرة الرقابة الداخلية 8+

override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    // This changes the header background
    view.tintColor = UIColor.blueColor()

    // Gets the header view as a UITableViewHeaderFooterView and changes the text colour
    var headerView: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
    headerView.textLabel.textColor = UIColor.redColor()

}

لا تنس أن تضيف هذه القطعة من التعليمات البرمجية من مندوب أو سيتم خفض وجهة نظركم الخروج أو تظهر خلف طاولة في بعض الحالات، نسبة إلى ارتفاع وجهة نظركم / التسمية.

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

إذا كنت لا تريد إنشاء طريقة عرض مخصصة، يمكنك أيضا تغيير لون مثل هذا (يتطلب دائرة الرقابة الداخلية 6):

-(void) tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    if ([view isKindOfClass: [UITableViewHeaderFooterView class]]) {
        UITableViewHeaderFooterView* castView = (UITableViewHeaderFooterView*) view;
        UIView* content = castView.contentView;
        UIColor* color = [UIColor colorWithWhite:0.85 alpha:1.]; // substitute your color here
        content.backgroundColor = color;
    }
}

يمكنك أن تفعل ذلك على الرئيسية.القصة المصورة في 2 ثانية.

  1. تحديد عرض الجدول
  2. انتقل إلى سمات المفتش
  3. قائمة البند
  4. انتقل لأسفل لعرض العنوان الفرعي
  5. تغيير "الخلفية"

Have a look here

وتحديد لون الخلفية على UITableViewHeaderFooterView قد تم إهمال. الرجاء استخدام contentView.backgroundColor بدلا من ذلك.

وتعيين لون الخلفية والنص من قسم المجال: (شكرا لWilliam Jockusch وDj S)

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    if ([view isKindOfClass: [UITableViewHeaderFooterView class]]) {
        UITableViewHeaderFooterView* castView = (UITableViewHeaderFooterView*) view;
        castView.contentView.backgroundColor = [UIColor grayColor];
        [castView.textLabel setTextColor:[UIColor grayColor]];
    }
}

سويفت 4

تغيير لون الخلفية, نص التسمية اللون و الخط على رأس من UITableView قسم ببساطة تجاوز willDisplayHeaderView جدول عرض مثل ذلك:

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        let header = view as! UITableViewHeaderFooterView
        header.backgroundView?.backgroundColor = .white
        header.textLabel?.textColor = .black
        header.textLabel?.font = UIFont(name: "Helvetica-Bold", size: 14)
} 

هذا يعمل تماما بالنسبة لي ، وآمل أن لا تساعدك أيضا!

إليك كيفية إضافة صورة في عرض الرأس:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
    UIImageView *headerImage = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top-gery-bar.png"]] autorelease];

    headerImage.frame = CGRectMake(0, 0, tableView.bounds.size.width, 30);

    [headerView addSubview:headerImage];

    return headerView;
}

لiOS8 (بيتا) وسويفت اختيار اللون RGB تريد وحاول هذا:

override func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView! {
    var header :UITableViewHeaderFooterView = UITableViewHeaderFooterView()

    header.contentView.backgroundColor = UIColor(red: 254.0/255.0, green: 190.0/255.0, blue: 127.0/255.0, alpha: 1)
    return header

و}

(و"تجاوز" هناك منذ وأنا باستخدام UITableViewController بدلا من UIViewController الطبيعي في مشروعي، ولكن انها ليست إلزامية لتغيير لون مقطع الرأس)

وسيظل ينظر إلى نص رأس الخاص بك. ملاحظة أنك سوف تحتاج إلى ضبط ارتفاع مقطع الرأس.

وحظا سعيدا.

سويفت 2

كنت قادرا على النجاح في تغيير لون خلفية القسم مع إضافة طمس الأثر (الذي هو حقا بارد).لتغيير لون خلفية القسم بسهولة:

  1. أولا اذهب إلى لوحة العمل و تحديد عرض الجدول
  2. انتقل إلى سمات المفتش
  3. قائمة البند
  4. انتقل لأسفل لعرض
  5. تغيير "الخلفية"

ثم طمس الأثر ، إضافة إلى التعليمات البرمجية:

override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    // This is the blur effect

    let blurEffect = UIBlurEffect(style: .Light)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)

    // Gets the header view as a UITableViewHeaderFooterView and changes the text colour and adds above blur effect
    let headerView: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
    headerView.textLabel!.textColor = UIColor.darkGrayColor()
    headerView.textLabel!.font = UIFont(name: "HelveticaNeue-Light", size: 13)
    headerView.tintColor = .groupTableViewBackgroundColor()
    headerView.backgroundView = blurEffectView

}

وأنا أعرف الإجابة لها، فقط في حالة، وفي استخدام سويفت ما يلي

    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let tableViewWidth = self.tableView.bounds

        let headerView = UIView(frame: CGRectMake(0, 0, tableViewWidth.size.width, self.tableView.sectionHeaderHeight))
        headerView.backgroundColor = UIColor.greenColor()

        return headerView
    }

دائرة الرقابة الداخلية 8+

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        tableView.tableHeaderView?.backgroundColor = UIColor.blue()
}

وبناء على الجوابDj S، وذلك باستخدام سويفت 3. هذا يعمل كبيرة على دائرة الرقابة الداخلية 10.

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    // Background color
    view.tintColor = UIColor.black

    // Text Color
    let headerView = view as! UITableViewHeaderFooterView
    headerView.textLabel?.textColor = UIColor.white
}

ولدي مشروع استخدام الخلايا عرض جدول ثابت، في دائرة الرقابة الداخلية 7.x. willDisplayHeaderView لا اطلاق النار. ومع ذلك، فإن هذا الأسلوب يعمل طيب:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSLog(@"%s", __FUNCTION__);
    CGRect headerFrame = CGRectMake(x, y, w, h);    
    UIView *headerView = [[UIView alloc] initWithFrame:headerFrame];  
    headerView.backgroundColor = [UIColor blackColor];
 -(void) tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view
  forSection:(NSInteger)section
  {
        if ([view isKindOfClass: [UITableViewHeaderFooterView class]])
        {
             UITableViewHeaderFooterView *castView = (UITableViewHeaderFooterView *) view;
             UIView *content = castView.contentView;
             UIColor *color = [UIColor whiteColor]; // substitute your color here
             content.backgroundColor = color;
             [castView.textLabel setTextColor:[UIColor blackColor]];
        }
 }

وأعتقد أن هذا الرمز هو ليس سيئا للغاية.

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = tableView.dequeueReusableHeaderFooterViewWithIdentifier(MyHeaderView.reuseIdentifier) as MyHeaderView
    let backgroundView = UIView()
    backgroundView.backgroundColor = UIColor.whiteColor()
    headerView.backgroundView = backgroundView
    headerView.textLabel.text = "hello"
    return headerView
}

في دائرة الرقابة الداخلية 7.0.4 أنا خلقت رأس مخصص مع أنها تملك XIB.لا شيء يذكر هنا قبل أن ينجح.كان فرعية من UITableViewHeaderFooterView للعمل مع dequeueReusableHeaderFooterViewWithIdentifier: ويبدو أن الطبقة هي عنيدة جدا بخصوص لون الخلفية.حتى أخيرا وأضاف UIView (هل يمكن أن تفعل ذلك أيضا مع رمز أو IB) مع اسم customBackgroudView ثم قم بتعيين انها backgroundColor الملكية.في layoutSubviews:أنا وضعت هذا الرأي هو الإطار إلى حدود. يعمل مع دائرة الرقابة الداخلية 7 و يعطي أي مواطن الخلل.

// in MyTableHeaderView.xib drop an UIView at top of the first child of the owner
// first child becomes contentView

// in MyTableHeaderView.h
@property (nonatomic, weak) IBOutlet UIView * customBackgroundView;

// in MyTableHeaderView.m
-(void)layoutSubviews;
{
    [super layoutSubviews];

    self.customBackgroundView.frame = self.bounds;
}
// if you don't have XIB / use IB, put in the initializer:
-(id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    ...
    UIView * customBackgroundView = [[UIView alloc] init];
    [self.contentView addSubview:customBackgroundView];
    _customBackgroundView = customBackgroundView;
    ...
}


// in MyTableViewController.m
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    MyTableHeaderView * header = [self.tableView
                                          dequeueReusableHeaderFooterViewWithIdentifier:@"MyTableHeaderView"];
    header.customBackgroundView.backgroundColor = [UIColor redColor];
    return header;
}

ومجرد تغيير لون طبقة من وجهة النظر رأس

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
  UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,    tableView.bounds.size.width, 30)] autorelease];
 headerView.layer.backgroundColor = [UIColor clearColor].CGColor
}

إذا كان أي شخص يحتاج سريع، وتبقي عنوان:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView(frame: CGRect(x: 0,y: 0,width: self.tableView.frame.width, height: 30))
    view.backgroundColor = UIColor.redColor()
    let label = UILabel(frame: CGRect(x: 15,y: 5,width: 200,height: 25))
    label.text = self.tableView(tableView, titleForHeaderInSection: section)
    view.addSubview(label)
    return view
}

في حالتي، وهي تعمل مثل هذا:

let headerIdentifier = "HeaderIdentifier"
let header = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: headerIdentifier)
header.contentView.backgroundColor = UIColor.white

وفقط تعيين لون الخلفية من وجهة النظر الخلفية:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){         
  let tableHeader = view as! UITableViewHeaderFooterView        
  tableHeader.backgroundView?.backgroundColor = UIColor.white     
}

ومع RubyMotion / RedPotion، لصق هذا في TableScreen الخاص بك:

  def tableView(_, willDisplayHeaderView: view, forSection: section)
    view.textLabel.textColor = rmq.color.your_text_color
    view.contentView.backgroundColor = rmq.color.your_background_color
  end

وتعمل مثل السحر!

وحصلت على رسالة من كسكودي من خلال السجل حدة

<اقتباس فقرة>   

[TableView] إعداد لون الخلفية على   وقد تم إهمال UITableViewHeaderFooterView. يرجى تحديد العرف   UIView مع لون الخلفية المطلوب لbackgroundView   الملكية بدلا من ذلك.

وبعد ذلك أنا مجرد خلق UIView جديد، ووضع كخلفية لHeaderView. ليس حلا جيدا ولكن من السهل كما قال كسكودي.

وعلى الرغم من أن func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) ستعمل كذلك، يمكنك acheive هذا دون تنفيذ طريقة مندوب آخر. فيكم func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? الطريقة، يمكنك استخدام view.contentView.backgroundColor = UIColor.white بدلا من view.backgroundView?.backgroundColor = UIColor.white الذي لا يعمل. (وأنا أعلم أن backgroundView هو اختياري، ولكن حتى عندما يكون هناك، وهذا ليس ووكينغ دون تنفيذ willDisplayHeaderView

وعن طريق UIAppearance يمكنك تغييره لجميع الرؤوس في التطبيق الخاص بك مثل هذا:

وUITableViewHeaderFooterView.appearance (). backgroundColor = theme.subViewBackgroundColor

وسويفت 4 يجعل من السهل جدا. ببساطة هذا إضافة إلى صفك وتعيين لون حسب الحاجة.

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        view.backgroundColor = UIColor(red: 0.094, green: 0.239, blue: 0.424, alpha: 1.0)
    }

وأو إذا كان لون بسيط

override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        view.backgroundColor = UIColor.white
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top