效果圖:
農(nóng)家樂房間管理 ,方便農(nóng)家樂主人管理自己的房間.
上部是日期顯示 (以當前日期為開始往后 60天)
左側(cè)是 房間號 ,
可以上下,左右滑動,方便管理農(nóng)家樂的預定情況.
點擊彈出框 我就偷懶了,哈哈
主要控件: UIScrollView 和 CollectionView
主要代碼
QTSelectCollectionView
@protocol QTSelectCollectionViewDelegate <NSObject>
@optional
@protocol QTSelectCollectionViewDelegate <NSObject>
@optional
/// 監(jiān)聽dateCollectionView的滾動位移
- (void)selectCollectionView:(QTSelectCollectionView *)collectionView DidScrollWithContentOffset:(CGPoint)contentOffset;
// 監(jiān)聽選中某個cell
- (void)selectCollectionView:(QTSelectCollectionView *)collectionView didSelectItemAtIndex:(NSInteger)index;
@end
@interface QTSelectCollectionView : UICollectionView
/// 所有被選中cell的索引
@property (nonatomic,strong) NSMutableArray *SelectedCellIndexs;
/// 所有被選中cell的訂單Id
@property (nonatomic,strong) NSMutableArray *SelectedCellAccountingIds;
/// cell的大小
@property (nonatomic,assign) CGSize itemSize;
//**代理/
@property (nonatomic, weak)id<QTSelectCollectionViewDelegate> SelectColDelegate;
QTDateCollectionView
@class QTDate,QTDateCollectionView;
@protocol QTDateCollectionViewDelegate <NSObject>
@optional
/// 監(jiān)聽的滾動位移
- (void)dateCollectionView:(QTDateCollectionView *)collectionView DidScrollWithContentOffset:(CGPoint)contentOffset;
@end
@interface QTDateCollectionView : UICollectionView
/// cell的大小
@property (nonatomic,assign) CGSize itemSize;
//**代理*/
@property (nonatomic, weak) id<QTDateCollectionViewDelegate> dateDelegate;
@end
控制器
#pragma mark 準備UI
- (void)prepareUI {
//1,添加子控件
[self.view addSubview:self.roomNoView];
[self.view addSubview:self.totalNoView];
[self.view addSubview:self.dateCollectionView];
[self.view addSubview:self.backScrollView];
// 2.設(shè)置frame
// 設(shè)置roomNoView沙绝、totalNoView
CGFloat viewY = 0;
self.roomNoView.frame = CGRectMake(0, viewY, viewWidth*2, viewHeight*heightMultiple*0.5);
self.totalNoView.frame = CGRectMake(0, viewY + viewHeight * heightMultiple * 0.5, viewWidth*2, viewHeight*heightMultiple*0.5);
// 設(shè)置collectionViewW
self.dateCollectionView.frame = CGRectMake(viewWidth * 2, 0, collectionViewW, viewHeight*heightMultiple);
itemW = (collectionViewW - 4)*0.2;
self.dateCollectionView.itemSize = CGSizeMake(itemW, viewHeight*heightMultiple);
CGFloat scrollViewY = CGRectGetMaxY(self.dateCollectionView.frame);
CGFloat scrollViewH = HEIGHT - scrollViewY-64 ;
self.backScrollView.frame = CGRectMake(0, scrollViewY, WIDTH , scrollViewH);
self.selectCollectionView.frame = CGRectMake(0, 0, collectionViewW, scrollViewH);
}
創(chuàng)建小cell
// 添加日期選項
- (void)addSelectCollectionView {
// 創(chuàng)建selectCollectionView
self.selectCollectionView = [[QTSelectCollectionView alloc]init];
// 指定代理
self.selectCollectionView.SelectColDelegate = self;
// 取消顯示水平滾動指示器
self.selectCollectionView.showsHorizontalScrollIndicator = NO;
// 1. 添加子控件
[self.backScrollView addSubview:self.selectCollectionView];
// 2. 設(shè)置frame
CGFloat selectColX = CGRectGetMaxX(self.roomView.frame);
CGFloat selectColY = self.roomView.frame.origin.y;
CGFloat selectColW = self.roomView.height;
self.selectCollectionView.frame = CGRectMake(selectColX, selectColY, collectionViewW, selectColW);
self.selectCollectionView.itemSize = CGSizeMake(itemW, viewHeight);
// 添加到數(shù)組中
[self.selectCollectionViewArr addObject:self.selectCollectionView];
}
監(jiān)聽事件
#pragma mark - ZLTDateCollectionViewDelegate方法
/// 根據(jù)dateCollectionView的滾動症昏,使選項selectCollectionVie同步滾動
- (void)dateCollectionView:(QTDateCollectionView *)collectionView DidScrollWithContentOffset:(CGPoint)contentOffset
{
self.currentContentOffset = contentOffset;
// 調(diào)用監(jiān)聽collectionView滾動的方法
[self collectionViewDidScrollWithContentOffset:contentOffset];
// 便利數(shù)組
for (QTSelectCollectionView *selectCollectionView in self.selectCollectionViewArr) {
selectCollectionView.contentOffset = contentOffset;
}
}
#pragma mark - ZLTSelectCollectionViewDelegate方法
/// 根據(jù)selectCollectionVie的滾動袜刷,使顯示日期的dateCollectionView同步滾動
- (void)selectCollectionView:(QTSelectCollectionView *)collectionView DidScrollWithContentOffset:(CGPoint)contentOffset
{
self.dateCollectionView.contentOffset = contentOffset;
// 調(diào)用監(jiān)聽collectionView滾動的方法
[self collectionViewDidScrollWithContentOffset:contentOffset];
}
時間處理
/// 獲取分割后的當前日期谤牡,形式"yyyy","MM","dd" WithIndex:(NSUInteger)index
- (NSArray *)getDate{
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60*60*24*self.index];
// NSLog(@"--index:%zd",self.index);
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = @"yyyy-MM-dd";
// formatter.dateFormat = [date getDayOfWeekShortString];
NSString *dateStr = [formatter stringFromDate:date];
// NSLog(@"%@",dateStr);
return [dateStr componentsSeparatedByString:@"-"];
}
/// 獲取當前周幾
- (NSString *)getWeekday
{
NSDateComponents *_comps = [[NSDateComponents alloc] init];
[_comps setDay:[[self getDay] integerValue]];
NSString *monthStr = [self getDate][1];
[_comps setMonth:[monthStr integerValue]];
[_comps setYear:[[self getYear] integerValue]];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *_date = [gregorian dateFromComponents:_comps];
NSDateComponents *weekdayComponents = [gregorian components:NSCalendarUnitWeekday fromDate:_date];
NSInteger _weekday = [weekdayComponents weekday]-1; // 此處結(jié)果多了一天,減1丙曙,得到當前是周幾}
// 轉(zhuǎn)為字符串
NSString *weekdayStr = nil;
// NSLog(@"_weekday:%zd",_weekday);
switch (_weekday) {
case 1:
weekdayStr = @"周一";
break;
case 2:
weekdayStr = @"周二";
break;
case 3:
weekdayStr = @"周三";
break;
case 4:
weekdayStr = @"周四";
break;
case 5:
weekdayStr = @"周五";
break;
case 6:
weekdayStr = @"周六";
break;
case 0:
weekdayStr = @"周日";
break;
}
// NSLog(@"_weekday::%@",weekdayStr);
return weekdayStr;
}