TableView 不响应 TableViewcell 自定义协议

TableView not responding to TableViewcell custom protocol

我有一个 TableviewController:TableViewController.m,它有动态单元格。
我将一个单元格(只有一个)子类化为:RegisterTableViewCell.m/.h,在故事板的这个单元格中有一个 UIButton,并在 TableViewcell.m 中为相同的单元格创建了一个插座。
我在 TableViewcell 中声明了一个自定义协议,以获取 TableViewController.m 中按钮的点击事件。我想在点击这个按钮时做一个 segue。

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);
}

问题:

我的 didClickOnCellAtIndex 函数未在 TableViewController.m 中调用。 即使是 RegisterTableViewCell.m 中的 didTapButton func,也不执行 'if' 条件,- "responds to selector = 0"。

另外,我刚刚对其中一个单元格进行了子类化,我在 cellForRowAtIndexPath 中获取了它的实例,但是仅在显示 table 视图时才调用 cellForRowAtIndexPath。

不会通过选择此单元格上的时间按钮清除 ref RegisterTableViewCell *cellreg 及其 属性 cellreg.delegate 吗?

我的全部 objective 是在一些选定的单元格(现在只说 1)中将 uibutton 放在右端,当用户单击单元格或此按钮(最好是单元格)时,我想做一个弹出窗口显示转至 vc,弹出窗口箭头指向按钮 .
对于这个问题,我的目标是在 TableViewController 的 RegisterTableViewCell 中的按钮上获取单击事件,以便我可以从这里与发件人一起调用 prepareforsegue。

我卡在这了。请帮忙。

您没有在 cellForRowAtIndexPath 中返回 cellreg (RegisterTableViewCell),这就是问题所在。

您走在正确的轨道上,但我建议您进行一些更改以使它按照您想要的方式工作。

让我们从 RegisterTableViewCell 开始。您可能会发现,通过右键单击您的按钮并拖动到文件中,从笔尖创建 PopoverAnchorButton 操作连接比 awakeFromNib 更容易。此外,您可能需要某种设置函数,它接受诸如委托和单元格索引之类的参数作为参数。这将使您可以通过在 table.

出队时更新值来重用该单元格

接下来,TableViewController。要让您的 table 视图回收单元格,请创建一个重用标识符字符串(您将为所有相同类型的单元格使用相同的标识符)并在您的 table 视图上调用 registerNib:forCellReuseIdentifier用你的 RegisterTableViewCellviewDidLoad 之类的地方创建的 UINib 对象。然后,在 tableView:cellForRowAtIndexPath: 中,您将使用 dequeueReusableCellWithIdentifier 获得可重复使用的单元格。将其转换为您的自定义 class,调用我提到的设置函数,以便您的委托和索引属性正确,然后 return 它(您不需要像当前那样创建两个单元格)。

这应该得到您的 table 回收单元,并保持适合索引的值,委托完好无损。

问题是:

  1. 你把 UITableViewCell *cell 放在你的 table 和 return 里面。所以 table 视图将显示此 UITableViewCell 单元格。

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    return cell;
    
  2. 你分配了 init RegisterTableViewCell,分配了 Delegate.. 但不要使用它,(不要 return cellreg)所以它永远不会出现在 UI 中,你无法与此单元格交互以触发按钮和委托。

    RegisterTableViewCell *cellreg = [[RegisterTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"register"];
    cellreg.delegate = self;
    cellreg.cellIndex = indexPath.row;
    

已通过 init 和 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;

据我了解,您想在按下按钮时呈现另一个视图。仅在某些单元格上出现的按钮?

NSNotificationCenter 或许能帮您解决问题。

您可以创建一个自定义单元格,并在右端添加一个按钮。根据您选择的任何条件,您可以隐藏按钮或在 table 的特定行中显示它。 您可以为单元格自定义 class 内的按钮创建一个 IBAction 并触发通知以告诉您该按钮已被按下。

- (IBAction)press:(UIButton *)sender {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"show" object:nil];
}

在您的主视图控制器中,只需将观察者添加到调用选择器以呈现您选择的视图控制器的通知中。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showSegue) name:@"show" object:nil];
}