【发布时间】:2013-06-10 14:49:50
【问题描述】:
我没有在 stackoverflow 上找到等效的问题,所以我将就该问题发布我自己的问题(如果有什么不明白的地方请告诉我):
问题:
我在自定义UITableViewController 中使用带有A-Z 部分标题的通用索引tableView(如在contacts.app 中)。
我覆盖 tableView:viewForHeaderInSection: 以提供我自己的节标题视图。
当用户向上或向下滚动表格时,我需要听取在 tableView 顶部或底部出现/消失的部分标题(以相应地更改一些自定义视图)。
我重写了这些方法(自 iOS 6.0 起可用),它们完全符合我的需要:
-
tableView:didEndDisplayingHeaderView:forSection: -
tableView:willDisplayHeaderView:forSection:
2。向上/向下滚动 tableView 时永远不会调用方法 1,而每次节标题从屏幕上消失时都会调用方法 1。
我不知道为什么一个被调用而另一个没有。这是苹果的错误还是我做错了什么?
类似问题:
我在这里发现了一个类似的问题 How to call a method “willDisplayFooterView” during Table View cusomization? 一个答案是这样的:
它们只会在 iOS 6.0 或更高版本下被调用,并且只有当您还实现了其他页眉/页脚标题/视图方法时。如果您提供页眉或页脚,则无需调用它们
我不确定我是否理解这个答案,但如果是这样,在子类中实现哪些方法似乎很重要。
代码:
我只发布了我的tableViewController 的非空覆盖委托和数据源方法:
tvcA.m
@implementation tvcA
...
#pragma mark - Table view data source
- (NSArray *) sectionIndexTitlesForTableView : (UITableView *) tableView
{
// returns sectionIndexArray with alphabet for example
}
- (UIView *) tableView : (UITableView *) tableView
viewForHeaderInSection : (NSInteger) section
{
MNSectionHeaderView* headerView = [[MNSectionHeaderView alloc] initWithFrame : CGRectMake(0, 0, 260, 22)];
return headerView;
}
- (NSInteger) tableView : (UITableView *) tableView
sectionForSectionIndexTitle : (NSString *) title
atIndex : (NSInteger) index {...}
...
@end
tvcB.h(从 tvcA 继承)
@interface tvcB : tvcA
...
@end
tvcB.m
@implementation tvcB
...
#pragma mark - Table view data source
- (NSInteger) numberOfSectionsInTableView : (UITableView *) tableView {...}
- (NSInteger) tableView : (UITableView *) tableView
numberOfRowsInSection : (NSInteger) section {...}
- (UITableViewCell*) tableView : (UITableView *) tableView
cellForRowAtIndexPath : (NSIndexPath *) indexPath {...}
- (CGFloat) tableView : (UITableView *) tableView
heightForHeaderInSection : (NSInteger) section {...}
- (UIView *) tableView : (UITableView *) tableView
viewForHeaderInSection : (NSInteger) section {....}
#pragma mark - Table view delegate
- (void) tableView : (UITableView*) tableView
willDisplayHeaderView : (UIView*) view
forSection : (NSInteger) section {
// custom configurations
}
- (void) tableView : (UITableView*) tableView
didEndDisplayingHeaderView : (UIView*) view
forSection : (NSInteger) section {
// custom configurations
}
- (void) tableView : (UITableView *) tableView
didSelectRowAtIndexPath : (NSIndexPath *) indexPath {...}
...
@end
【问题讨论】:
标签: iphone objective-c ios6 uitableview indexed