5

我对他们UITableViewUISwitchs意见。

表视图

当切换开关时,我想运行一个功能。该功能只记录开关是否打开以及开关已打开的行。我遇到的问题是,当我单击开关时,它不会记录正确的行,除非我在单击开关之前单击了该行。

我想我的问题是单击开关不会选择行。我怎样才能使它选择行或者我可以将 ID 添加到开关?

所以开关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" );
        }
4

7 回答 7

9

找到持有开关的单元格

UISwitch *switchInCell = (UISwitch *)sender;
UITableViewCell * cell = (UITableViewCell*) swithInCell.superview;

查找该单元格的索引路径

NSIndexPath * indexpath = [myTableView indexPathForCell:cell]

在你的情况下

 - (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" );
        }
于 2012-10-29T12:09:04.870 回答
9

您应该将方法中IndexPath.row的每个设置为标签SwitchcellForRowAtIndexPath

 switchView.tag= indexPath.row;

当开关值改变时,你会得到行号

- (void) switchChanged:(UISwitch *)sender {
  int rowIndex =[sender tag];
  //rowIndex you may use it further as you wanted.   

}

于 2012-10-29T12:09:14.893 回答
8

您可以检索在 tableview 中更改的 UISwitch 的 NSIndexPath。这与本文中已经回答的任何控件的想法相同:检测在 UITableView 中按下了哪个 UIButton

- (void) switchChanged:(id)sender 
{
    CGPoint switchPositionPoint = [sender convertPoint:CGPointZero toView:[self tableView]];
    NSIndexPath *indexPath = [[self tableView] indexPathForRowAtPoint:switchPositionPoint];
}

这将适用于 iOS7,以前我使用 [sender superview] 但现在返回 UITableViewCellScrollView 内的 UITableViewCellContentView。

于 2013-12-30T21:56:28.887 回答
5

我必须加倍mySwitch.superview.superview才能得到正确的单元格。

这是一个例子

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

}
于 2013-11-17T20:48:19.743 回答
1

我认为问题在于您使用 dequeueReusableCellWithIdentifier,并且您的所有单元格都具有相同的 id。

于 2012-10-29T11:59:57.190 回答
1

更好的方法是确定发送者所在的单元格。

- (UITableViewCell *)findCellForView:(UIView *)view
{
    for (; view != nil; view = view.superview)
        if ([view isKindOfClass:[UITableViewCell class]])
            return view;
    return nil;
}

一旦你有了这个方法。那么这是一个替换的[self.inputTableView indexPathForSelectedRow]问题

UITableViewCell *cell = [self findCellForView:sender];
NSIndexPath *indexPath = [self.inputTableView indexPathForCell:cell];
于 2012-10-29T12:06:26.187 回答
-1
#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
于 2013-12-02T07:52:26.640 回答