Я пытаюсь создать недельный календарь, используя uiclloectionview. вот как это выглядело бы при взгляде на фокус:uicollectionview scrolling change cell indexpath
original view Когда я прокручиваю нижнюю часть и прокручиваю назад. те ячейки, которые исчезли из-за прокрутки текста изменения. scrolled view
Вот мой код:
@interface ViewController()
@end
@implementation ViewController
@synthesize calendar;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//[self.calendar setCollectionViewLayout:[[CalendarFlowLayout alloc]init]];
timeline = [NSArray arrayWithObjects:@"8:00", @"9:00",@"10:00",@"11:00",@"12:00",@"13:00",@"14:00",@"15:00",@"16:00",@"17:00",@"18:00",@"19:00",@"20:00",@"21:00",@"22:00",nil];
weekline=[NSArray arrayWithObjects:@"Mon", @"Tue",@"Wen",@"Thr",@"Fri",@"Sat",@"Sun",nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 128;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
if (![cell.contentView viewWithTag:2]) {
UILabel * title;
UIView *message;
title =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
title.font=[UIFont systemFontOfSize:14.0];
title.textAlignment=NSTextAlignmentCenter;
//title.backgroundColor=[UIColor redColor];
if (indexPath.row%8==0) {
int time =indexPath.row/8;
if (time<[timeline count]) {
title.text =[NSString stringWithFormat:@"%@",[timeline objectAtIndex:time]];
}
}else if(indexPath.row>0&&indexPath.row<8){
if (indexPath.row<[weekline count]+1) {
title.text =[NSString stringWithFormat:@"%@",[weekline objectAtIndex:indexPath.row-1]];
}
[cell.layer setBorderColor:[UIColor blackColor].CGColor];
[cell.layer setBorderWidth:1.0f];
}else{
[cell.layer setBorderColor:[UIColor blackColor].CGColor];
[cell.layer setBorderWidth:1.0f];
}
title.textColor=[UIColor blackColor];
message = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height)];
message.tag = 2;
message.backgroundColor =[UIColor clearColor];
[message addSubview:title];
[cell.contentView addSubview:message];
}
return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
CGFloat width =[[UIScreen mainScreen]bounds].size.width/8;
return CGSizeMake(width, width);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(0, 0, 0, 0);
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
return 0.0;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return 0.0;
}
@end
Я просто удивляюсь, почему indexpath сохранить изменения и как решить эту проблему? Спасибо.
+1. Btw вот старая должность, где я объяснил, как это сделать правильно: http://stackoverflow.com/questions/23801418/uicollectionview-adding-image-to-a-cell/23802296#23802296 – Kujey
, если я создаю свой собственный подкласс UIcollectionviewcell, будет ли эта проблема? мне нужно изменить свой код? –
Да, и да. Создайте подкласс, который уже содержит дополнительные элементы пользовательского интерфейса (вы можете сделать это в Storyboard, если вы его используете, и связать его с вашим классом с помощью IBoutlets, как обычно), ваш cellForIndexPath упрощается. – Paulw11