簡體   English   中英

UITableViewCell中專有的UISwitch

[英]UISwitch exclusive in UITableViewCell

我有一個帶有一個UISwitch行的UITableView。 去做吧:

UISwitch *switchController = [[UISwitch alloc] initWithFrame:CGRectZero];
if([valor isEqualToString:@"true"])  
    [switchController setOn:YES animated:YES];
else
    [switchController setOn:NO animated:YES];

switchController.tag = row;
[switchController addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = switchController;
[switchController release];

好的,我要排他一行,當我觸摸一個UISwitch時,我需要將所有其他UISwitch都置於OFF狀態。 但是我做不到...有人可以幫助我嗎?

謝謝朋友。

-(void) switchChanged:(id)sender{
    UISwitch *switchController = sender;
}

一種簡單的方法可能是這樣的。

您希望該用戶一次只能打開一行。 為此,您需要跟蹤兩件事:首先是開關(我們可以通過單元格的附件視圖訪問它),其次是打開的開關(為此我們使用tag屬性)。

So we will need:
>One variable to keep track of which row's switch is on.
>Array which will hold the of the switches.

我用以下代碼創建了一個示例應用程序:(我將行數作為常量設為10)

SwitchTableController.h

    #import <UIKit/UIKit.h>
#define NUMBER_OF_ROWS 10
        @interface SwitchTableController : UITableViewController {

            int selectedSwitchRow;
            NSMutableArray *switchArray;
        }

        @end

SwitchTableController.m代碼

#import "SwitchTableController.h"

@implementation SwitchTableController

#pragma mark -
#pragma mark View lifecycle

- (void)dealloc {
    [switchArray release];
    [super dealloc];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    selectedSwitchRow = -1;
    if (switchArray == nil)
    {
        switchArray  = [[NSMutableArray alloc] initWithCapacity:10];
    }
    for (int i=0; i<NUMBER_OF_ROWS; i++)
    {
        UISwitch *switchController = [[UISwitch alloc] initWithFrame:CGRectZero];
        [switchController setOn:NO animated:YES];
        switchController.tag = i;
        [switchController addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
        [switchArray addObject:switchController];
        [switchController release];
    }
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return NUMBER_OF_ROWS;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.accessoryView = [switchArray objectAtIndex:indexPath.row];
   return cell;
}

-(void) switchChanged:(UISwitch *)sender
{
    if (selectedSwitchRow >= 0 && selectedSwitchRow<[switchArray count] && selectedSwitchRow != sender.tag)
    {
        UISwitch *tempSwitch = [switchArray objectAtIndex:selectedSwitchRow];
        [tempSwitch setOn:NO animated:YES];
        [self.tableView reloadData];
    }
    selectedSwitchRow = sender.tag;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM