我已经将一个switchview作为附件视图添加到UItableviewcell,我在表中有5个条目,即,5个单元格和那个行中的开关..我给每个开关一个标签值,它等于indexpath .row(所以第一个开关为0,第二个为1 ......第五个开关为4)
UISwitch *switchView = [[UISwitch alloc]initWithFrame:CGRectZero];
cell.accessoryView = switchView;
[switchView setOn:NO animated:NO];
[switchView setTag:indexPath.row];
我已经给出了切换的目标方法,当切换的值改变时,会调用目标方法。
[switchView addTarget:self action:@selector(switchTimer:) forControlEvents:UIControlEventValueChanged];
在这个基于Switch I事件发生变化的目标方法中,尝试构建一个计数等于交换机数量的数组。 例如:如果开关0和3为ON且开关1,2,4为OFF,则阵列看起来像这样
A = {"ON","OFF","OFF","ON","OFF"}
以下是我用来实现此目的的方法:
-(void)switchTimer:(UISwitch *)senderSwitch{
NSLog(@"Sender Switch Tag is %d",senderSwitch.tag);
for(int i=0;i<[array_Timers count];i++){//Start for loop
senderSwitch.tag = i;
if(senderSwitch.on){
NSLog(@"%@ is ON",[array_Timers objectAtIndex:i]);
//Here I add an object to the array with value "ON"
}
else{
NSLog(@"%@ is OFF",[array_Timers objectAtIndex:i]);
//Here I add an object to the array with value "OFF"
}
}//End for loop
}
问题是每次开关都开启,比如开关2(标签1)所有开关都会得到标签值1,即使我借助他们的标签值迭代所有开关。我想知道所有开关的状态,即使只有1个开关的值发生变化..但是没有发生,所有开关都得到标签值1,我得到一个这样的数组
A = {"ON","ON","ON","ON","ON"}//when switch 2 is ON
A = {"OFF","OFF","OFF","OFF","OFF"}//when switch 2 is OFF
有人可以帮我找出问题所在吗? 提前致谢
编辑:完整的cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [timersTableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType=UITableViewCellAccessoryNone;
UISwitch *switchView = [[UISwitch alloc]initWithFrame:CGRectZero];
cell.accessoryView = switchView;
[switchView setOn:NO animated:NO];
[switchView setTag:indexPath.row];
[switchView addTarget:self action:@selector(switchTimer:) forControlEvents:UIControlEventValueChanged];
[switchView release];
UIView *v = [[[UIView alloc] init] autorelease];
v.backgroundColor = [UIColor orangeColor];
cell.selectedBackgroundView = v;
}
UserTimer *usertimer = [array_Timers objectAtIndex:indexPath.row];//irrelevant here
[cell.textLabel setText:usertimer.alias];
return cell;
}
答案 0 :(得分:1)
如果你想知道所有开关的状态,即使只有1个开关的值被改变,试试这个,
-(void)switchTimer:(UISwitch *)senderSwitch{
NSLog(@"Sender Switch Tag is %d",senderSwitch.tag);
for(int i=0;i<[array_Timers count];i++){//Start for loop
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UISwitch *switch = (UISwitch *)[cell.contentView viewWithTag:indexPath.row];
if(switch.on){
NSLog(@"%@ is ON",[array_Timers objectAtIndex:i]);
//Here I add an object to the array with value "ON"
}
else{
NSLog(@"%@ is OFF",[array_Timers objectAtIndex:i]);
//Here I add an object to the array with value "OFF"
}
}//End for loop
}
答案 1 :(得分:1)
当您使用for循环时,开关的值会发生变化。所以你不需要在这里使用for循环。相反,你只需要替换当前开关索引处的对象。
-(void)switchTimer:(UISwitch *)senderSwitch {
NSString *currentValue = @"";
NSInteger index = senderSwitch.tag;
if(senderSwitch.on){
currentValue = @"ON";
}
else {
currentValue = @"OFF";
}
[array_Timers replaceObjectAtIndex: index withObject: currentValue];
}
答案 2 :(得分:0)
您需要做的是:在加载表之前,主要使用值为OFF初始化Array。对于Ex:
-(Void) ViewDidLoad {
array_Timers = [NSArray alloc]... // Add the Objects .
}
在UITableView委托:(不要尝试将Tag设置为0到任何元素)
UISwitch *switchView = [[UISwitch alloc]initWithFrame:CGRectZero];
cell.accessoryView = switchView;
[switchView setOn:NO animated:NO];
[switchView setTag:indexPath.row+25]; //don't try to set Tag as 0 to any elements
[switchView addTarget:self action:@selector(switchTimer:) forControlEvents:UIControlEventValueChanged];
然后根据事件
替换值-(void)switchTimer:(UISwitch *)senderSwitch{
[array_Timers replaceObjectAtIndex:0 withObject:[[NSNumber numberWithBool:senderSwitch - 25]];
NSLog (@" Values %@", array_Timers );
}
答案 3 :(得分:0)
试试这个......
-(void)switchTimer:(UISwitch *)senderSwitch{
NSLog(@"Sender Switch Tag is %d",senderSwitch.tag);
for (UISwitch *mySwitch in switchesArray) {
if(mySwitch.on){
NSLog(@"%@ is ON",mySwitch);
//Here I add an object to the array with value "ON"
}
else{
NSLog(@"%@ is OFF",mySwitch);
//Here I add an object to the array with value "OFF"
}
}//End for loop
注意 - 如果您重复使用开关并且array_Timers中的顺序与表格视图中的顺序不一致,则应首先按标记对开关进行排序。