【问题标题】:TableView not responding to TableViewcell custom protocolTableView 没有响应 TableViewcell 自定义协议
【发布时间】:2016-06-07 16:38:07
【问题描述】:

我有一个 TableviewController:TableViewController.m,它有动态单元格。
我已将一个单元格(只有一个)子类化为:RegisterTableViewCell.m/.h,在情节提要的此单元格中有一个 UIButton,并在 TableViewcell.m 中为相同的单元格创建了一个插座。
我在 TableViewcell 中声明了一个自定义协议,以获取 TableViewController.m 中按钮上的单击事件。我想在单击此按钮时进行转场。

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

问题:

在 TableViewController.m 中没有调用我的 didClickOnCellAtIndex 函数。 即使是 RegisterTableViewCell.m 中的 didTapButton func,'if' 条件也不会执行,-“响应选择器 = 0”。

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

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

我的全部目标是在某些选定的单元格(现在只说 1)中放置一个 uibutton 到右端,当用户单击单元格或此按钮(最好是单元格)时,我想做一个弹出式演示文稿 segue vc 带有指向按钮的弹出箭头。
对于这个问题,我的 obj 是在 TableViewController 的 RegisterTableViewCell 中的按钮上获取单击事件,以便我可以从这里调用 prepareforsegue 与发件人。

我被困在这个问题上。请帮忙。

【问题讨论】:

    标签: ios objective-c uitableview uipopovercontroller


    【解决方案1】:

    问题是:

    1. 您将UITableViewCell *cell 放入您的表中并将其返回。所以表格视图将显示这个UITableViewCell 单元格。

      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
      return cell;
      
    2. 你分配初始化RegisterTableViewCell,分配Delegate..但不要使用它,(不要返回cellreg)所以它永远不会出现在UI中,你不能与这个单元格交互来触发按钮和委托。

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

    通过 init 修复此问题并返回 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;
    

    【讨论】:

    • 完美 :) 非常感谢,你拯救了我的一天。我的答案就在我的问题本身中。已投票并接受为答案。
    • 从情节提要中使用标识符设置单元格永远不能为零。所以你实际上可以删除这 3 行。
    • @BhavukJain 我不喜欢使用情节提要,我通常使用 nil 文件代替,因此这是我设置 tableview 单元格的编码风格(便于重用具有多个 tableview 或多个项目的单元格)。也许他可以将单元格拖放到情节提要或使用 nib 创建和自定义单元格,我们不知道。
    • 我有单元格和 regeistertableviewcell,特别是在 d 故事板中。而且我使用这 3 行,是的,它不会为零。我只是使用了我没有返回单元格的想法。
    【解决方案2】:

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

    【讨论】:

    • 谢谢。我得到了它 。 :)
    【解决方案3】:

    您走在正确的轨道上,但我建议您更改一些内容以使其按您的意愿工作。

    让我们从RegisterTableViewCell 开始。您可能会发现通过右键单击按钮并拖入文件而不是awakeFromNib,从笔尖创建PopoverAnchorButton 操作连接更容易。此外,您可能需要某种设置函数,该函数接受诸如委托和单元索引之类的参数作为参数。这将允许您通过在表格出列时更新值来重用单元格。

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

    这应该让您的表格回收单元格,并保持适合索引的值,并且委托完好无损。

    【讨论】:

    • 指出,我肯定会尝试。谢谢
    【解决方案4】:

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

    NSNotificationCenter 可能有助于解决您的问题。

    您可以创建一个自定义单元格,并在右端添加一个按钮。根据您选择的任何条件,您可以隐藏按钮或将其显示在表格的特定行中。 您可以在单元格的自定义类中为按钮创建一个 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];
    }
    

    【讨论】:

    • 谢谢。我会试试这个。
    猜你喜欢
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    相关资源
    最近更新 更多