<legend id='I5duP'><style id='I5duP'><dir id='I5duP'><q id='I5duP'></q></dir></style></legend>
<tfoot id='I5duP'></tfoot>

        <small id='I5duP'></small><noframes id='I5duP'>

        • <bdo id='I5duP'></bdo><ul id='I5duP'></ul>

        <i id='I5duP'><tr id='I5duP'><dt id='I5duP'><q id='I5duP'><span id='I5duP'><b id='I5duP'><form id='I5duP'><ins id='I5duP'></ins><ul id='I5duP'></ul><sub id='I5duP'></sub></form><legend id='I5duP'></legend><bdo id='I5duP'><pre id='I5duP'><center id='I5duP'></center></pre></bdo></b><th id='I5duP'></th></span></q></dt></tr></i><div id='I5duP'><tfoot id='I5duP'></tfoot><dl id='I5duP'><fieldset id='I5duP'></fieldset></dl></div>

        将委托添加到自定义 UITableViewCell(访问错误)

        时间:2023-07-28
      1. <legend id='PQwC4'><style id='PQwC4'><dir id='PQwC4'><q id='PQwC4'></q></dir></style></legend>
      2. <i id='PQwC4'><tr id='PQwC4'><dt id='PQwC4'><q id='PQwC4'><span id='PQwC4'><b id='PQwC4'><form id='PQwC4'><ins id='PQwC4'></ins><ul id='PQwC4'></ul><sub id='PQwC4'></sub></form><legend id='PQwC4'></legend><bdo id='PQwC4'><pre id='PQwC4'><center id='PQwC4'></center></pre></bdo></b><th id='PQwC4'></th></span></q></dt></tr></i><div id='PQwC4'><tfoot id='PQwC4'></tfoot><dl id='PQwC4'><fieldset id='PQwC4'></fieldset></dl></div>
          <tbody id='PQwC4'></tbody>
      3. <tfoot id='PQwC4'></tfoot>

            • <small id='PQwC4'></small><noframes id='PQwC4'>

              • <bdo id='PQwC4'></bdo><ul id='PQwC4'></ul>

                  本文介绍了将委托添加到自定义 UITableViewCell(访问错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试将自定义委托添加到我的自定义 UITableViewCell.

                  I'm trying to add a custom delegate to a custom UITableViewCell of mine.

                  在这个单元格上,我有一个按钮,它需要在 UITableView 所在的 ViewController 中触发一个方法.

                  On this cell I have a button which need to fire a method in the ViewController where the UITableView is located on.

                  我正在执行添加自定义委托的所有常规步骤,但由于某种原因,当我在运行时打开 VC 时,应用程序崩溃并给我一个错误的访问错误.

                  I'm doing all the usual steps to add a custom delegate but for some reason when I open the VC on runtime the app crashes and gives me a bad access error.

                  当我评论所有委托代码时,它可以正常工作,所以我猜测我添加委托的方式有问题.当我不注释我的 id 属性时,它似乎崩溃了.

                  When I comment all the delegate code it works properly so my guess is that something is wrong the way I add my delegate. When I leave my id property uncommented it seems to crash.

                  我在 VC 中实现协议.我确实将委托分配给了 cellForRowAtIndexPath: 中的单元格.所有委托方法都已到位.我一直在其他类中使用类似的结构,但之前从未在 UITableViewCells 的子类中使用过.

                  I implement the protocol in the VC. I did assign the delegate to the cell in the cellForRowAtIndexPath:. All the delegate methods are in place. I've been using similar constructions in other classes but never in a subclass of UITableViewCells before.

                  在我发布一些代码之前,我想知道是否有人以前遇到过这个错误.他是否以及如何解决它,或者是否可以为 UITableViewCell 子类制作自定义委托.

                  Before I post some code I would like to know if someone encountered this bug before. If and how he solved it or if it is even possible to make a custom delegate for an UITableViewCell subclass.

                  我的 customCell.h

                  My customCell.h

                  #import <UIKit/UIKit.h>
                  
                  @class MyTableViewCell;
                  
                  @protocol MyTableCellProtocoll <NSObject>
                  
                  -(void) didPressButton:(MyTableViewCell *)theCell;
                  
                  @end
                  
                  @interface MyTableViewCell : UITableViewCell
                  {
                  
                      UIButton *myButton;
                  
                      id<MyTableCellProtocoll> delegationListener;
                  
                  }
                  
                  @property (nonatomic,retain) UIButton *myButton;
                  
                  @property (nonatomic,assign) id<MyTableCellProtocoll> delegationListener;
                  
                  - (void) buttonAction;
                  
                  @end
                  

                  .m

                  #import "MyTableViewCell.h"
                  
                  @implementation MyTableViewCell
                  
                  @synthesize myButton;
                  @synthesize delegationListener;
                  
                  - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
                  {
                      self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
                      if (self) {
                          // Initialization code
                  
                          self.myButton = [UIButton buttonWithType:UIButtonTypeCustom];
                          self.myButton.backgroundColor = [UIColor clearColor];
                          self.myButton.frame = CGRectMake(5, 0, 10, 32);
                          self.myButton.titleLabel.adjustsFontSizeToFitWidth = YES;
                          [self.myButton addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
                          [self.contentView addSubview:self.myButton];  
                      }
                      return self;
                  }
                  
                  - (void) buttonAction
                  {
                      NSLog(@"buttonpressed");
                      [self.delegationListener didPressButton:self];
                  }
                  
                  @end
                  

                  MyVC 在 .h 中正确实现委托

                  MyVC implements the delegate as proper in the .h

                  @interface MyVC : UIViewController <UITableViewDelegate, UITableViewDataSource, MyTableCellProtocoll>
                  

                  它还使用 .m 中 MyTableCellProtocoll 中的一个委托方法

                  and it also uses the one delegatemethod from MyTableCellProtocoll in the .m

                  - (void)didPressButton:(MyTableViewCell *)theCell
                  {
                      NSLog(@"didPressButton");
                  }
                  

                  我在 cellForRowAtIndexPath 中分配委托:

                  I assign the delegate in the cellForRowAtIndexPath:

                  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
                  {    
                      static NSString *CellIdentifier = @"Cell";
                  
                      MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                      if (cell == nil) {
                          cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
                          cell.delegationListener = self;
                      }
                  
                      return cell;
                  }
                  

                  这些都是我用于代理的所有位置.我想要它做的是更改按钮标题并在该单元格之后插入一些行(修改后的重复项)

                  These are all locations I use for the delegate. What I want it to do is change the buttontitle and insert some rows after that cell (modified duplicates)

                  希望这会让事情变得更清楚.

                  Hope this will clear things up a bit more.

                  正如评论部分中提到的那样,按钮方法在开始时没有被调用.使用 logcode 编辑代码.

                  The button method is not being called at start as was metioned in the comment section. Editted code with logcode.

                  按钮没有问题.是委托属性在某处搞砸了.通过断点、日志和评论,我确定了这一点.

                  There is nothing wrong with the button. It is the delegate property that is messed up somewhere. With breakpoints and logs and commenting i made sure of that.

                  代码正在运行到我的数据源列表中的最后一个元素,然后它崩溃了.我想现在离问题更近了 1 步.

                  The code is being run up to the last element in my datasource list then it crashes. 1 step closer to the problem now i suppose.

                  最终好的,似乎所有的代表和按钮以及其他组件都不是问题.这是 heightForRowAtIndexPath: 把我搞砸了.总之感谢大家的支持!

                  FINAL Ok, it seems like all of the delegates and buttons and other components weren't the problem at all. It was the heightForRowAtIndexPath: screwing me over. Thanks for all of your support anyhow!

                  推荐答案

                  这个问题已经解决了.问题不在代表中,与其他答案中的按钮问题无关.问题在于对 heightForRowAtIndexpath: 单元格的访问,一旦弄清楚就很容易解决.

                  This question has been solved. The problem wasn't in the delegate and unrelated to the button issues in the other answers. The problem was lying in the access of a cell in the heightForRowAtIndexpath: and was easily solved once figured out.

                  jrturton 好心地为我指明了正确的方向,对此他表示感谢.

                  I was kindly pointed to the right direction by jrturton and for this he has my thanks.

                  这篇关于将委托添加到自定义 UITableViewCell(访问错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:iPhone 中的代理是什么? 下一篇:在 dealloc 中将委托设置为 nil

                  相关文章

                  <tfoot id='UpSuA'></tfoot>

                      <bdo id='UpSuA'></bdo><ul id='UpSuA'></ul>

                    <small id='UpSuA'></small><noframes id='UpSuA'>

                    <legend id='UpSuA'><style id='UpSuA'><dir id='UpSuA'><q id='UpSuA'></q></dir></style></legend>

                  1. <i id='UpSuA'><tr id='UpSuA'><dt id='UpSuA'><q id='UpSuA'><span id='UpSuA'><b id='UpSuA'><form id='UpSuA'><ins id='UpSuA'></ins><ul id='UpSuA'></ul><sub id='UpSuA'></sub></form><legend id='UpSuA'></legend><bdo id='UpSuA'><pre id='UpSuA'><center id='UpSuA'></center></pre></bdo></b><th id='UpSuA'></th></span></q></dt></tr></i><div id='UpSuA'><tfoot id='UpSuA'></tfoot><dl id='UpSuA'><fieldset id='UpSuA'></fieldset></dl></div>