如何在 UITableView
单元格上嵌入 UISwitch
?示例可以在设置菜单中看到。
我目前的解决方案:
UISwitch *mySwitch = [[[UISwitch alloc] init] autorelease];
cell.accessoryView = mySwitch;
最佳答案
将其设置为附件 View 通常是要走的路。您可以在 tableView:cellForRowAtIndexPath:
中设置它。您可能希望在切换开关时使用目标/ Action 来执行某些操作。像这样:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
switch( [indexPath row] ) {
case MY_SWITCH_CELL: {
UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:@"SwitchCell"];
if( aCell == nil ) {
aCell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"SwitchCell"] autorelease];
aCell.textLabel.text = @"I Have A Switch";
aCell.selectionStyle = UITableViewCellSelectionStyleNone;
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
aCell.accessoryView = switchView;
[switchView setOn:NO animated:NO];
[switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[switchView release];
}
return aCell;
}
break;
}
return nil;
}
- (void)switchChanged:(id)sender {
UISwitch *switchControl = sender;
NSLog( @"The switch is %@", switchControl.on ? @"ON" : @"OFF" );
}
https://stackoverflow.com/questions/3770019/
相关文章:
ios - 如何使用 UISegmentedControl 切换 View ?
iphone - 如何找到 malloc "double free"错误的原因?
iphone - 如何为 View 或图像沿弯曲路径的移动设置动画?
objective-c - .h 和 .m 文件中的@interface 定义之间的区别
objective-c - ARC : strong or retain? 的@property 定
ios - 为什么masksToBounds = YES会阻止CALayer阴影?
objective-c - 如何向 objc_exception_throw 添加断点?
ios - NSOperation 和 NSOperationQueue 工作线程与主线程