今天写了个代理方法,代理也设置了,但是点击按钮时,按钮点击事件相应了,但是里面的代理却显示为空,就很郁闷,是哪的原因呢?
RecentCell
- (IBAction)clickMinsuButton:(UIButton *)sender {
if ([self.delegaterespondsToSelector:@selector(clickMinsuNameRecentCell:)]) {
[self.delegateclickMinsuNameRecentCell:self];
}
}
但是在控制器也设置代理了呀
PlanViewController
self.recentCell = [[RecentCellalloc] init];
CommonAdapter *commonAdapter = [[CommonAdapteralloc] initCommonAdapterWithTableview:self.planTableViewcell:self.recentCelldatas:self.recentArray];
self.recentCell.delegate =self;
self.commonAdapter = commonAdapter;
[commonAdapter refresh];
//下面方法不调用
#pragma mark - RecentCellDelegate
- (void)clickMinsuNameRecentCell:(RecentCell *)cell
{
MinsuDetailTableVC *minsuDetailTableVC = [[MinsuDetailTableVCalloc] init];
minsuDetailTableVC.hidesBottomBarWhenPushed =YES;
[selfpresentViewController:minsuDetailTableVCanimated:YEScompletion:^{
}];
}
待我细细看下代码,发现问题在于
self.recentCell = [[RecentCell alloc] init];
此时的recentCell是空的,并没有任何界面,而在适配器里面才显示了,所以,考虑应该在cell有
界面,点击时才会相应,所以就做了修改
CommonAdapter
@property (nonatomic,strong)UIViewController<RecentCellDelegate> * showViewController;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[self.cellclass] cellWithTableview:tableView];
//四边多余的区域
self.tableView.contentInset =UIEdgeInsetsZero;
if ([[self.cellclass] isSubclassOfClass:[RecentCellclass]]) {
RecentCell *recentCell = (RecentCell *)cell;
recentCell.delegate =self.showViewController;
}
return cell;
}
PlanViewController
self.recentCell = [[RecentCellalloc] init];
CommonAdapter *commonAdapter = [[CommonAdapteralloc] initCommonAdapterWithTableview:self.planTableViewcell:self.recentCelldatas:self.recentArray];
commonAdapter.showViewController =self;
self.commonAdapter = commonAdapter;
[commonAdapter refresh];
#pragma mark - RecentCellDelegate
- (void)clickMinsuNameRecentCell:(RecentCell *)cell
{
MinsuDetailTableVC *minsuDetailTableVC = [[MinsuDetailTableVCalloc] init];
minsuDetailTableVC.hidesBottomBarWhenPushed =YES;
[selfpresentViewController:minsuDetailTableVCanimated:YEScompletion:^{
}];
}
代理方法调用了,完美的解决了问题,OK。。。