我正在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];
}
}
答案 0 :(得分:1)
更新:抱歉,以前的代码内容有部分可重用性的错误,请使用新代码:
1.删除lastNotificationTitle和lastNotificationSection,添加NSMutableDictionary * notificationTitlesDic
2.chang somethingHappens:和configureHeaderSectionWithString:with 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;
}
使用我的无测试代码进行测试:]