I have a TableviewController : TableViewController.m , this has dynamic cells .
I have subclassed one cell(just one ) to : RegisterTableViewCell.m/.h , have a UIButton in this cell in storyboard and have created an outlet for the same in TableViewcell.m .
I have decalared a custom protocol in TableViewcell to get the click event on button in TableViewController.m . I want to do a segue on click of this button .
RegisterTableViewCell.h
@protocol CellDelegate <NSObject>
-(void)didClickOnCellAtIndex:(NSInteger)cellIndex withData:(id)data;
@end
@property (weak,nonatomic) id<CellDelegate>delegate;
@property (assign,nonatomic) NSInteger cellIndex;
@property (weak, nonatomic) IBOutlet UIButton *PopoverAnchorButton;
RegisterTableViewCell.m
@synthesize PopoverAnchorButton = _PopoverAnchorButton;
- (void)awakeFromNib {
// Initialization code
[self.PopoverAnchorButton addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
//[self.PopoverAnchorButton
}
- (void)didTapButton:(id)sender{
NSLog(@"Anchor Button Pressed");
NSLog(@"self.delegate %d ", [self.delegate isEqual:nil]);
NSLog(@"responds to selector %d ", [self.delegate respondsToSelector:@selector(didClickOnCellAtIndex:withData:)]);
if(self.delegate && [self.delegate respondsToSelector:@selector(didClickOnCellAtIndex:withData:)]){
[self.delegate didClickOnCellAtIndex:_cellIndex withData:@"abc"];
}
}
TableViewController.h
@interface SideBarTableViewController : UITableViewController <UIPopoverPresentationControllerDelegate ,CellDelegate>
TableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
RegisterTableViewCell *cellreg = [[RegisterTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"register"];
cellreg.delegate = self;
cellreg.cellIndex = indexPath.row;
return cell;
}
-(void)didClickOnCellAtIndex:(NSInteger)cellIndex withData:(id)data
{
NSLog(@"cell at index %ld clicked",(long)cellIndex);
}
Problem:
My didClickOnCellAtIndex func in not called in TableViewController.m . Even didTapButton func in RegisterTableViewCell.m , the 'if' condition is not executed, - "responds to selector = 0".
Also , i have just subclassed one of cells, whose instance i am getting in cellForRowAtIndexPath, but cellForRowAtIndexPath is called only once the table view is shown .
Won't the ref RegisterTableViewCell *cellreg and its property cellreg.delegate cleared by the time button on this cell is selected ?
My whole objective is to put a uibutton in some selected cells(for now say just 1) to the right end and when user clicks the cell or this button(preferably cell ), i want to do a popover presentation segue to a vc with popover arrow pointing towards the button .
For this question, my obj is to get the click event on the button in RegisterTableViewCell in TableViewController so that i can call prepareforsegue with the sender from here.
I am stuck at this . Please help.
The problem is:
You are put the UITableViewCell *cell
in your table and return it. So the tableview will show this UITableViewCell
cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
return cell;
You alloc init RegisterTableViewCell, assign Delegate.. but don't use it, (do't return cellreg) so it never show up in UI and you can not interaction with this cell to trigger button and delegate.
RegisterTableViewCell *cellreg = [[RegisterTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"register"];
cellreg.delegate = self;
cellreg.cellIndex = indexPath.row;
Fixed this by init and return RegisterTableViewCell
RegisterTableViewCell *cell = [tableView dequeueReuseWithIdentifier:@"regisID"];
if(cell == nill) {
[tableView registerNibForCellReuserWithIdentifier:@"regisID"];//with storyboard or nib
cell = [tableView dequeueReuseWithIdentifier:@"regisID"];
}
cell.delegate = cell;
//cell setData
return cell;