简体   繁体   English

自定义UITableViewCell中的按钮在iOS 7中不响应

[英]Button in custom UITableViewCell not responding in iOS 7

This is more or less a continuation of this post: Button in UITableViewCell not responding under ios 7 这或多或少是本文的延续: UITableViewCell中的按钮在ios 7下不响应

I am having the same exact issue and have tried every suggestion in the thread. 我有相同的确切问题,并尝试了线程中的每个建议。 Obviously I don't own that question so I can't edit it to give more info, and thus why I am posting this question now! 显然,我不拥有该问题,因此无法对其进行编辑以提供更多信息,因此,为什么我现在要发布此问题!

Problem: 问题:

I have VC nib that I load up that has a tableview in it that I resize based on how many rows are in it. 我加载的VC nib包含一个tableview,可根据其中的行数调整大小。 Each row is made from a custom uitableviewcell subclass using a nib file. 每行都是使用nib文件从自定义uitableviewcell子类生成的。 That class/nib has 4 buttons in it. 该班级/笔尖有4个按钮。 I can load this code up in iOS 6 or iOS 8 right now and it works perfectly. 我现在可以在iOS 6或iOS 8中加载此代码,并且可以完美运行。 I don't have a iOS 7 device so I'm bound to the simulator which is at 7.1 (which is the version I'm guess the user that reported this issue was using as well given it was today). 我没有iOS 7设备,所以我绑定到了7.1版的模拟器(我想是报告该问题的用户使用的版本以及今天的版本)。 Now in the simulator, and the user's phone, I can touch/click everything else on that VC except any of the buttons in the cells. 现在,在模拟器和用户的电话中,我可以触摸/单击该VC上除单元格中任何其他按钮之外的所有其他内容。 It's as if they had UserInteractionEnabled set to NO, but they don't and neither are any of their parent views (as I'll soon get into). 就像他们将UserInteractionEnabled设置为NO一样,但是它们没有,并且它们的任何父视图也不是(我将很快讲到)。

Tried solutions: 尝试过的解决方案:

-Completely recreating the nib from scratch both using and not using autolayout -使用和不使用自动布局完全重新创建笔尖

-Calling [self.contentView addSubview:button] in the awakeFromNib of the cell class -在单元格类的awakeFromNib中调用[self.contentView addSubview:button]

-Tried re-adding the buttons to the contentView at runtime with [self.contentView addSubView:button] -尝试在运行时使用[self.contentView addSubView:button]将按钮重新添加到contentView

-Have ensured four times over that every view in the hierarchy I can find that leads to these buttons have userInteractionEnabled set to YES. -确保四次遍历后,我可以发现导致这些按钮的层次结构中的每个视图的userInteractionEnabled设置为YES。 (including but not limited to the tableview itself, the cell, the contentView and when I added a "parent view" to the buttons that it was set as well) (包括但不限于表格视图本身,单元格, contentView以及当我向设置的按钮添加“父视图”时)

-Tried raising all the buttons with a parent view that contains nothing but the buttons -尝试使用父视图提高所有按钮,该视图只包含按钮

-All buttons are at the top(visually bottom) of the event stack(add and remove are the other two buttons): -所有按钮都位于事件堆栈的顶部(可视底部)(添加和删除是其他两个按钮): 层次结构清单

-Have set the table cell selection from single to none. -已将表格单元格选择从单个设置为无。

-I am not overriding layoutSubviews in my cell class -我没有在单元格类中覆盖layoutSubviews

-I can not move any views outside of the Content View as Interface Builder takes them completely out of the cell if I do that. -我无法将任何视图移出内容视图,因为如果这样做,Interface Builder会将其完全移出单元格。

-I have tried disabling the userInteractionEnabled on just the ContentView at runtime with no change -我尝试在运行时仅在ContentView上禁用userInteractionEnabled而不进行任何更改

-I tried putting in the cell creation code of the tableview [cell bringSubviewToFront:cell.button]; -我尝试放入[cell bringSubviewToFront:cell.button];的单元格创建代码[cell bringSubviewToFront:cell.button]; for the different buttons to the same result. 为不同的按钮获得相同的结果。

Hopefully Helpful Facts: 希望有帮助的事实:

-I tried setting all of the background colors of all of the views in the hierarchy to different colors so I could visually debug it at runtime... it looked exactly as expected. -我尝试将层次结构中所有视图的所有背景色设置为不同的颜色,以便可以在运行时直观地对其进行调试...它看起来完全符合预期。 No overlaps or coverings. 没有重叠或覆盖。 (This was limited to only views in the cell) (这仅限于单元格中的视图)

-Here is all of the settings for the TableView: -这是TableView的所有设置: TableView SS 1TableView SS 2

-I tried to load this in the new XCode 6 to use the visual debugger but the 7.1 simulator included with it actually ran the code perfectly so I could debug it... -我尝试将其加载到新的XCode 6中以使用可视调试器,但其中随附的7.1仿真器实际上完美地运行了代码,因此我可以对其进行调试...

-Here is the dequeueing code in the VC: -这是VC中的出队代码:

NiTGroupTimeCell* cell = (NiTGroupTimeCell*)[tableView dequeueReusableCellWithIdentifier:ident forIndexPath:indexPath];

-Here is the code in the viewDidLoad of the VC to set up the cell nib with the table(it's 2 because this is the from scratch one): -这是VC的viewDidLoad中的代码,用于用表格设置单元格笔尖(为2,因为这是从头开始的):

[self.timesTable registerNib:[UINib nibWithNibName:@"NiTGroupTimeCell2" bundle:nil]  forCellReuseIdentifier:@"GroupTime"];

-All connections were made via IB. -所有连接均通过IB建立。 These are all using IBAction or IBOutlet. 这些都使用IBAction或IBOutlet。

-I have NSLog statements in all button methods to test if they are actually called, but I have also tested with breakpoints. -我在所有按钮方法中都有NSLog语句来测试它们是否被实际调用,但是我还使用断点进行了测试。 All are never triggered in testing. 所有这些都不会在测试中触发。

-The only TableView delegate or datasource methods implemented are as follows: -唯一实现的TableView委托或数据源方法如下:

-(int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

-As per suggestion I took Revel to it and found that a mystery UILabel and UIImageView were in the view.... but as you can see their frames are all zeros so they shouldn't be getting in the way of anything so back to where we were I'm afraid: -根据建议,我对Revel进行了研究,发现视图中存在一个神秘的UILabelUIImageView ....但是您可以看到它们的帧全为零,所以它们不应该妨碍任何事情,所以请回到我怕在哪里:

层数

Revel的层次结构列表

UILabel frame: UILabel框架: UILabel框架

UIImageView frame: UIImageView框架: UIImageView框架

IIRC I counted this off as a Simulator bug before, but since it's happening on the user's device it must be an actual issue and it's holding up my pipeline so help would be GREATLY appreciated! IIRC我之前认为这是一个模拟器错误,但是由于它是在用户设备上发生的,因此这必须是一个实际问题,并且它阻碍了我的工作流程,因此将非常感谢您的帮助! Thanks in advance! 提前致谢!

PS I'm happy to post whatever, but because of all the shifting in debugging I didn't know exactly what people would want to see and I didn't want to overload this post because I knew it was going to be long with everything else. PS我很乐意发布任何内容,但是由于调试方面的所有变化,我不知道确切地人们会想看到什么,并且我不想超载这篇文章,因为我知道这将花费很多时间其他。

So apparently the issue was these lines of code(in diff format from my git diff output): 所以显然问题是这些代码行(来自我的git diff输出的diff格式):

--(int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
+-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

and

--(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
+-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

so yeah... seems that in iOS7+ if you have the "old" output types for these datasource methods it'll look fine but bork your tableview functionally... good times, but hey it's fixed now^^ hopefully this helps others. 所以,是的。。。似乎在iOS7 +中,如果您为这些数据源方法使用“旧”输出类型,它看上去会很好,但在功能上却使您的表视图...好时机,但是,现在它已修复^^希望对其他人有所帮助。

I would suspect that there's an issue with your custom UITableViewCell subclass, NiTGroupTimeCell , or with the IBAction connections from the nib to the tableViewCell subclass. 我怀疑您的自定义UITableViewCell子类NiTGroupTimeCell或从笔尖到tableViewCell子类的IBAction连接是否存在问题。
I also wonder about the extra empty UILabel and UIImageView. 我也想知道额外的空UILabel和UIImageView。 They sound like the default properties declared in UITableViewCell.h, UIImageView *imageView and UILabel *textLabel . 它们听起来像UITableViewCell.h, UIImageView *imageView UILabel *textLabelUILabel *textLabel声明的默认属性。 The fact that they're getting instantiated (and aren't nil) could be a clue as to the weird, unexpected behavior you're seeing. 它们被实例化(并且不为零)的事实可能是您所看到的怪异,意外行为的线索。
Do you have IBOutlets to the UIButtons? 您是否有IBOutlet到UIButton? What about changing properties (in code) for the buttons (such as background color) to make sure you actually are retaining them in the NiTGroupTimeCell . 如何更改按钮的属性(以代码形式)(例如背景色)以确保您实际上将它们保留在NiTGroupTimeCell

You're saying that you tried [self.contentView addSubview:button] but your button in your first image is inside a view inside contentView. 您是说您尝试过[self.contentView addSubview:button],但您第一张图片中的按钮位于contentView内部的视图内。 Try linking an IBOutlet to that view, then try [nameOfView addSubview]; 尝试将IBOutlet链接到该视图,然后尝试[nameOfView addSubview];

Throwing a wild guess as it looks like you have covered pretty much everything else. 似乎您已经涵盖了几乎所有其他内容,因此引发了一个疯狂的猜测。

You said: 你说:

I load up that has a tableview in it that I resize based on how many rows are in it 我加载了其中有一个tableview,我根据其中有多少行来调整大小

And you posted a screen shot saying the clip subviews is off on the UITableView 并且您发布了一个屏幕截图,说UITableView上的clip subviews已关闭

Are you setting the frame of the tableview incorrectly but its showing the cells anyway due the the subviews not being clipped? 您是否未正确设置tableview的框架,但由于未修剪子视图而仍然显示了单元格?

Load up reveal again and check the height of the UITableView 再次加载reveal并检查UITableView的高度

Not sure if it is the case here. 不知道是否是这种情况。 But if you name the custom cell outlets imageView , textLabel or any of the "built-in" UITableViewCell 's properties you will get weird results and behavior. 但是,如果你的名字自定义单元格网点imageViewtextLabel或任何“内置”的UITableViewCell的属性,你会得到奇怪的结果和行为。

You need to check couple of things: 您需要检查几件事:

  1. Make sure all the parent views of the button have User Interaction enabled. 确保按钮的所有父视图都启用了“ User Interaction
  2. Check AutoSizing in the size inspector and make sure the button lies inside the view so it could receive touch events otherwise touch events will get ignore. 在大小检查AutoSizing中检查“ AutoSizing调整大小”,并确保按钮位于视图内,以便它可以接收触摸事件,否则触摸事件将被忽略。 You can verify it by changing the colour of the view. 您可以通过更改视图的颜色来进行验证。 Changing colour or NSLog the frame of button and parent views will help you to troubleshoot if this is the problem. 更改颜色或NSLog按钮和父视图的框架将帮助您解决此问题。
  3. Try to make things simpler and don't addSubView programmatically, if designing from IB. 如果是从IB设计,​​请尝试使事情更简单,不要addSubView编程方式添加addSubView
  4. Make sure some component is not overlapping the button with a clearColor background color. 确保某些组件没有与clearColor背景色重叠按钮。

What worked for me is this: 对我有用的是:

self.contentView.userInteractionEnabled = NO;

I called this after loading my custom cell from nib / storyboard. 我从笔尖/情节提要加载自定义单元格后调用了此命令。 Without the userinteractionEnabled set to NO, my buttons haven't been responding to touches somehow. 没有将userinteractionEnabled设置为NO,我的按钮就无法以某种方式响应触摸。 My contentView has received all touches and did not forward them to my buttons. 我的contentView已收到所有触摸,并且没有将它们转发给我的按钮。

I've seen code where 我在哪里看过代码

[cell bringSubviewToFront:button]

has been used as a workaround, for me it did not solve the issue. 已被用作解决方法,对我而言,它并没有解决问题。

I had a simillar issue, while adding Custom Acions based on Guestures. 我在添加基于Guestures的自定义Acions时遇到了类似的问题。

I was adding Action Button on runtime in Inherited class of UITableViewCell The bahaviour was if I add buttons outside the visible rect, after animating buttons inside, I was unable to click / tap. 我在UITableViewCell的Inherited class中的运行时上添加了Action Button。行为是,如果我在可见的rect之外添加按钮,则在对其中的按钮设置动画之后,我无法单击/点击。 But in My Case I was able to Tap / Click if buttons were added in visible rect. 但是在“我的情况”下,如果在可见矩形中添加了按钮,我便可以轻按/单击。

What Worked for me, I added those buttons in View instead of ContentView of UITableViewCell, then animated only Content View. 对我有用的是,我在View添加了这些按钮,而不是UITableViewCell的ContentView ,然后仅对Content View进行了动画处理。 You may try somthing simillar 您可以尝试类似的东西

-- Vishal -维沙尔

You've said that there are no TouchRecognizers(TapRecognizers?), but I think you should double-check that, and look not only for touch recognizers but for any recognizers in controller that uses that cell,even added to self.view/tableView. 您已经说过没有TouchRecognizers(TapRecognizers?),但是我认为您应该仔细检查一下,不仅要寻找触摸识别器,还要寻找使用该单元格的控制器中的任何识别器,甚至添加到self.view / tableView 。 I was recently trying to find out why cells don't select(delegate method wasn't called) only to find out that I've added 2 gesture recognizers(in code, those were necessary for other things, but I had to do that in other way) that would prevent cell selection. 我最近试图找出为什么不选择单元格(未调用delegate方法)只是为了发现我添加了2个手势识别器(在代码中,这些手势识别器是其他事情所必需的,但是我必须这样做)以其他方式)将阻止单元格选择。

Also the sign of it may be that if you hold button long enough(put breakpoint there so you can make it easier), action will fire. 同样的迹象可能是,如果您按住按钮足够长的时间(在那里放置断点,这样您可以更轻松地进行操作),动作就会触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM