1

I have a table view with the following implementation,

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    return [self.postsArr count];
}

This is working fine before I set the header view titles. I set the header view titles like,

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


    if (self.newsSegmentControl.selectedSegmentIndex == 1) {

        NSLog(@"section:%ld",(long)section-1);
        BTPost *sectionPost = [self.postsArr objectAtIndex:section];

        NSString *actDate = [NSString stringWithFormat:@"/Date(%ld)/",sectionPost.timeStamp];

        NSString *nDate = [[[[actDate componentsSeparatedByString:@"("] objectAtIndex:1] componentsSeparatedByString:@")"] objectAtIndex:0];
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:([nDate doubleValue] / 1000)];
        NSString *dayString = [self getDayLabel:date];
        sectionName =  dayString;

    }else{
        return nil;
    }
    return sectionName;
}

When I'm running this,

[![https://i.stack.imgur.com/aesU3.png][1]][1]

I got this kind of output. When I'm scrolling down it shows the section headers correctly. But when I'm scrolling up it shows the header with an empty title. How may I fix this?

codebot
  • 2,540
  • 3
  • 38
  • 89
  • Check what returns `sectionName` when scrolling up/down, maybe it returns `nil` when scrolling up, that's why headers get empty, and if the method executes `if` statement everytime... `NSLog(@"%@", sectionName);` – schmidt9 Mar 17 '16 at 20:06
  • Yes. I want to hide headers with the same titles. How may I do it? – codebot Mar 17 '16 at 20:09
  • Hm, you wrote initially only about "empty titles". Can they be the same? It means in your `postsArr` should be equal records – schmidt9 Mar 17 '16 at 20:12
  • It can be repeated. So Is there a way to compare the current title with last title of header? – codebot Mar 17 '16 at 20:20
  • Do you want to group records by date? What is selectedSegmentIndex? – schmidt9 Mar 17 '16 at 20:31
  • Basically you can store previous date and compare to current one. If they are equal, then return nil, it will hide header. – schmidt9 Mar 17 '16 at 20:43

0 Answers0