前言
我們項(xiàng)目最近需要寫一個(gè)日歷的功能,我在這之前也是沒有寫過這個(gè)。然后自己就去查API,去看別人的講解。貌似通了一些,又貌似沒通。哈哈,把我寫的我們的最初定的實(shí)現(xiàn)效果拿出來,大家如果有需要可以看一下。自己寫一下,不要做拿來黨,不然到時(shí)候如果運(yùn)行有錯(cuò)誤,你改都不知道去哪修改。
思想
其實(shí)日歷比較好寫的,因?yàn)樘O果已經(jīng)把你所需要的API都給你提供了,你只需要調(diào)用就行了。我個(gè)人感覺只是在把當(dāng)月天數(shù)排布的時(shí)候麻煩一點(diǎn)點(diǎn),我個(gè)人采用一個(gè)數(shù)組加載所有的數(shù)據(jù)的形式去展示。當(dāng)然八仙過海,各顯神通,這個(gè)看個(gè)人的理解。 我的排布代碼:- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CalenderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CALENDER forIndexPath:indexPath];
if (indexPath.section == 0) {
cell.dateLabel.text = timerArray[indexPath.row];
cell.dateLabel.font = [UIFont systemFontOfSize:14];
cell.dateLabel.textColor = [UIColor colorWithRed:1.0
green:0.0
blue:0.0
alpha:0.495803420608108];
}else{
cell.dateLabel.textColor = [UIColor colorWithRed:0.0
green:0.0
blue:0.0
alpha:0.495803420608108];
//獲得本月第一天在星期幾
allDayArray = [NSMutableArray array];
NSInteger day = [self currentFirstDay:_date];
for (NSInteger i = 0; i < day; i++){
[allDayArray addObject:@""];
}
NSInteger days = [self currentMonthOfDay:_date];
for (NSInteger i = 1; i <= days; i++) {
[allDayArray addObject:@(i)];
}
//把剩下的空間置為空
for (NSInteger i = allDayArray.count; i < 42; i ++) {
[allDayArray addObject:@""];
}
cell.dateLabel.text = [NSString stringWithFormat:@"%@",allDayArray[indexPath.row]];
}
return cell;
}
運(yùn)行結(jié)果:

結(jié)束
至此結(jié)束,哈哈? ? ? 忘了說了,如果各位看客看著有用,請(qǐng)給個(gè)小星星,謝了。