自定义accessoryView中的UISwitch
我有我的 UITableViewCell
子类,其中我以这种方式使用 UISwitch
作为 accessoryView
:mySwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
self.accessoryView = mySwitch;
一切都好!该应用程序运行良好。
现在我需要在开关上方添加一些UIImageView
,所以我想“好吧,让我们制作一个自定义的accessoryView!”:
<代码> UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 10.0f, 100.0f, 60.0f)];
...
mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0f, 22.0f, 94.0f, 27.0f)];
[myView addSubview:mySwitch];
self.accessoryView = myView;
[myView 发布];
一切似乎都很好,但有一个奇怪的行为。当我打开另一个视图控制器并返回到表时,开关神秘地改变了......
这不是数据管理的问题,而只是单元格重绘的问题...... 请帮帮我,我能做什么?
提前致谢
I have my UITableViewCell
subclass in which I use an UISwitch
as accessoryView
in this way:mySwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
self.accessoryView = mySwitch;
and all is ok! The app works fine.
Now I need to add some UIImageView
above the switch, so I thought "Well, let's make a custom accessoryView!":
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 10.0f, 100.0f, 60.0f)];
...
mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0f, 22.0f, 94.0f, 27.0f)];
[myView addSubview:mySwitch];
self.accessoryView = myView;
[myView release];
All seems to be ok, but there is a strange behavior. When I open another viewcontroller and get back to the table the switches are mysteriously changed...
It's not a problem in data management, but only in the cell redraw...
Please, help me, what can I do?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发生这种情况是因为细胞被重复使用。因此,如果您将子视图添加到单元格的内容视图中,然后在另一行中重用该单元格后,该视图将出现在该行中。避免这种情况的最佳方法是将 NSArray 保存在包含所有自定义视图(带有子视图)的表数据源(通常是视图控制器)中。然后你可以这样做:
This happens because cells are reused. So if you added subview to cell's content view and then after that cell is reused in another row, that view will appear in that row. The best way to avoid this, is to hold NSArray in your table's datasource (it's usually your view controller) that contains all the custom views (with subviews). Then you can do like this:
嗯...我发现了问题...它没有链接到表重绘...
在
UIControlEventValueChanged
上,选择器以这种方式检索开关值和单元格indexPath.row
:UISwitch *tempSwitch = (UISwitch *)sender;
UITableViewCell *cell = (UITableViewCell *)[tempSwitch superview];
然后它更新保存在 NSMutableArray 中的相应对象(数据逻辑类)(tableview 的数据源)
但现在切换的不是accessoryView,而是accessoryView的子视图,因此对象以不可预测的方式更新。我用第二条
superview
消息解决了这个问题。为我的错误感到抱歉并感谢所有人...
Emh... I found the problem... it was not linked to table redraw...
On
UIControlEventValueChanged
the selector retrieves the switch value and the cellindexPath.row
in this way:UISwitch *tempSwitch = (UISwitch *)sender;
UITableViewCell *cell = (UITableViewCell *)[tempSwitch superview];
and then it updates the corresponding object (of a data logic class) saved in a
NSMutableArray
(the datasource of the tableview)But now the switch is a not the accessoryView, but a subview of the accessoryView, so the object is updates in a unpredictable way. I solved with a second
superview
message.Sorry for my mistake and thanks to all...