【问题标题】:uicollectionview scrolling change cell indexpathuicollectionview 滚动更改单元格索引路径
【发布时间】:2015-01-26 10:35:32
【问题描述】:

我正在尝试使用 uilloectionview 创建周历。这是视图成为焦点时的样子:

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 不断变化以及如何解决这个问题?谢谢。

【问题讨论】:

    标签: ios objective-c uicollectionview nsindexpath


    【解决方案1】:

    只有在单元格没有 message UIView(标签 2)时才配置单元格 - 因此,当单元格被重复使用时,您只需按照之前配置的方式返回它们 - 但是不能保证单元格可以像以前一样在同一 indexPath 中重复使用 - 事实上,您几乎可以保证它不会。

    您每次都需要正确配置您的单元格。如果您在情节提要中配置原型单元格,或者如果您不想这样做,这将更容易创建一个 UICollectionViewCell 子类,在其 init 方法中添加 titlemessage 控件。

    【讨论】:

    • +1。顺便说一句,这是一篇旧帖子,我解释了如何正确地做到这一点:stackoverflow.com/questions/23801418/…
    • 如果我创建自己的 UIcollectionviewcell 子类,它会解决这个问题吗?我需要更改我的代码吗?
    • 是的,是的。创建一个已经包含其他 UI 元素的子类(如果您使用它并像往常一样使用 IBoutlets 将其链接到您的类,您可以在 Storyboard 中执行此操作),您的 cellForIndexPath 变得更简单。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2013-04-01
    • 1970-01-01
    相关资源
    最近更新 更多