gpt4 book ai didi

iphone - 自定义 accessoryView 中的 UISwitch

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:27:03 25 4
gpt4 key购买 nike


我有我的 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)];<br/>
...<br/>
mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0f, 22.0f, 94.0f, 27.0f)];<br/>
[myView addSubview:mySwitch];<br/>
self.accessoryView = myView;<br/>
[myView release];<br/>

一切似乎没问题,但有一个奇怪的行为。当我打开另一个 View Controller 并返回表格时,开关神秘地改变了......
这不是数据管理中的问题,而是仅在单元格重绘中...请帮帮我,我该怎么办?

提前致谢

最佳答案

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

-(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;
}

关于iphone - 自定义 accessoryView 中的 UISwitch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4975822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com