ios - 当观察者在 UITableViewHeaderFooter 子类中时 NSNotification 被多次调用

标签 ios objective-c uitableview nsnotificationcenter

我正在实现NSNotificationsUITableViewHeaderFooter子类,因为我想更改 section按一次时的标题 row本节的内容。在 UITableViewHeaderFooter那里是 observer并在 tableViewController这是postnotification 。我已在观察器中记录了通知对象,并且我多次看到日志,因为部分位于 tableview 中。 ,对我来说这还不错,因为我可以通过indexPath.section来管理它,但这正常吗?如果我有更多部分,会有问题吗?有什么办法可以解决这个问题吗?

正如我在另一个问题中对 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];

    }
}

最佳答案

更新:抱歉,之前的代码存在一些与部分可重用性有关的错误,请使用新代码:

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

2.chang someHappens: 和 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;
    }

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

关于ios - 当观察者在 UITableViewHeaderFooter 子类中时 NSNotification 被多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19991793/

相关文章:

objective-c - 创建一个包含多个 NSMutableDictionary 对象的 NSMutableArray

ios - 创建一个新的Cocoapod : use example project for remote repo?

ios - 有没有办法在倒数计时器中使用 UIButton 作为文本?

ios - 在项目中手动导入时,从 swagger-codegen 生成的 Objective c 客户端无法正常工作

iOS - 索引超出范围

ios - Swift 2.2 – 如何为 UIView 中嵌套的 UIButton 调用 TouchesShouldCancelInContentView?

ios - Cell 和 TextField 未在 UITableView 中按顺序显示

ios - iphone如何截出高质量的截图

ios - 在 iOS 应用程序中使用 viewForHeaderInSection 方法

swift - 从 NSNotificationCenter 执行 reloadData() 后 UITableView 为空