自定义accessoryView中的UISwitch

发布于 2024-10-17 05:18:47 字数 746 浏览 4 评论 0原文


我有我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

左秋 2024-10-24 05:18:47

发生这种情况是因为细胞被重复使用。因此,如果您将子视图添加到单元格的内容视图中,然后在另一行中重用该单元格后,该视图将出现在该行中。避免这种情况的最佳方法是将 NSArray 保存在包含所有自定义视图(带有子视图)的表数据源(通常是视图控制器)中。然后你可以这样做:

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath {
    NSInteger row = indexPath.row;
    static NSString* cellIdentifier = @"Trololo";

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];
    if( !cell ) {       
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier: cellIdentifier] autorelease];
    }

    [[cell.contentView subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)];
    [cell.contentView addSubview: [_your_cell_views objectAtIndex: row]];

    return cell;
}

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:

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath {
    NSInteger row = indexPath.row;
    static NSString* cellIdentifier = @"Trololo";

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];
    if( !cell ) {       
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier: cellIdentifier] autorelease];
    }

    [[cell.contentView subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)];
    [cell.contentView addSubview: [_your_cell_views objectAtIndex: row]];

    return cell;
}
凹づ凸ル 2024-10-24 05:18:47

嗯...我发现了问题...它没有链接到表重绘...
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 cell indexPath.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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文