2012-03-16 12 views
60

से छिपाने के हैडर मैं एक UITableView, कि एक चालू माह से खर्च को प्रदर्शित करता है (स्क्रीनशॉट देखें) है:UITableView: खाली अनुभाग

मेरे समस्या खाली वर्गों के लिए शीर्ष लेख के साथ है। क्या उन्हें छिपाने का कोई तरीका है? डेटा कोर्डाटा से लोड किया गया है।

TitleForHeader

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ 
if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0) { 
    return nil; 
} else { 

NSDate *today = [NSDate date ]; 
int todayInt = [dataHandler getDayNumber:today].intValue; 

NSDate *date = [NSDate dateWithTimeIntervalSinceNow:(-(todayInt-section-1)*60*60*24)]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]]];  
[dateFormatter setTimeStyle:NSDateFormatterNoStyle]; 
[dateFormatter setDateStyle:NSDateFormatterMediumStyle]; 
NSString *formattedDateString = [dateFormatter stringFromDate:date]; 
    return formattedDateString;} 

} 

ViewForHeader

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ 
if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0) { 
    return nil; 
} else { 

    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 312, 30)]; 
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(4, 9, 312, 20)]; 
    UIView *top = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 312, 5)]; 
    UIView *bottom = [[UIView alloc]initWithFrame:CGRectMake(0, 5, 312, 1)]; 

    [top setBackgroundColor:[UIColor lightGrayColor]]; 
    [bottom setBackgroundColor:[UIColor lightGrayColor]]; 

    [title setText:[expenseTable.dataSource tableView:tableView titleForHeaderInSection:section]]; 
    [title setTextColor:[UIColor darkGrayColor]]; 
    UIFont *fontName = [UIFont fontWithName:@"Cochin-Bold" size:15.0]; 
    [title setFont:fontName]; 


    [headerView addSubview:title]; 
    [headerView addSubview:top]; 
    [headerView addSubview:bottom]; 

    return headerView; 

} 

} 

heightForHeader

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 

NSLog(@"Height: %d",[tableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0); 
if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section == 0]) { 
    return 0; 
} else { 
    return 30; 
} 
} 

numberOfRow:

इस

कोड है कि शीर्ष लेख शीर्षक उत्पन्न करता है sInSection

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

int rows = 0; 
for (Expense* exp in [dataHandler allMonthExpenses]) { 
    if ([exp day].intValue == section) { 
     rows++; 
    } 
} 

return rows; 
} 

enter image description here सेबस्टियन

उत्तर

45

क्या होगा अगर में - tableView:viewForHeaderInSection: आप return nil यदि खंड गिनती 0.

संपादित है: आप में तत्वों की संख्या प्राप्त करने के लिए उपयोग कर सकते हैं numberOfRowsInSection अनुभाग।

संपादित: शायद आप में titleForHeaderInSection अगर numberOfRowsInSection है 0.

संपादित भी नहीं के बराबर लौटना चाहिए: आप निम्न विधि को लागू किया था?

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

संपादित: स्विफ्ट 3 उदाहरण

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    switch section { 
    case 0: 
     if self.tableView(tableView, numberOfRowsInSection: section) > 0 { 
      return "Title example for section 1" 
     } 
    case 1: 
     if self.tableView(tableView, numberOfRowsInSection: section) > 0 { 
      return "Title example for section 2" 
     } 
    default: 
     return nil // when return nil no header will be shown 
    } 
    return nil 
} 
+1

बीमार पाँच कोशिश नहीं है - बस दोपहर के भोजन के लिए बाहर गया ;-) बीमार पद परिणाम जैसे ही मैं पीसी पर वापस आ जाता हूं। धन्यवाद :-) –

+0

दुख की बात यह काम नहीं करती .. मैंने नए कोड और एक नया स्क्रीनशॉट के साथ सवाल अपडेट किया। –

+0

क्या आपको किसी प्रकार की त्रुटि मिली? – LuisEspinoza

10

विधि -[UITableViewDelegate tableView:heightForHeaderInSection:] पर एक नजर डालें। विशेष रूप से ध्यान दें कि इसके प्रलेखन के साथ जुडा हुआ:

पहले iOS 5.0 करने के लिए, टेबल देखा गया स्वचालित रूप से हेडर की ऊंचाई 0 वर्गों जहां tableView:viewForHeaderInSection: एक nil दृश्य लौटे के लिए आकार परिवर्तन होगा। आईओएस 5.0 और बाद में, आपको इस विधि में प्रत्येक सेक्शन हेडर के लिए वास्तविक ऊंचाई वापस करनी होगी।

+0

धन्यवाद इसलिए एमएच दोस्तों - बीमार आपको कुछ घंटों में अपडेट करते हैं और जो भी सबसे अच्छा काम करते हैं उन्हें स्वीकार और अपवर्तित करेंगे :-) –

+1

दुख की बात यह काम नहीं करती .. मैंने नए कोड के साथ प्रश्न अपडेट किया है & एक नया स्क्रीनशॉट। –

128

आपको उपयुक्त अनुभागों के लिए tableView:heightForHeaderInSection: से 0 सेट करना होगा। यह कुछ ऐसा है जो काफी हाल ही में बदल गया और मुझे कुछ स्थानों पर मिला। UITableViewDelegate यह कहते हैं ... से

