【问题标题】:UIButton touch not working properly in UITableView Custom cellUITableView 自定义单元格中的 UIButton 触摸无法正常工作
【发布时间】:2013-12-05 05:17:30
【问题描述】:

UIButton TouchUpInside 或任何其他事件在 UITableView 自定义单元格中无法正常工作。如果我按下角落的按钮,有时触摸会起作用。

其实我想随时重新排序单元格,所以我启用了编辑模式。

[_profileTblView setEditing:YES animated:YES];
_profileTblView.allowsSelectionDuringEditing=YES;

如果我禁用编辑模式,则 UIButton 事件正常工作,但我无法重新排序单元格。在这里,我希望两者一起工作。

那么,解决这个问题的方法是什么..

我的手机看起来像

Cell 的 Prototype 看起来像

【问题讨论】:

  • 您的单元格中是否有嵌入按钮的子视图?
  • 是的。按钮、标签、图像是 UIView 的子视图,而 UIView 是单元格的子视图
  • 你应该给 UIView 一个背景颜色,看看你的按钮是否完全在它的范围内。超出其超级视图的范围是按钮无响应的常见原因。
  • 我添加了单元原型。在该单元格的背景颜色为蓝色,UIview 的背景颜色为白色。如果我将编辑模式设置为 NO 那么它可以正常工作。
  • 试试这个。将视图的 clipsToBounds 属性设置为 YES,然后运行应用程序。当它处于编辑模式时,你还能看到整个按钮吗?

标签: ios objective-c uitableview uibutton


【解决方案1】:

在您的自定义单元结束时,

1) 制作自定义单元格的 .h 文件的协议,如

@protocol CustomCellDelegate
@optional
- (void)ButtonTappedOnCell:(id)sender;
@end

2) 在你的 .h 文件中设置委托,并在点击方法上设置自定义单元格的按钮

@property (nonatomic, strong) id<CustomCellDelegate> delegate;
- (IBAction)buttonTapped:(id)sender;

3) 在您的 .m 文件中,

- (IBAction)buttonTapped:(id)sender {
    [self.delegate ButtonTappedOnCell:self];
}

4) 将 IBAction 与 Button 链接

5) 取消选中自定义单元格的使用自动布局

在您的表格视图端,

1) 设置自定义单元的委托

@interface SomeViewController : UIViewController<UITableViewDelegate, UITableViewDataSource, CustomCellDelegate>

2) 在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中,设置单元格的委托cell.delegate=self

3)根据您的需要实现新制作的Delegate的功能,即

- (void)ButtonTappedOnCell:(id)sender
{
//your logic
}

希望对你有所帮助。

干杯

【讨论】:

    【解决方案2】:
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
         UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"" forIndexPath:indexPath];
        [cell.buttnName2 addTarget:self action:@selector(BtnAction:) forControlEvents:UIControlEventTouchUpInside];
        return Cell;
        }
    //action for button
            - (void)BtnAction:(id)sender//type 1 button action
            {
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-14
      • 1970-01-01
      • 2018-06-24
      • 2016-09-12
      相关资源
      最近更新 更多