Objective-C-UI打开UITableViewCell并激活其他开关

Objective-C-UI打开UITableViewCell并激活其他开关,objective-c,uitableview,uiswitch,Objective C,Uitableview,Uiswitch,我的UITableViewCell(自定义单元格)上有一个UISwitch,它可以在其他单元格上激活开关而无需任何触摸,我真的不知道为什么(我的单元格上没有任何代码激活开关) (来源:) . (来源:) 代码: //创建单元 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIden

我的UITableViewCell(自定义单元格)上有一个UISwitch,它可以在其他单元格上激活开关而无需任何触摸,我真的不知道为什么(我的单元格上没有任何代码激活开关)


(来源:)
.
(来源:)

代码:

//创建单元

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    CustomClassCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[CustomClassCell alloc] initWithStyle:UITableViewCellStyleDefault
                                  reuseIdentifier:cellIdentifier];
    }

    NSDictionary *classDetail = [self.classesArray objectAtIndex:indexPath.row];
    cell.textLabel.text = [classDetail objectForKey:@"nome"];
    NSString *detailText = [NSString stringWithFormat:@"Professor %@ às %@. %@ minutos",[classDetail objectForKey:@"professor"], [classDetail objectForKey:@"hora"], [classDetail objectForKey:@"duracao"] ];

    NSString *weekDayString = [[self.viewData objectForKey:@"@attributes"] objectForKey:@"index"];

    cell.weekDay = [weekDayString integerValue];

    cell.detailTextLabel.text = detailText;
    
    cell.cellData = classDetail;

    return cell;
}

//CustomClassCell.m

//This action is linked via storyboard to Value Changed on my UISwitch:
-(IBAction)isOn:(id)sender{
    UISwitch *switcher = sender;
    BOOL isOn = [switcher isOn];
    (isOn == YES ? [self activeNotification] : [self deactiveNotification]);
}

-(void)activeNotification{
    NSString *classTimeString = [self.cellData objectForKey:@"hora"];
    NSArray *hourAndMinutes = [classTimeString componentsSeparatedByString:@":"];
    int hour = [[hourAndMinutes objectAtIndex:0] intValue];
    int minutes = [[hourAndMinutes objectAtIndex:1] intValue];

    UILocalNotification *locNot = [[UILocalNotification alloc] init];

    NSDate *now   = [NSDate date];

    NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit |
                       NSDayCalendarUnit) fromDate: now];
    now = [gregorian dateFromComponents:components];

    NSDate *alarmDate = [self getDayFromNumber:self.weekDay fromToday:now];

    alarmDate = [alarmDate dateByAddingTimeInterval:60*60*hour + 60*minutes];

    if (([now compare:alarmDate] == NSOrderedAscending) || ([now compare:alarmDate] == NSOrderedSame) ){
        alarmDate = [alarmDate dateByAddingTimeInterval:60*60*hour + 60*(minutes - 30)];
    } else {
        alarmDate = [alarmDate dateByAddingTimeInterval:60*60*hour + 60*(minutes - 30) + 60*60*24*7];
    }

    locNot.fireDate = alarmDate;
    [[UIApplication sharedApplication] scheduleLocalNotification: locNot];
}

-(void)deactiveNotification{
    //TODO: implement deactivation   
}


-(NSDate *)getDayFromNumber:(NSInteger)number fromToday:(NSDate *)today{
    number++;

    NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit
                                                   fromDate:today];


    NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
    [componentsToSubtract setDay: 0 - ([weekdayComponents weekday] - number)];

    NSDate *beginningOfWeek = [gregorian dateByAddingComponents:componentsToSubtract
                                                     toDate:today options:0];

    NSDateComponents *components =
    [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit |
                       NSDayCalendarUnit) fromDate: beginningOfWeek];
    beginningOfWeek = [gregorian dateFromComponents:components];

    return beginningOfWeek;
}
有什么想法吗


问候

这是一个单元重用问题,因为您正在重用单元,但没有配置交换机状态(在配置单元文本时应执行此操作)


这要求您在开关更改时更新源数据(
classDetail
/
cellData
),以便以后返回单元格时使用该信息。这既可以防止更改的重复,也可以确保单元格从屏幕上滚下后,选定的开关不会变为取消选择的开关。

这很好!谢谢你的帮助!