Question

I am implementing NSNotifications in a UITableViewHeaderFooter subclass because I want to change the section title when press in one row of this section.In UITableViewHeaderFooter there it is the observer and in the tableViewController it is the postnotification. I have logged the notification object in the observer and I see the log many times as sections are in the tableview, for me it is not bad because I can manage it passing indexPath.section, but is it normal? Could I have problems if I have more sections? Is there any way to handle this?

As I said to Joiningss in the other question I also have troubles with section title when I do scroll in the tableView, the title change and show the title that I had before.(Reusability problems..)

Here is my code (Forgive me for the code this is just a test)

First my Custom HeaderFooterView:

- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor blackColor];
        textLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 14)];
        [textLabel setTextColor:[UIColor whiteColor]];
        [textLabel setFont:[UIFont systemFontOfSize:14]];
        [textLabel setBackgroundColor:[UIColor clearColor]];

        [self.contentView addSubview:textLabel];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(somethingHappens:) name:@"notificationName" object:nil];


    }
    return self;
}

-(void) somethingHappens:(NSNotification*) notification
{
    NSLog(@"Notification %@", notification);
    NSDictionary *notificationDic = notification.object;
    NSString *title = [notificationDic valueForKey:@"title"];
    NSIndexPath *indexPath = [notificationDic objectForKey:@"index"];
    [self configureHeaderSectionWithString:title andSection:indexPath.section];
}
-(void)configureHeaderSectionWithString:(NSString *) text andSection:(NSInteger)section
{
    if(self.section == section)
    textLabel.text = text;
}

Here my TableView methods for this implementation:

#pragma mark - Custom getter

- (UITableView *)tableView {
    //custom init of the tableview
    if (!tableView) {
        // regular table view
        tableView = [[UITableView alloc] initWithFrame:UIEdgeInsetsInsetRect(self.view.bounds, tableViewInsets) style:UITableViewStylePlain];
        tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor blackColor];
        [tableView registerClass:[WPSectionHeaderView class] forHeaderFooterViewReuseIdentifier:SectionHeaderViewIdentifier];

        return tableView;
    }
    return tableView;
}


#pragma mark - TableView Delegate

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

     WPSectionHeaderView *sectionHeaderView = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:SectionHeaderViewIdentifier];

    sectionHeaderView.section = section;
    sectionHeaderView.delegate = self;
    [sectionHeaderView configureHeaderSectionWithString:[sectionsArray objectAtIndex:section] andSection:section];

    return sectionHeaderView;
}
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 2){
        NSString *title = [categoriesArray objectAtIndex:indexPath.row];
        NSDictionary *dic = [NSDictionary dictionaryWithObjects:@[indexPath,title] forKeys:@[@"index", @"title"]];

        [[NSNotificationCenter defaultCenter] postNotificationName:@"notificationName" object:dic];

    }
}
Was it helpful?

Solution

UPDATE : Sorry,previous code content some bugs with section reusability,please use the new code:

1.remove lastNotificationTitle and lastNotificationSection, add NSMutableDictionary * notificationTitlesDic

2.chang somethingHappens: and configureHeaderSectionWithString: with the code:

 -(void) somethingHappens:(NSNotification*) notification
    {
        NSLog(@"Notification %@", notification);
        NSDictionary *notificationDic = notification.object;
        NSString *title = [notificationDic valueForKey:@"title"];
        NSIndexPath *indexPath = [notificationDic objectForKey:@"index"];
        [self.notificationTitlesDic setObject:title forKey:[NSString  stringWithFormat:@"%d",indexPath.section]];
        if(self.section == indexPath.section){
            textLabel.text = title;
        } 
    }


 -(void)configureHeaderSectionWithString:(NSString *) text andSection:(NSInteger)section
    {
        NSString * targetKey = [NSString stringWithFormat:@"%d",section];
        for (NSString * key in self.notificationTitlesDic) {
            if([targetKey isEqualToString:key]){
                textLabel.text = [self.notificationTitlesDic valueForKey:key];
                return;
            }
        }
        textLabel.text = text;
    }

have a test with my testless code :]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top