2012-11-21 99 views
0

我的大部分tableView都有一个恒定的表行高度。该桌子高度可以改变,但只能在设计时间。我不想编码该表行高度两次。如何创建一个计数符合某个协议的对象的类别?

所以我做这样的事情:

@interface UIViewController (cellHeightofFirstRowForTable) <UITableViewDataSource> 


@end 

#import "UIViewController+cellHeightofFirstRowForTable.h" 

@implementation UIViewController (cellHeightofFirstRowForTable) 
-(CGFloat) cellHeightForTable: (UITableView *) tableView 
{ 
    cellHeight= [self tableView:tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; 
} 
@end 

基本上在第一时间表格高度要求,我想计算一次,将其存储在一个静态变量,然后简单地使用该值。

我得到这个编译错误:

No visible @interface for 'UIViewController' declares the selector 'tableView:heightForRowAtIndexPath:' 

但喜,我已经在这是一个的UIViewController协议符合UITableViewDataSource委托界面中指定。

那有什么用?

我应该只是复制代码?

注意:现在我已经知道设计是愚蠢的。这个问题留待将来使用。

回答

1

无论有多明智的本能可能会告诉你,tableView:heightForRowAtIndexPath:UITableViewDelegate的一部分,而不是UITableViewDataSource。它也被标记为@optional,所以即使您确实声明了执行该协议的UIViewController类别,也不会确保该方法得到实施。

+0

鉴于当时我们总是将UITableViewDelegate和UITableViewDataSource委托设置为同一个对象,我想知道为什么苹果会将它们分开?哦,好吧...... –

+0

因为委托是处理交互的东西,而数据源就是这样。它们可以是并且有时是独立的对象 - 将其视为控制器与模型。 –

相关问题