[英]uicollectionview scrolling change cell indexpath
I'm trying to create a week calendar by using uiclloectionview. 我正在尝试使用uiclloectionview创建一个星期日历。 here is what it would look like when the view come to focus:
当视图成为焦点时,会是这样:
original view when I scroll to bottom and scroll back to top. 当我滚动到底部并滚动回到顶部时的原始视图 。 those cells that were disappeared because of scrolling change text.
由于滚动更改而消失的那些单元格。 scrolled view
滚动视图
Here is my code: 这是我的代码:
@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
I just wonder why indexpath keep changing and how to fix this issue? 我只是想知道为什么indexpath不断变化,以及如何解决此问题? thank you.
谢谢。
You only configure the cell if it doesn't have the message
UIView
(tag 2) - so when cells are reused you are simply returning them as they were configured previously - however there is no guarantee that the cell be reused in the same indexPath
as it was previously - in fact you can pretty much guarantee that it won't. 您仅在没有
message
UIView
(标记2)的情况下配置单元格-因此,当单元格被重用时,您只需按先前配置的方式将它们返回即可-但是,不能保证该单元格可在与indexPath
相同的indexPath
重用以前是这样-实际上,您几乎可以保证不会。
You need to correctly configure your cell every time. 您需要每次正确配置单元。 This will be much easier if you configure your prototype cell in the storyboard, or if you don't want to do that create a
UICollectionViewCell
subclass that adds the title
and message
controls in its init method. 如果您在情节
UICollectionViewCell
配置原型单元,或者不希望这样做,则创建UICollectionViewCell
子类以在其init方法中添加title
和message
控件,这将更加容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.