我有一個關於在UIcollectionviewcell
中添加UIView
的問題。我有UIcollectionview
,並且是條件,如果條件爲真,我需要將UIView
添加到UIcollectionviewcell
。如果條件爲假,請不要添加。 更新後UIcollectionview
UIView
被添加到Cell
其條件爲真,並且其中一些條件爲假。如何增加UIView
到UIcollectionviewcell
?可以添加一個,兩個或三個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;
}
它不工作的連接。 – Alexey
你能否更具描述性地說明爲什麼它不起作用?您是否在故事板的標識符中指定了「單元」名稱?你的numberOfItemsInSection方法是否返回多於0個項目?當你設置一個斷點在YourView的initWithFrame中,它是否觸及斷點? – Xuan
此方法將uiview添加到所有單元格。如果您在條件下使用它不起作用。 – Alexey