Question

I have a UISwitch on my UITableViewCell (Custom cell) that is activating switches on other cells without any touch, I don't really know why (I don't have any code activating switches on my cells).

Valid XHTML
(source: abril.com)
. Valid XHTML
(source: abril.com)
.

Code:

//Creating cells

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

Any ideas?

Regards!

Was it helpful?

Solution

This is a cell reuse problem because you are reusing cells but you aren't configuring the switch status (which should be done when you configure the cell text).

This requires that you update your source data (classDetail / cellData) as the switches change so that you can use that information later when returning cells. This is both to prevent duplication of changes and the ensure that a selected switch doesn't change to a deselected switch after a cell is scroll off-screen.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top