首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITableViewCell中独有的UISwitch

UITableViewCell中独有的UISwitch
EN

Stack Overflow用户
提问于 2012-02-28 22:12:39
回答 1查看 796关注 0票数 0

我有一个用于行的带有一个UISwitch的UITableView。为此,请执行以下操作:

代码语言:javascript
复制
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中。但我不能做it...Someone能帮我吗?

谢谢,朋友们。

代码语言:javascript
复制
-(void) switchChanged:(id)sender{
    UISwitch *switchController = sender;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-28 23:09:25

一种简单的方法可以是这样的。

您希望该用户一次只能打开一行。为此,您需要跟踪两件事,第一件事是开关(我们可以通过cell的附件视图访问它),第二件事是打开的开关(假设我们使用tag属性)。

代码语言:javascript
复制
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

代码语言:javascript
复制
    #import <UIKit/UIKit.h>
#define NUMBER_OF_ROWS 10
        @interface SwitchTableController : UITableViewController {

            int selectedSwitchRow;
            NSMutableArray *switchArray;
        }

        @end

SwitchTableController.m代码

代码语言:javascript
复制
#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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9483488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档