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

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

        <tfoot id='QEgf4'></tfoot>

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

        Objective c - 为自定义 UITableViewCell 的按钮处理按钮触摸事件的最佳实践

        时间:2023-07-30
          <tbody id='e8hnt'></tbody>

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

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

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

                  <tfoot id='e8hnt'></tfoot>
                  本文介绍了Objective c - 为自定义 UITableViewCell 的按钮处理按钮触摸事件的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  为自定义 UITableViewCell 的按钮处理按钮触摸事件的最佳做法是什么?

                  What is best practice to handle a button touch event for a button of a custom UITableViewCell?

                  我的课程:MyViewController, MyCustomCell

                  我能想到三个选项:

                  第一个选项- 将按钮作为 MyCustomCell 的属性,然后在 MyViewController .m 文件中为其添加目标MyViewController 作为目标.

                  First option- Have the button as a property of MyCustomCell, and then add a target to it in the MyViewController .m file with MyViewController as the target.

                  MyViewController.m 文件

                  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
                  {
                      static NSString *CellIdentifier = @"customCell";
                  
                      MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                      if (cell == nil) {
                      cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
                  
                      [cell.theButton addTarget:self
                                         action:@selector(theButtonTapped:)
                               forControlEvents:UIControlEventTouchUpInside];
                      }
                  
                      // Configure the cell...    
                      [self configureCell:cell atIndexPath:indexPath];
                  
                      return cell;
                  }
                  
                  - (void)theButtonTapped:(UIButton *)sender
                  {
                      MyCustomCell *selectedCell = (MyCustomCell *)sender.superview;
                  
                      if (selectedCell) {
                          NSIndexPath *indexPath = [self.tableView indexPathForCell:selectedCell];
                          MyModel *selectedModel = [self.model objectAtIndex:indexPath.row]; 
                  
                          // do something with the model...
                      }
                  }
                  

                  第二个选项- 如果自定义单元格是在 IB 中制作的,将 nib File's Owner 设置为 MyViewController,在中实现 buttonTapped: 方法MyViewController 并将按钮的 Touch Up Inside 事件连接到 buttonTapped: 方法.

                  Second option- If the custom cell was made in IB, Set the nib File's Owner to be MyViewController, implement buttonTapped: method in MyViewController and connect the button's Touch Up Inside event to the buttonTapped: method.

                  第三个选项-如果自定义单元格不是在 IB 中制作的,请在 MyCustomCell .m 文件中使用 MyCustomCell 作为目标.
                  定义一个 MyCustomCellDelegate 添加 @property (nonatomic, assign) id;将委托给MyCustomCell,并在点击按钮时调用此委托.
                  创建单元格时将MyViewController设置为单元格的delegate,实现MyCustomCellDelegate协议.

                  Third option- if the custom cell wasn't made in IB, add a target to the button in the MyCustomCell .m file with MyCustomCell as the target.
                  Define a MyCustomCellDelegate add @property (nonatomic, assign) id<MyCustomCellDelegate> delegate to MyCustomCell and call this delegate when button tapped.
                  Set MyViewController as the cell's delegate when creating cells and implement the MyCustomCellDelegate protocol.

                  MyCustomCell.h 文件

                  @class MyCustomCell;  
                  
                  @protocol MyCustomCellDelegate <NSObject>
                  - (void)buttonTappedOnCell:(MyCustomCell *)cell;
                  @end
                  
                  @interface MyCustomCell : UITableViewCell
                  
                  @property (nonatomic, retain) UIButton *theButton;
                  @property (nonatomic, assign) id<MyCustomCellDelegate> delegate;
                  
                  @end
                  

                  MyCustomCell.m 文件

                  - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
                  {
                      self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
                      if (self) {
                          // Initialization code
                          self.theButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                          self.theButton.frame = CGRectMake(10,10,50,30);
                          [self addSubview:self.theButton];
                  
                          [self.theButton addTarget:self
                                             action:@selector(theButtonTapped:)
                                   forControlEvents:UIControlEventTouchUpInside];
                      }
                      return self;
                  }
                  
                  - (void)theButtonTapped:(UIButton *)sender
                  {
                      [self.delegate buttonTappedOnCell:self];
                  }
                  

                  MyViewController.m 文件

                  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
                  {
                      static NSString *CellIdentifier = @"customCell";
                  
                      MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                      if (cell == nil) {
                          cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
                  
                          cell.delegate = self;
                      }
                  
                      // Configure the cell...    
                      [self configureCell:cell atIndexPath:indexPath];
                  
                      return cell;
                  }
                  
                  - (void)buttonTappedOnCell:(MyCustomCell *)selectedCell
                  {
                      if (selectedCell) {
                          NSIndexPath *indexPath = [self.tableView indexPathForCell:selectedCell];
                          MyModel *selectedModel = [self.model objectAtIndex:indexPath.row];
                  
                          // do something with the model...
                      }
                  }
                  

                  推荐答案

                  将单元格的行存储为自定义按钮的 tag 属性.

                  Store the row of the cell as tag property of your custom button.

                  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
                  {
                      // bla bla bla
                      if (!cell)
                      {
                          //bla bla bla
                          [cell.yourButton addTarget:self selector:@selector(yourButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
                      }
                      // bla bla bla
                      cell.yourButton.tag = indexPath.row;
                  }
                  
                  -(void)yourButtonTapped:(id)sender
                  {
                      int tag = [(UIButton *)sender tag];
                      NSLog(@"tapped button in cell at row %i", tag);
                  }
                  

                  这篇关于Objective c - 为自定义 UITableViewCell 的按钮处理按钮触摸事件的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                • <tfoot id='M1l5Q'></tfoot>

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

                    <legend id='M1l5Q'><style id='M1l5Q'><dir id='M1l5Q'><q id='M1l5Q'></q></dir></style></legend>
                      <tbody id='M1l5Q'></tbody>

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