0

我正在尝试Calendar展示UITableView. 每个部分对应日历的每一年,每个部分包含 12 个单元格对应 12 个月。

我正在尝试显示 10 年日历、5 年回到当前日期和 5 年未来。我可以将当​​前年份(部分)2013 作为第 0 部分加载到UITableView. 但是当我向上滚动表格时,我希望看到 2012 年高于 2013 年。但由于 2013 年是第 0 节,所以我不能这样做,尽管我可以在当前节之后插入新节,这很简单。

- (id)initWithFrame:(CGRect)frame logic:(LineKalMonthLogic *)theLogic delegate:(id<LineKalViewDelegate>)theDelegate
{
frame.size.width =  kkLineTileSize.width;
frame.size.height = 31 *  kkLineTileSize.height;

if (self = [super initWithFrame:frame]) {
    self.clipsToBounds = YES;
    logic = [theLogic retain];
    delegate = theDelegate;

    CGRect rect = CGRectMake(0, 0,1024, 716);
    _mainTable =[[UITableView alloc] initWithFrame:rect style:UITableViewStyleGrouped];
    _mainTable.delegate = self;
    _mainTable.dataSource = self;

    _mainTable.backgroundColor = [UIColor grayColor];
    [self addSubview:_mainTable];
    monthHeight = 723;
    backMonthArray = [[NSMutableArray alloc] init];
    heightArray = [[NSMutableArray alloc] init];
    _sectionNo = 1;
}
return self;
}
-(void)calculateMonths:(int)monthNo{

int i ;
for (i=monthNo; i<monthNo+12; i++) {

    CGRect monthRect = CGRectMake(0.f, 0.f, 1024,723);

    backMonthView = [[[LineKalMonthView alloc] initWithFrame:monthRect andDays: [logic numberOfdaysInMonth:i] andMonthName:(NSString *)[logic gettingMonthDate:i]] autorelease];

    [delegate showForCurrentMonth];

    [backMonthView showDates:logic.daysInSelectedMonth
        leadingAdjacentDates:logic.daysInFinalWeekOfPreviousMonth
       trailingAdjacentDates:logic.daysInFirstWeekOfFollowingMonth];

    [backMonthArray addObject:backMonthView];
    [delegate reloadData];
    monthHeight = backMonthView.frame.size.height;
    [heightArray addObject:[NSNumber numberWithFloat:monthHeight]];
}
nextMonthNo = i;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _sectionNo;    //count of section
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return 12; 
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [[heightArray objectAtIndex:indexPath.row+indexPath.section*12] intValue];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:MyIdentifier] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
}

   [cell.contentView addSubview:[backMonthArray objectAtIndex:indexPath.row+indexPath.section*12]]; //backMonthArray contains 12 months data 

return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *) cell forRowAtIndexPath:(NSIndexPath *)indexPath
{

if(indexPath.row == 10 && indexPath.section == _sectionNo-1) //self.array is the array of items you are displaying
{
    dispatch_async(dispatch_get_main_queue(), ^{
  [self calculateMonths:nextMonthNo];
        _sectionNo = _sectionNo+1;
[_mainTable insertSections:[NSIndexSet indexSetWithIndex:_sectionNo-1] withRowAnimation:UITableViewRowAnimationNone];
    });
}
}

所以我只想在当前部分之前显示部分。

4

0 回答 0