फिक्स्ड सेक्शन हेडर के साथ UITableView


106

गलियों, मैं पढ़ रहा हूँ कि डिफ़ॉल्ट व्यवहार UITableViewअनुभाग शीर्ष पंक्तियों पंक्तियों को तालिका के शीर्ष पर पिन करना है क्योंकि आप अनुभागों के माध्यम से स्क्रॉल करते हैं जब तक कि अगला अनुभाग प्रीविओस अनुभाग पंक्ति को दृश्य से बाहर नहीं धकेलता है।

मैं एक UITableViewअंदर है UIViewControllerऔर यह मामला नहीं लगता है।

क्या यह केवल उद्दंड व्यवहार है UITableViewController?

यहाँ कुछ सरलीकृत कोड है जो मेरे पास है। मैं UIControllerइंटरफ़ेस और प्रत्येक तालिका दृश्य विधि दिखाऊंगा जिसे मैंने तालिका दृश्य बनाने के लिए कार्यान्वित किया है। मेरे पास एक सहायक डेटा स्रोत वर्ग है जो मुझे टेबल के साथ उपयोग के लिए अपनी वस्तुओं को अनुक्रमित करने में मदद करता है।

    @interface MyUIViewController ()<UITableViewDelegate, UITableViewDataSource>
        @property (nonatomic, readonly) UITableView *myTableView;
        @property (nonatomic, readonly) MyCustomHelperDataSource *helperDataSource;
    @end

    //when section data is set, get details for each section and reload table on success
    - (void)setSectionData:(NSArray *)sections {
        super.sectionData = sections; //this array drives the sections

        //get additional data for section details
        [[RestKitService sharedClient] getSectionDetailsForSection:someId 
        success:^(RKObjectRequestOperation *operation, RKMappingResult *details) {
            NSLog(@"Got section details data");
            _helperDataSource = [[MyCustomHelperDataSource alloc] initWithSections:sections andDetails:details.array];
            [myTableView reloadData];
        } failure:^(RKObjectRequestOperation *operation, NSError *error) {
            NSLog(@"Failed getting section details");
        }];
    }

    #pragma mark <UITableViewDataSource, UITableViewDelegate>

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        if (!_helperDataSource) return 0;
        return [_helperDataSource countSectionsWithDetails]; //number of section that have details rows, ignore any empty sections
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        //get the section object for the current section int
        SectionObject *section = [_helperDataSource sectionObjectForSection:section];
        //return the number of details rows for the section object at this section
        return [_helperDataSource countOfSectionDetails:section.sectionId];
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        UITableViewCell * cell;

        NSString *CellIdentifier = @"SectionDetailCell";

        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
            cell.textLabel.font = [UIFont systemFontOfSize:12.0f];
        }

        //get the detail object for this section
        SectionObject *section = [_helperDataSource sectionObjectForSection:indexPath.section]; 

        NSArray* detailsForSection = [_helperDataSource detailsForSection:section.sectionId] ;
        SectionDetail *sd = (SectionDetail*)[detailsForSection objectAtIndex:indexPath.row];

        cell.textLabel.text = sd.displayText;
        cell.detailTextLabel.text = sd.subText;
        cell.detailTextLabel.textColor = [UIColor blueTextColor];

        return cell;
    }

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50.0f;
}

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

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger) section {
    //get the section object for the current section
    SectionObject *section = [_helperDataSource sectionObjectForSection:section]; 

    NSString *title = @"%@ (%d)";

    return [NSString stringWithFormat:title, section.name, [_helperDataSource countOfSectionDetails:section.sectionId]];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 260, 0)];
    header.autoresizingMask = UIViewAutoresizingFlexibleWidth;

    header.backgroundColor = [UIColor darkBackgroundColor];

    SSLabel *label = [[SSLabel alloc] initWithFrame:CGRectMake(3, 3, 260, 24)];
    label.font = [UIFont boldSystemFontOfSize:10.0f];
    label.verticalTextAlignment = SSLabelVerticalTextAlignmentMiddle;
    label.backgroundColor = [UIColor clearColor];
    label.text = [self tableView:tableView titleForHeaderInSection:section];
    label.textColor = [UIColor whiteColor];
    label.shadowColor = [UIColor darkGrayColor];
    label.shadowOffset = CGSizeMake(1.0, 1.0);
    [header addSubview:label];

    return header;
}

वह काम करना चाहिए। कुछ कोड दिखाओ।
मथायस बाउच

UITableView का उपयोग करने के लिए यह पूरी तरह से ठीक तरीका है, और हेडर के साथ काम करना चाहिए। हेडर आपके मामले में क्या नहीं कर रहे हैं?
एरिक

@ शीर्ष लेख पंक्तियों के साथ स्क्रॉल कर रहे हैं। स्क्रॉल करते समय वे तालिका के शीर्ष पर नहीं रुक रहे हैं। मैंने अपने तालिका दृश्य उत्पन्न करने के लिए कोड का उपयोग किया है। यह एक UIViewController के अंदर एक UITableView है।
टॉपविक

जवाबों:


304

जब UITableViewStyleतालिका की संपत्ति सेट की जाती है तो हेडर केवल निश्चित रहते हैं UITableViewStylePlain। यदि आपने इसे सेट किया है UITableViewStyleGrouped, तो हेडर कोशिकाओं के साथ स्क्रॉल करेंगे।


