سؤال

لدي سؤال بسيط للغاية (آمل ذلك). كيف يمكنني تغيير لون رأس القسم في UitableView من الشفافة الأزرق إلى الأسود؟ شكرا لك مقدما.

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

المحلول

تحتاج إلى تنفيذ هذه الطريقة في بروتوكول UitableViewDelegate:

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

هنا رابط إلى توثيق

... وافعل شيئًا كهذا (Sub في لونك الخاص):

UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 22)] autorelease];
[sectionView setBackgroundColor:[UIColor blackColor]];
return sectionView;

يمكنك أيضًا استخدام عدد صحيح في القسم إلى ألوان بديلة أو شيء مشابه. أعتقد أن الارتفاع الافتراضي للأقسام هو 22 ، ولكن يمكنك أن تجعله ما تريد. هل هذا ما قصدته بسؤالك؟ أتمنى أن يساعدك هذا.

نصائح أخرى

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

لا تتضمن هذه الطريقة تحديد طرق العرض المخصصة الخاصة بك.
في iOS 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/bblog/ios-to-to-customize-table-view-header-and-footer- colors/

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
   {
     UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,tableView.bounds.size.width, 30)];
     if (section == 0)
     [headerView setBackgroundColor:[UIColor redColor]];
     else 
     [headerView setBackgroundColor:[UIColor clearColor]];
     return headerView;
   }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top