पहले iOS 5.0 करने के लिए, टेबल देखा गया स्वचालित रूप से हेडर की ऊंचाई 0 वर्गों के लिए आकार बदलने जहां tableView होगा: viewForHeaderInSection: एक शून्य दृश्य लौट आए। आईओएस 5 में।0 और बाद में, आपको इस विधि में प्रत्येक सेक्शन हेडर के लिए वास्तविक ऊंचाई वापस करनी होगी।

तो तुम मुझे पता है यह एक पुराने सवाल यह है की तरह

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
    if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0) { 
     return 0; 
    } else { 
     // whatever height you'd want for a real section header 
    } 
} 
+0

दुख की बात यह काम नहीं करती .. मैंने नए कोड और एक नया स्क्रीनशॉट के साथ सवाल अपडेट किया। –

+0

आईओएस 6 और स्थिर तालिका कक्षों के साथ काम नहीं कर रहा है – helioz

+2

स्टेटिक टेबल थोड़ा अलग हैं। यह कोड कस्टम तालिका शीर्षलेख दृश्यों के लिए है, न केवल 'शीर्षक'। यदि आप 'शीर्षक' सेट कर रहे हैं तो आपको विधि कॉल 'तालिका दृश्य के लिए' शून्य 'वापस करने की आवश्यकता होगी: शीर्षकफॉरहेडर इनसेक्शन:' जब उचित हो। यदि आप कस्टम हेडर दृश्यों का उपयोग कर रहे हैं तो आपको 'viewForHeaderInSection' ओवरराइड करना होगा। यदि आप ऐसा कर रहे हैं, तो इस कोड को काम करना चाहिए। यदि नहीं, तो मुझे इस मुद्दे के साथ मदद करने के लिए और जानकारी चाहिए। – DBD

9

कुछ करना होगा, लेकिन मैं यह करने के लिए जोड़ना चाहते हैं। को heightForHeaderInSection से 0 में बदलने के लिए से 0 को बदलने के दृष्टिकोण को पसंद करते हैं क्योंकि यह indexPath के साथ समस्या का कारण बन सकता है, जहां से हेडर के कारण अभी भी वहां होना चाहिए लेकिन छुपा हुआ है।

साथ

ताकि कहा और DBD's answer पर निर्माण आप ऐसा है जैसे कि यह कोई पंक्तियों के साथ वर्गों के लिए नहीं के बराबर करने के लिए titleForHeaderInSection: सेट कर सकते हैं:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0) { 
     return nil; 
    } else { 
     // return your normal return 
    } 
} 
44

मेरी अजीब स्थिति में मैं वापस जाने के लिए है:

viewForHeaderInSection -> शून्य

viewForFooterInSection -> शून्य (! पाद लेख के बारे में भूल नहीं है)

heightForHeaderInSection ->0.01 (! शून्य नहीं)

heightForFooterInSection -> 0,01

केवल इस मामले में खाली वर्गों गायब हो जाते हैं पूरी तरह से

+9

मैंने इसे हेडर हाइट को 0.01 पर सेट करके किया था। –

+1

आईओएस 8.4 मैं अभी भी 0.01 का उपयोग कर पाठ देख सकते हैं, लेकिन 0.0 एक आकर्षण ओवरराइड समारोह tableView काम किया (tableView: UITableView, heightForHeaderInSection अनुभाग: इंट) -> CGFloat { अगर sectionedTableData [अनुभाग] .count == 0 { वापसी CGFloat (0.0) } वापसी CGFloat (40।0) } – DogCoffee

+0

0.01 मेरे लिए काम किया, lol –

4

यह उचित तरीके से हो रहा है , यह सही ढंग से एनिमेट करेगा & साफ काम करता है ... ऐप्पल के इरादे से ...

tableView प्रतिनिधि

करने के लिए उचित जानकारी प्रदान करें जब खंड में कोई आइटम नहीं, में 0.0f लौटें:

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

उपयुक्त डेटा हटाने कार्य करें:

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 

..Also के लिए नहीं के बराबर वापसी tableView

  1. कॉल [tableView beginUpdates];
  2. अपने डेटा स्रोत से आइटम निकालें, जहां तत्वों को निकाल कर ली का ट्रैक रखने ..
  3. कोशिकाओं आप हटाया की indexPaths साथ कॉल deleteRowsAtIndexPaths
  4. यदि आपके डेटास्रोत में कोई आइटम नहीं है (यहां आप केवल शीर्षलेख के साथ समाप्त हो जाएंगे)। उस खंड को फिर से लोड करने के लिए reloadSections: पर कॉल करें। यह सही एनीमेशन ट्रिगर करेगा और छुपा/स्लाइड/हेडर फीका होगा।
  5. अंत में [tableView endUpdates]; फोन अद्यतन समाप्त करने के लिए ..
7

2015 में iOS 8 और xCode6 का उपयोग कर, मेरे लिए काम किया गया था।

/* प्रत्येक खंड के लिए शीर्षक लौटें यदि और केवल यदि पंक्ति प्रत्येक अनुभाग के लिए गिनती 0. */

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ 

    if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0) { 
     return nil; 
    }else{ 

    // here you want to return the title or whatever string you want to return for the section you want to display 

    return (SomeObject*)someobjectArray[section].title; 
    } 
} 
संबंधित मुद्दे