我在UITableViewCell(自定义单元格)上有一个UISwitch,它可以在没有任何触摸的情况下激活其他单元格上的开关,我真的不知道为什么(我的单元格上没有任何代码激活开关)。
Valid XHTML http://m.abril.com/img1.png。 Valid XHTML http://m.abril.com/img2.png
代码:
//创建单元格
- (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;
}
有什么想法吗?
问候!
答案 0 :(得分:1)
这是一个单元重用问题,因为您正在重用单元格,但您没有配置开关状态(应在配置单元格文本时执行此操作)。
这要求您在交换机更改时更新源数据(classDetail
/ cellData
),以便以后在返回单元格时可以使用该信息。这既可以防止重复更改,也可以确保在单元格滚动到屏幕后,所选开关不会更改为取消选择的开关。