我正在使用HMSegmentedControl
6个章节标题(星期一,星期二,星期三,星期四,星期五,星期六)。
当今天是星期五,那么我想将HMSegmentedControl
滚动到星期五的索引(即索引4)。
我已将HMSegmentedControl.selectedSegmentIndex
设置为星期五的索引,但它不会滚动到该索引。
当我手动点击星期五然后HMSegmentedControl
滚动。
有谁知道我能做到这一点。
segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[NSLocalizedString(@"MONDAY", nil), NSLocalizedString(@"TUESDAY", nil), NSLocalizedString(@"WEDNESDAY", nil), NSLocalizedString(@"THURSDAY", nil), NSLocalizedString(@"FRIDAY", nil), NSLocalizedString(@"SATURDAY", nil)]];
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
CGFloat viewWidth = CGRectGetWidth(self.view.frame);
segmentedControl.frame = CGRectMake(0, 44, viewWidth, 45);
segmentedControl.segmentEdgeInset = UIEdgeInsetsMake(0, 10, 0, 10);
segmentedControl.backgroundColor = [Nav_Bar_Color toColor];
segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
segmentedControl.selectionIndicatorColor = [UIColor whiteColor];
segmentedControl.verticalDividerEnabled = YES;
segmentedControl.verticalDividerColor = [UIColor clearColor];
segmentedControl.verticalDividerWidth = 1.5f;
[segmentedControl setTitleFormatter:^NSAttributedString *(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected) {
NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont fontWithName:FONT_NAME size:FONT_SIZE]}];
return attString;
}];
[segmentedControl addTarget:self action:@selector(loadTimeTableData:) forControlEvents:UIControlEventValueChanged];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitWeekday fromDate:[NSDate date]];
int weekdayNumber = [components weekday];
weekdayNumber = weekdayNumber - 1;
switch (weekdayNumber) {
case 1:
case 7:
segmentedControl.selectedSegmentIndex = 0;
break;
case 2:
segmentedControl.selectedSegmentIndex = 1;
break;
case 3:
segmentedControl.selectedSegmentIndex = 2;
break;
case 4:
segmentedControl.selectedSegmentIndex = 3;
break;
case 5:
segmentedControl.selectedSegmentIndex = 4;
break;
case 6:
segmentedControl.selectedSegmentIndex = 5;
break;
default:
break;
}
[segmentedControl setSelectedSegmentIndex:segmentedControl.selectedSegmentIndex animated:YES];
[self.view addSubview:segmentedControl];
答案 0 :(得分:0)
您应该在segementControl selected at index
方法中调用loadTimeTableData:
方法。