iphone - 将 UIView 添加到 UICollectionviewcell

标签 iphone objective-c uicollectionview

我有一个关于在 UIcollectionviewcell 中添加 UIView 的问题。我有UIcollectionview并且是条件,如果条件为真我需要将UIView添加到UIcollectionviewcell。如果条件为假,则不添加。 更新 UIcollectionview 后,UIView 将添加到 Cell 中,其中条件为 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;
}

最佳答案

将您的 View 直观地添加到您的
StoryBoard 或 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;

}

关于iphone - 将 UIView 添加到 UICollectionviewcell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21286825/

相关文章:

iphone - AWS S3 IOS sdk 中的困惑?

iphone - 如何从 iPhone 核心位置获取当前(美国)状态?

ios - 使用标签来检测是否有东西被窃听

ios - 在 UICollectionView 上放置一个按钮并保持滚动

iphone - 如果我在 UITableView headerView 中放置 iAd,我的应用程序会被拒绝吗?

iphone - 将 float 转换为整数

javascript - 在 UIWebView 中从 JavaScript 获取返回值

ios - 我是否需要 ApplicationDelegate 函数中的 UIApplication* 应用程序?

ios - UICollectionView 重新加载数据无法更新 CollectionView

ios - layoutAttributesForElementsInRect 的无限循环