3
मेरी टेबलव्यू शैली UITableViewStylePlain है, लेकिन यह या तो कोशिकाओं के साथ स्क्रॉल करती है।
युदुन

8
इसके लिए कुछ संभावित स्पष्टीकरण दिए गए हैं। एक महत्वपूर्ण नोट यह है कि हेडर केवल इसके खंड के भीतर कोशिकाओं के लिए तय किए जाएंगे। इसलिए, यदि आपके पास अनुभाग 0 में हेडर है, तो यह खंड 1 में कक्षों के लिए निश्चित नहीं रहेगा। एक अन्य संभावित व्याख्या यह है कि आप अपने यूआईटेबल व्यू को उचित शैली के साथ आरंभ नहीं कर रहे हैं। यह अनुशंसा की जाती है कि आप initWithStyle:UITableViewStylePlainटेबल व्यू.स्टाइल = UITableViewStylePlain जैसी किसी चीज़ को कॉल करने के लिए उपयोग नहीं करेंगे।
बॅकॉन्क

UITableViewStyleGrouped हैडर / पाद लेख को अन्य कक्षों के साथ नियत करता है, जबकि UITableViewStylePlain शीर्ष लेख / पाद लेख "फ्लोट" बनाता है जब शीर्ष लेख / पाद लेख तालिका के शीर्ष / तल पर पहुँचता है।
स्टोनलैम

@ लबोंक मैं एक स्ट्रेचेबल हेडर दृश्य का उपयोग कर रहा हूँ। और मेरी शैली UITableViewStyleGrouped है। मैं स्क्रॉल करते समय शीर्ष लेख को ठीक करना चाहता हूं। अगर मैं इसे UITableViewStylePlain बनाता हूं, तो हेडर स्क्रीन के बीच में तय हो रहा है, मैं हेडर को नीचे स्क्रॉल करना चाहता हूं, लेकिन जब स्क्रॉल करना चाहता हूं तो मैं इसे पहली पंक्ति पर ठीक करना चाहता हूं .. क्या यह संभव हो सकता है?
गोरिब डेवलपर

2

अपनी तालिका दृश्य शैली बदलें:

self.tableview = [[UITableView आवंटन] initwithFrame: फ़्रेम शैली: UITableViewStyleGrouped];

UITableView के लिए सेब प्रलेखन के अनुसार:

UITableViewStylePlain- एक सादा तालिका दृश्य। किसी भी अनुभाग शीर्षलेख या पाद लेख को इनलाइन विभाजक के रूप में प्रदर्शित किया जाता है और तालिका दृश्य स्क्रॉल किए जाने पर तैरता है।

UITableViewStyleGrouped- एक तालिका दृश्य जिसके अनुभाग पंक्तियों के विभिन्न समूहों को प्रस्तुत करते हैं। अनुभाग शीर्ष लेख और पाद लेख फ्लोट नहीं करते हैं।

आशा है यह छोटा सा बदलाव आपकी मदद करेगा ।।


2
यह प्रश्न के अनुरोध के बिल्कुल विपरीत प्रतीत होता है - यह होना चाहिए Plain, नहींGrouped
CupawnTae

1

स्विफ्ट 3.0

UITableViewDelegate और UITableViewDataSource प्रोटोकॉल के साथ एक ViewController बनाएँ । तो यह अंदर एक tableView बनाने के लिए, अपनी शैली की घोषणा होने के लिए UITableViewStyle.grouped । इससे हेडर ठीक हो जाएंगे।

lazy var tableView: UITableView = {
    let view = UITableView(frame: UIScreen.main.bounds, style: UITableViewStyle.grouped)
    view.delegate = self
    view.dataSource = self
    view.separatorStyle = .none
    return view
}()

0

आप तालिका के बाउंस गुण को NO पर सेट भी कर सकते हैं। यह सेक्शन हेडर को नॉन-फ्लोटिंग / स्टैटिक रखेगा, लेकिन तब आप टेबलव्यू की बाउंस प्रॉपर्टी भी खो देते हैं।


0

UITableView सेक्शन हेडर बनाने के लिए चिपचिपा या चिपचिपा नहीं:

  1. तालिका दृश्य की शैली बदलें - इसे चिपचिपा नहीं बनाने के लिए समूहीकृत करें और इसे चिपचिपा अनुभाग हेडर के लिए सादे बनाएं - यह मत भूलो: आप इसे स्टोरीबोर्ड से कोड लिखने के बिना कर सकते हैं। (अपने टेबल व्यू पर क्लिक करें और इसे दाईं ओर से शैली में बदलें / घटक मेनू)

  2. यदि आपके पास अतिरिक्त घटक हैं जैसे कि कस्टम दृश्य या आदि, तो कृपया उपयुक्त डिज़ाइन बनाने के लिए तालिका दृश्य के मार्जिन की जांच करें। (जैसे वर्गों के लिए हेडर की ऊंचाई और सूचकांक पथ पर सेल की ऊंचाई, वर्गों)


-2

अब आपका टेबलव्यू सादे टेबल स्टाइल की तरह दिखता है, लेकिन बज़ सेटिंग टेबल स्टाइल को ग्रुप में सेट न करें।

[_tableView setBackgroundView:nil];
_tableView.backgroundColor = [UIColor whiteColor];
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.