【发布时间】:2020-04-09 18:44:24
【问题描述】:
我在我的 uitableviewcontroller 中使用了不同的原型单元。它工作正常,除非我使用 UISwitches。如果我使用很多 UISwitches 并切换一个 switch,其他 Switch 也会被切换。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dict;
if (indexPath.row > 0) {
dict = [self.fieldsArray objectAtIndex:indexPath.row-1];
int nr = [[dict objectForKey:@"typeid"] intValue];
if (nr == 1) {
//cell with in texfield
} else if (nr == 2) {
static NSString *CellIdentifier2 = @"switch";
UITableViewCell *cell = [self.ficheTableView dequeueReusableCellWithIdentifier:CellIdentifier2 forIndexPath:indexPath];
cell.textLabel.text = @"";
[self createLabelWithCell:cell tag:1 dict:dict valuekey:@"label" indexpath:indexPath];
UISwitch *switch1 = (UISwitch *)[cell viewWithTag:2];
switch1.tag = [[dict valueForKey:@"id"] integerValue];
switch1.accessibilityIdentifier = @"value";
switch1.on = NO;
if ([dict valueForKey:@"value"] != [NSNull null]) {
if ([[dict valueForKey:@"value"] isEqualToString:@"1"]) {
NSLog(@"%ld %@",(long)switch1.tag, [dict valueForKey:@"value"]);
switch1.on = YES;
}
}
[switch1 addTarget: self action: @selector(flipSwitch:) forControlEvents:UIControlEventValueChanged];
return cell;
} else if (nr == 3) {
//cell with a textfield number only
}
...
}
- (IBAction)flipSwitch:(id)sender {
UISwitch *switch2 = (UISwitch*)sender;
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *aValue;
NSString *tag = [NSString stringWithFormat:@"%ld",(long)switch2.tag];
NSString *userid = [userDefaults stringForKey:kuserid];
if (switch2.on) aValue = @"1"; else aValue = @"0";
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
switch2.accessibilityIdentifier,@"field",
tag,@"id",
aValue,@"value",
@"BLABLA",@"appcode",
userid,@"userid",
@"updatefichenr", @"command",
nil];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:appDelegate.baseURLString parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@",error);
}];
}
如果我读取位于较低 UISwitches 的 FlipSwitch 中的标签,我也会得到其他 UISwitches 的标签。
我已经在这里查看过类似的问题,但我没有找到解决问题的方法。
【问题讨论】:
标签: objective-c xcode uitableview uiswitch