【发布时间】:2016-10-24 08:15:21
【问题描述】:
我在表格视图的一个单元格中有一个切换按钮。之前在 iOS 9 中,一切正常,点击切换按钮后会转到 action 方法。但更新到 iOS 10 后,永远不会调用 action 方法。有没有人遇到过类似的问题?
细胞.m
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectZero];
[bgView setImage:[[UIImage imageNamed:@"customcell.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)]];
[self setBackgroundView:bgView];
UIImageView *selectedBgView = [[UIImageView alloc] initWithFrame:CGRectZero];
[selectedBgView setImage:[[UIImage imageNamed:@"cellselect.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)]];
[self setSelectedBackgroundView:selectedBgView];
nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 13, 250, 40)];
[nameLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleRightMargin];
[nameLabel setFont:[UIFont boldSystemFontOfSize:16]];
[nameLabel setBackgroundColor:[UIColor clearColor]];
[nameLabel setTextColor:[UIColor whiteColor]];
[nameLabel setNumberOfLines:0];
[self.contentView addSubview:nameLabel];
switchButton = [[UISwitch alloc] initWithFrame: CGRectMake(230, 17, 100, 30)];
[switchButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
[switchButton addTarget:self action:@selector(switchActivated:) forControlEvents:UIControlEventTouchUpInside];
[switchButton setHidden:YES];
[self.contentView addSubview:switchButton];
}
return self;
}
动作方法:
- (void) switchActivated:(id)sender
{
if(switchButton.on)
{
[selectedItem setObject:@"YES" forKey:@"valueEntry"];
}
else
{
[selectedItem setObject:@"NO" forKey:@"valueEntry"];
}
}
ViewController.m 索引路径处的行单元格:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewCell *tableViewCell = [aTableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"homeReportCustomizationCell_%d",indexPath.row]];
if(!tableViewCell)
{
tableViewCell = [[Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"Cell_%d",indexPath.row]];
}
...........
}
【问题讨论】:
-
删除这个`[switchButton setHidden:YES];`并检查
-
这不是问题,我有其他方法来控制这个按钮的显示或隐藏,现在我可以点击按钮但它没有转到操作方法
-
什么是 self.contentView ?它应该是 cell.contentView。请检查一下。
-
@DavidKong 所以你的
switchActivated方法甚至没有被调用还是什么?尝试在方法或断点中添加 NSLog。因为我认为方法应该得到一个电话。还可以尝试将控制事件更改为 -UIControlEventValueChanged -
我发现了这个问题,这个项目使用 ios6.1 作为基础 SDK,部署目标是 6.1,在我全部更改为 7.0 后一切正常...不知道为什么,因为以前可以工作对 10.0 以下的所有设备都很好,更新到 iOS 10 后出现了这个奇怪的问题。这个项目太旧了,需要做一些改进。感谢您的所有 cmets。
标签: ios objective-c ios10 uiswitch