Iphone 将UIView添加到UICollectionviewcell

Iphone 将UIView添加到UICollectionviewcell,iphone,objective-c,uicollectionview,Iphone,Objective C,Uicollectionview,我有一个关于在UIcollectionviewcell中添加UIView的问题。我有UIcollectionview并且是条件,如果条件为真,我需要将UIView添加到UIcollectionviewcell。如果条件为false,则不要添加。 更新UIcollectionview后,将UIView添加到单元格,其中条件为true,而某些条件为false。如何将UIView添加到UIcollectionviewcell?可以将一个、两个或三个UIView添加到单元格中 - (UICollecti

我有一个关于在
UIcollectionviewcell
中添加
UIView
的问题。我有
UIcollectionview
并且是条件,如果条件为真,我需要将
UIView
添加到
UIcollectionviewcell
。如果条件为false,则不要添加。 更新
UIcollectionview
后,将
UIView
添加到
单元格
,其中条件为true,而某些条件为false。如何将
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;
}
-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{    
NSInteger dayStart=[self dayWeekStart:[self getCurentDate:indexPath.section]];
CalendarCollectionViewCell*单元格=[collectionView dequeueReusableCellWithReuseIdentifier:@“cellIdentifier”forIndexPath:indexPath];
if(indexPath.item+1
使用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中,以显示内容。

将您的视图直观地添加到MyRootView中
故事板或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;

}

你能更详细地描述一下为什么它不起作用吗?您是否在故事板的标识符中指定了“单元”名称?NumberOfItemsSection方法返回的项是否超过0项?当您在YourView的initWithFrame中设置断点时,它是否达到了断点?此方法将uiview添加到所有单元格中。如果将其与条件一起使用,则不起作用。如果我需要添加一个uiview,则它起作用。但我需要在不同的uiview单元中添加不同的量。有些我不需要添加uiview。@Alexey-我发布了我的新答案-一旦你有了一个uiview,你也可以添加另一个视图的子视图-这样你就可以为每个单元格添加自定义视图谢谢你的帮助。如果输入参数改变会发生什么?重新加载UICollectionView后。@Alexey您能描述一下您想要实现什么-详细-参数吗