xcode 单击 UISwitch 时选择 UITableView 的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13121139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select UITableView's row when clicking on UISwitch
提问by Will
I have a UITableView
with UISwitchs
on them.
我有一个UITableView
与UISwitchs
他们。
When the switch is toggled I want to run a function. The function just logs If the switch is on or off and the row that the switch has been changed on. The problem that im having is that when I click on the switch it does not log the correct row unless I have clicked on that row before clicking the switch.
当切换开关时,我想运行一个功能。该函数只记录开关是否打开以及开关已更改的行。我遇到的问题是,当我单击开关时,它不会记录正确的行,除非我在单击开关之前单击了该行。
I guess my problem is that clicking the switch does not select the row. How can I make it so that it either selects the row or can I add the ID to the switch?
我想我的问题是单击开关不会选择该行。我怎样才能让它选择行或者我可以将 ID 添加到开关?
So switch ID "1" is "ON".
所以开关ID“1”是“ON”。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"POICell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//set the cell text to the catName
cell.textLabel.text = [self.catNames objectAtIndex:[indexPath row]];
//add switch
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchView;
[switchView setOn:YES animated:NO];
[switchView addTarget:self action:@selector(switchChanged: ) forControlEvents:UIControlEventValueChanged];
// Configure the cell...
return cell;
}
- (void) switchChanged:(id)sender {
NSString *StrCatID =[[NSString alloc]init];
StrCatID = [self.catIDs objectAtIndex:[self.inputTableView indexPathForSelectedRow].row];
UISwitch* switchControl = sender;
NSLog( @"The switch for item %@ is %@",StrCatID, switchControl.on ? @"ON" : @"OFF" );
}
回答by Bala
To find the cell that holds the switch
找到持有开关的单元格
UISwitch *switchInCell = (UISwitch *)sender;
UITableViewCell * cell = (UITableViewCell*) swithInCell.superview;
To find the indexpath of that cell
找到该单元格的索引路径
NSIndexPath * indexpath = [myTableView indexPathForCell:cell]
In your case
在你的情况下
- (void) switchChanged:(id)sender {
UISwitch *switchInCell = (UISwitch *)sender;
UITableViewCell * cell = (UITableViewCell*) swithInCell.superview;
NSIndexPath * indexpath = [myTableView indexPathForCell:cell]
NSString *strCatID =[[NSString alloc]init];
strCatID = [self.catIDs objectAtIndex:indexpath];
NSLog( @"The switch for item %@ is %@",StrCatID, switchInCell.on ? @"ON" : @"OFF" );
}
回答by Kamar Shad
You should set the IndexPath.row
as a Tag to each Switch
in cellForRowAtIndexPath
Method
您应该在方法IndexPath.row
中将每个设置为标签Switch
cellForRowAtIndexPath
switchView.tag= indexPath.row;
And when switch value change .you'll get the Row number
当开关值改变时,你会得到行号
- (void) switchChanged:(UISwitch *)sender {
int rowIndex =[sender tag];
//rowIndex you may use it further as you wanted.
}
}
回答by NSDestr0yer
You can retrieve the NSIndexPath for the UISwitch that was changed in the tableview. This is the same idea for any control as already answered in this post : Detecting which UIButton was pressed in a UITableView
您可以检索在 tableview 中更改的 UISwitch 的 NSIndexPath。这与本文中已经回答的任何控件的想法相同:检测 UITableView 中按下了哪个 UIButton
- (void) switchChanged:(id)sender
{
CGPoint switchPositionPoint = [sender convertPoint:CGPointZero toView:[self tableView]];
NSIndexPath *indexPath = [[self tableView] indexPathForRowAtPoint:switchPositionPoint];
}
This will work for iOS7, previously I used [sender superview] but that now returns a UITableViewCellContentView inside of a UITableViewCellScrollView.
这适用于 iOS7,以前我使用 [sender superview] 但现在返回 UITableViewCellScrollView 内的 UITableViewCellContentView。
回答by Pushpak
I had to do double mySwitch.superview.superview
to get the proper cell.
我必须加倍mySwitch.superview.superview
才能获得正确的单元格。
Here's an example
这是一个例子
- (void)switchToggle:(UISwitch *)mySwitch
{
UITableViewCell *cell = (UITableViewCell *)mySwitch.superview.superview;
NSIndexPath *indexpath = [self.tableView indexPathForCell:cell];
NSLog(@"toggle section %d rowID %d", indexpath.section, indexpath.row);
}
回答by edzio27
I think the problem is that you use dequeueReusableCellWithIdentifier, and all of your cell has the same id.
我认为问题在于您使用 dequeueReusableCellWithIdentifier,并且您的所有单元格都具有相同的 ID。
回答by Jeffery Thomas
A better way to do this is determine which cell the sender is in.
一个更好的方法是确定发件人所在的单元格。
- (UITableViewCell *)findCellForView:(UIView *)view
{
for (; view != nil; view = view.superview)
if ([view isKindOfClass:[UITableViewCell class]])
return view;
return nil;
}
Once you have this method. then it's a matter of replacing [self.inputTableView indexPathForSelectedRow]
with
一旦你有了这个方法。那么它的更换的问题[self.inputTableView indexPathForSelectedRow]
与
UITableViewCell *cell = [self findCellForView:sender];
NSIndexPath *indexPath = [self.inputTableView indexPathForCell:cell];
回答by user3040434
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (retain, nonatomic) IBOutlet UITableView *tableview;
@end
@interface ViewController ()
{
NSMutableArray *arr;
UITableViewCell* aCell;
UISwitch *myswitch;
}
#import "ViewController.h"
@interface ViewController ()
{
NSMutableArray *arr;
UITableViewCell* aCell;
UISwitch *myswitch;
}
@end
@implementation ViewController
@synthesize tableview;
- (void)viewDidLoad
{
[super viewDidLoad];
arr = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return [arr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
aCell = [tableview dequeueReusableCellWithIdentifier:@"SwitchCell"];
if( aCell == nil )
{
aCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SwitchCell"];
aCell.textLabel.text = [arr objectAtIndex:indexPath.row];
myswitch = [[UISwitch alloc]initWithFrame:CGRectZero];
aCell.accessoryView = myswitch;
[myswitch setOn:YES animated:YES];
}
switch (indexPath.row)
{
case 0:
{
[myswitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
}
break;
case 1:
{
[myswitch addTarget:self action:@selector(switchChanged1:) forControlEvents:UIControlEventValueChanged];
}
break;
}
return aCell;
}
- (void) switchChanged:(id)sender {
UISwitch *aswitch = sender;
if (aswitch.on==YES) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"hello" message:@"okfine" delegate:self cancelButtonTitle:@"done" otherButtonTitles:nil, nil];
[alert show];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"no1" message:@"notfine" delegate:self cancelButtonTitle:@"Returnback" otherButtonTitles:nil, nil];
[alert show];
}
}
- (void) switchChanged1:(id)sender {
UISwitch *aswitch1 = sender;
if (aswitch1.on==YES) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"second" message:@"okfine" delegate:self cancelButtonTitle:@"done" otherButtonTitles:nil, nil];
[alert show];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"no2" message:@"notfine" delegate:self cancelButtonTitle:@"Returnback" otherButtonTitles:nil, nil];
[alert show];
}
}
- (void)dealloc {
[tableview release];
[super dealloc];
}
@end