2013-07-10 6 views
78

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

मेरे पास के अंदर UITableView है और यह ऐसा प्रतीत नहीं होता है।

क्या यह 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; 
} 
+0

यह काम करना चाहिए।कुछ कोड दिखाएं –

+0

यह UITableView का उपयोग करने का एक बिल्कुल सही तरीका है, और शीर्षकों के साथ काम करना चाहिए। आपके मामले में हेडर क्या कर रहे हैं? – Eric

+0

@Eric हेडर पंक्तियों के साथ स्क्रॉल कर रहे हैं। स्क्रॉल करते समय वे टेबल के शीर्ष पर नहीं रुक रहे हैं। मैंने अपना टेबल व्यू जेनरेट करने के लिए उपयोग कर रहा कोड जोड़ा है। यह UIViewController के अंदर एक UITableView है। – topwik

उत्तर

233

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

+0

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

+6

इसके लिए कुछ संभावित स्पष्टीकरण हैं। एक महत्वपूर्ण नोट यह है कि हेडर केवल इसके अनुभाग के भीतर कोशिकाओं के लिए तय रहेंगे। इसलिए, यदि आपके पास सेक्शन 0 में हेडर है, तो यह धारा 1 में सेल्स के लिए तय नहीं रहेगा। एक और संभावित स्पष्टीकरण यह है कि आप उचित शैली के साथ अपने UITableView को प्रारंभ नहीं कर रहे हैं। यह अनुशंसा की जाती है कि आप 'initWithStyle: UITableViewStylePlain' का उपयोग करें, जैसे tableView.style = UITableViewStylePlain कुछ कॉल नहीं करेगा। – bachonk

+0

UITableViewStyleGrouped हेडर/फ़ूटर को अन्य कोशिकाओं के साथ तय करता है, जबकि UITableViewStylePlain हेडर/फ़ूटर "फ़्लोट" बनाता है जब हेडर/फ़ूटर टेबलव्यू के शीर्ष/नीचे तक पहुंच जाता है। – StoneLam

1

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

+0

आपने अपना जीवन बचाया! धन्यवाद केविनल! – BurtK

0

अपने tableview शैली बदलें:

self.tableview = [[UITableView alloc] initwithFrame: फ्रेम शैली: UITableViewStyleGrouped];

प्रति UITableView के लिए सेब प्रलेखन के रूप में:

UITableViewStylePlain- एक सादे तालिका दृश्य। किसी भी सेक्शन हेडर या फ़ूटर इनलाइन सेपरेटर्स के रूप में प्रदर्शित होते हैं और फ़्लोट करते हैं जब तालिका दृश्य स्क्रॉल किया जाता है।

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

आशा इस छोटा सा परिवर्तन से आपको मदद मिलेगी ..

+1

यह वास्तव में प्रश्न के विपरीत के विपरीत प्रतीत होता है - यह 'सादा' होना चाहिए, न कि 'समूहित' – CupawnTae

1

स्विफ्ट 3,0

UITableViewDelegate और UITableViewDataSource प्रोटोकॉल के साथ एक ViewController बनाएँ। फिर इसके अंदर एक तालिका बनाएं, इसकी शैली को 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

अब सादे तालिका शैली की तरह अपने tableview देखो लेकिन Buz सेटिंग तालिका शैली समूह के लिए सेट नाव नहीं है।

[_tableView setBackgroundView:nil]; 
_tableView.backgroundColor = [UIColor whiteColor]; 
संबंधित मुद्दे