2014-01-22 53 views
2

我有一个关于在UIcollectionviewcell中添加UIView的问题。我有UIcollectionview,并且是条件,如果条件为真,我需要将UIView添加到UIcollectionviewcell。如果条件为假,请不要添加。 更新后UIcollectionviewUIView被添加到Cell其条件为真,并且其中一些条件为假。如何增加UIViewUIcollectionviewcell?可以添加一个,两个或三个UIView单元格。将UIView添加到UICollectionviewcell

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{  
NSInteger dayStart = [self dayWeekStart:[self getCurentDate:indexPath.section]]; 

CalendarCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath]; 

if (indexPath.item+1 < dayStart) { 
    CalendarEmptyCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellemptyIdentifier" forIndexPath:indexPath]; 

    return cell; 

} else { 

    CalendarCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath]; 

    cell.titleLabel.text = [NSString stringWithFormat:@"%i",indexPath.item-dayStart+2]; 

    int todayDayRowInt = todayDayRow; 
    int dayPlusShift = todayDayRowInt + dayStart - 2; 

    cell.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:15]; 

    CALayer* layer = [cell layer]; 

    [layer setCornerRadius:15]; 

    dateFormatter.dateFormat = @"yyyy-MM-dd hh:mm:ss Z"; 

    cell.titleLabel.textColor = [UIColor lightGrayColor]; 

    dateFromString1 = [NSDate date]; 

    dateFromString2 = [dateFormatter dateFromString:_dateFinish]; 

    dateFromString3 = [dateFormatter dateFromString:_dateStart]; 


    if ([self checkDateFromCalendar:indexPath.section row:indexPath.item + 3 - dayStart] == 1) { 

     AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

     [arrColor removeAllObjects]; 

     for (int i=0; i<appDelegate.selectPill.count; i++) { 

      Pill *arrPill = [appDelegate.selectPill objectAtIndex:i]; 

      NSNumber *mDay = arrPill.manyday; 

      int mDayInt = [mDay integerValue]+1; 

      if ([[self sameDateByAddingMonths:[dateFormatter dateFromString:_dateStart] addMonths:indexPath.section addDay:indexPath.item + 3 - dayStart] compare:[self sameDateByAddingMonths:arrPill.start addMonths:0 addDay:mDayInt]] == NSOrderedAscending) { 

       NSDate *verifiableDate = [self sameDateByAddingMonths:[dateFormatter dateFromString:_dateStart] addMonths:indexPath.section addDay:indexPath.item]; 
       NSDate *verifiableDate2 = [self sameDateByAddingMonths:[dateFormatter dateFromString:_dateStart] addMonths:indexPath.section addDay:indexPath.item+1]; 

       NSTimeInterval diff = [verifiableDate timeIntervalSinceDate:arrPill.start]; 
       NSTimeInterval diff2 = [verifiableDate2 timeIntervalSinceDate:arrPill.start]; 

       yui = (diff2 - diff); 

       NSInteger sss = diff/yui; 

       if (diff >= 0) { 

        float fff = fmod (sss , ([arrPill.frequency doubleValue]+1)); 

        if (fff == 0) { 

         NSArray *components = [arrPill.color componentsSeparatedByString:@","]; 
         CGFloat r = [[components objectAtIndex:0] floatValue]; 
         CGFloat g = [[components objectAtIndex:1] floatValue]; 
         CGFloat b = [[components objectAtIndex:2] floatValue]; 
         CGFloat a = [[components objectAtIndex:3] floatValue]; 
         UIColor *color = [UIColor colorWithRed:r green:g blue:b alpha:a]; 

         UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0+(i*10), (i*10), 10, 10)]; 
         view.backgroundColor = [UIColor color]; 

         [cell.contentView addSubview:view]; 

        } 
       } 
      } 
     } 

     cell.titleLabel.textColor = [UIColor darkGrayColor]; 

    } else { 

     cell.titleLabel.textColor = [UIColor lightGrayColor]; 

    } 

    if ([dateFromString1 compare:dateFromString2] != NSOrderedDescending && [dateFromString1 compare:dateFromString3] != NSOrderedAscending) { 

     if (indexPath.section == todayMonthSection && indexPath.item == dayPlusShift){ 

      cell.backgroundColor = [UIColor colorWithRed:60.0/255.0 green:162.0/255.0 blue:161.0/255.0 alpha:1]; 
      cell.titleLabel.textColor = [UIColor whiteColor]; 
      cell.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:15]; 

     } else { 

      cell.backgroundColor = [UIColor whiteColor]; 

     } 

    } 

    return cell; 
} 
return cell; 
} 

回答

0

使用cell.contentView访问单元的UIView。

例子:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *identifier = @"Cell"; 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 
    MyRootView *view = [[MyRootView alloc]initWithFrame:CGRectMake(0, 0, 100, 100) someBusinessLogicParam:1]; 
    [cell.contentView addSubview:view]; 
    return cell; 
} 

,我认为将有助于为您有一个根视图这需要在你的业务逻辑作为参数。业务参数将决定在根视图的initWithFrame期间显示哪些视图。

修改MyRootView的例子:

- (id)initWithFrame:(CGRect)frame someBusinessLogicParam:(NSInteger)bp 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
     self.backgroundColor = [UIColor whiteColor]; 
     if (bp == 0) { 
      UIView *yourChild1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; 
      [self addSubview:yourChild1]; 
     } else if (bp == 1) { 
      UIView *yourChild2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; 
      [self addSubview:yourChild2]; 
     }else if (bp == 3) { 
      UIView *yourChild3 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; 
      [self addSubview:yourChild3]; 
      UIView *yourChild4 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)]; 
      [self addSubview:yourChild4]; 
     } 
    } 
    return self; 
} 

然后,您可以使用cell.contentView用你的逻辑沿着MyRootView添加到什么得到显示。

+1

它不工作的连接。 – Alexey

+0

你能否更具描述性地说明为什么它不起作用?您是否在故事板的标识符中指定了“单元”名称?你的numberOfItemsInSection方法是否返回多于0个项目?当你设置一个断点在YourView的initWithFrame中,它是否触及断点? – Xuan

+0

此方法将uiview添加到所有单元格。如果您在条件下使用它不起作用。 – Alexey

1

视觉上添加您以您的
故事板或XIB文件,并进行自定义单元格类使
使用下面的代码片段

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *cellIdentifier = @"Cell"; 

PhotosCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 
UIView *anotherView = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, 20.0, 20.0)]; 

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 50.0, 20.0)]; 
label.text = @"Hello"; 

[anotherView addSubview:label]; 

[cell.myView addSubview:anotherView]; 

if("Your condition is true") 
{ 
     cell.myView.hidden = YES; 
} 
else 
{ 
     cell.myView.hidden = YES; 

} 

return cell; 

} 
+0

它工作,如果我需要添加一个uiview。但我需要为不同的uiview单元添加不同的数量。有些我不需要添加uview。 – Alexey

+0

@Alexey - 我发布了我的新答案 - 一旦你有一个UIView,你可以添加另一个视图的子视图 - 这样你可以为每个单元添加自定义视图 –

+0

感谢您的帮助。 – Alexey