0

NSNotifications在一个UITableViewHeaderFooter子类中实现,因为我想在本节之一中 section按下时更改标题。在那里它是并且在它是. 我已经在观察者中记录了通知对象,并且我多次看到日志,因为部分位于 中,对我来说这还不错,因为我可以通过 indexPath.section 对其进行管理,但这正常吗?如果我有更多部分,我会遇到问题吗?有没有办法处理这个?rowUITableViewHeaderFooterobservertableViewControllerpostnotificationtableview

正如我在另一个问题中对 Joiningss 所说的,当我在 tableView 中滚动时,我也遇到了部分标题的问题,标题更改并显示了我之前的标题。(可重用性问题..)

这是我的代码(请原谅我的代码,这只是一个测试)

首先是我的自定义 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;
}

这是我用于此实现的 TableView 方法:

#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];

    }
}
4

1 回答 1

1

更新:抱歉,之前的代码存在部分可重用的错误,请使用新代码:

1.去掉lastNotificationTitle和lastNotificationSection,添加NSMutableDictionary * notificationTitlesDic

2.chang somethingHappens: 和 configureHeaderSectionWithString: 代码:

 -(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;
    }

用我的无测试代码进行测试:]

于 2013-11-15T01:58:54.407 回答