gpt4 book ai didi

iphone - UITableViewCell 独有的 UISwitch

转载 作者:行者123 更新时间:2023-11-29 04:47:00 24 4
gpt4 key购买 nike

我有一个 UITableView,其中有一个用于行的 UISwitch。要做到这一点:

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 置于关闭状态。但我做不到...有人可以帮助我吗?

谢谢 friend 。

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

最佳答案

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

您希望该用户一次只能打开一行。为此,您需要跟踪两件事,首先是开关(我们可以通过单元格的附件 View 访问它),其次是打开的开关。(为此假设我们使用标签属性)。

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;
}

关于iphone - UITableViewCell 独有的 UISwitch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9483488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com