[英]UITableView Delegate Method not getting called?
I' m trying to extract the UITableView Delegate method into a base class called BaseTableProvider
我正在尝试将UITableView Delegate方法提取到名为
BaseTableProvider
的基类中
//BaseTableProvider.h
@protocol RSTableProviderProtocol <NSObject>
- (void)cellDidPress:(NSIndexPath *) atIndexPath;
@optional
- (void)cellNeedsDelete:(NSIndexPath *) atIndexPath;
@end
@interface RSBaseTableProvider : NSObject <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, weak) id<RSTableProviderProtocol> delegate;
@end
//BaseTableProvider.m
@implementation RSBaseTableProvider
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc]init];
return cell;
}
@end
And then I create a subclass of BaseTableProvider, and override two UITableViewDelegate Method in the subclass 然后创建BaseTableProvider的子类,并在该子类中重写两个UITableViewDelegate方法
//RSFeedListTableProvider.h
@interface RSFeedListTableProvider : RSBaseTableProvider
@end
//RSFeedListTableProvider.m
@implementation RSFeedListTableProvider
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.dataSource count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
RSFeedCell *cell = (RSFeedCell *)[tableView dequeueReusableCellWithIdentifier:kFeedCell];
if(cell == nil){
cell = [[RSFeedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kFeedCell];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
Feed *feed = (Feed *)self.dataSource[indexPath.row];
if (feed != nil) {
cell.titleText = feed.title;
cell.subTitleText = feed.summary;
}
return cell;
}
@end
I initialized the ListTablerProvider in a ViewController. 我在ViewController中初始化了ListTablerProvider。
//RSFeedListViewController.m
@implementation RSFeedListViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
_feedListView = [[RSFeedListView alloc] init];
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
_provider = [[RSFeedListTableProvider alloc] init];
_provider.delegate = self;
return self;
}
#pragma mark -- Life Cycle
- (void)loadView{
RSFeedListView *aView = [[RSFeedListView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.feedListView = aView;
self.view = aView;
self.feedListView.tableView.delegate = _provider;
self.feedListView.tableView.dataSource = _provider;
}
But I can't see the cell built on the screen.I debuged the code found that UITableView Delegate Method in RSFeedListTableProvider was not called.Any one can help me?Thanks! 但是我看不到屏幕上构建的单元格。我调试了代码,发现未调用RSFeedListTableProvider中的UITableView Delegate Method,有人可以帮助我吗?谢谢!
Maybe you are inherited NSObject instead UITableViewController! 也许您继承了NSObject而不是UITableViewController! And you need to create property UITableView *tableView, and connect to your UITableViewController dataSource and delegate.
并且您需要创建属性UITableView * tableView,并连接到UITableViewController数据源和委托。
I think you did not implement the numberOfRowsInSection
datasource method in your subclass RSFeedListTableProvider
. 我认为您没有在子类
RSFeedListTableProvider
实现numberOfRowsInSection
数据源方法。 So it will invoke the super class implementation 因此它将调用超类实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 0;
}
and there you are returning zero , so cellForRowAtIndexPath
in your subclass never will be called. 并且在那里返回零 ,因此永远不会调用子类中的
cellForRowAtIndexPath
。
Solution is implement numberOfRowsInSection
in subclass and return proper count 解决方案是在子类中实现
numberOfRowsInSection
并返回正确的计数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.