2014-01-22 2 views
2

UIViewUIcollectionviewcell에 추가하는 것에 대한 질문이 있습니다. 나는 UIcollectionview을 가지고 있으며, 조건이 참이라면 UIViewUIcollectionviewcell에 추가해야합니다. 조건이 거짓이면 추가하지 않습니다. UIcollectionviewUIView을 업데이트 한 후 Cell에 추가되고 조건의 일부는 false입니다. UIViewUIcollectionviewcell에 추가하는 방법은 무엇입니까? 하나, 둘 또는 세 UIView 셀을 추가 할 수 있습니다.UICollectionviewcell에 UIView 추가

- (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

셀의 UIView에 액세스하려면 cell.contentView를 사용하십시오.

예 : I 도움이 될 생각

- (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; 
} 

는 그런 다음 표시됩니다 무엇에 로직과 함께 MyRootView에 추가 할 cell.contentView를 사용할 수 있습니다.

+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 셀에 다른 양을 추가해야합니다. 일부는 uiview를 추가 할 필요가 없습니다. – Alexey

+0

@Alexey - 내가 새로운 답변을 올렸습니다. 한 번 UIView를 사용하면 다른보기의 하위보기도 추가 할 수 있습니다. 각 셀마다 사용자 정의보기를 추가 할 수 있습니다. –

+0

도움을 주셔서 감사합니다. – Alexey

관련 문제