我正在实现NSNotifications
在 UITableViewHeaderFooter
子类,因为我想更改 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/