无法通过UICollectionView和NSTimer

时间:2018-06-15 03:48:39

标签: ios objective-c uiscrollview uicollectionview nstimer

嗨,当我将一个参数传递给scrollToItemAtIndexPath,它总是被终止时,我遇到了NSTimer的问题。有人可以帮忙吗?

SlideCollectionViewCell *cell = [collectionImageSlideView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

cell.showImage.image = [UIImage imageNamed:[images objectAtIndex:indexPath.row]];
NSInteger rowIndex = indexPath.row;
NSInteger imageCount = self->images.count-1;
if(rowIndex<imageCount){
    rowIndex = rowIndex+1;

}else{
    rowIndex = 0;
}
scrollingTimer = [NSTimer scheduledTimerWithTimeInterval: 5.0
                target: self
                selector:@selector(startTimer:)
                userInfo: @(rowIndex) repeats:YES];
return cell;

enter image description here

1 个答案:

答案 0 :(得分:0)

这是因为你要在NSIndexpath变量中传递一个NSInteger,你需要创建一个NSIndexpath然后将它传递给如下函数: -

cell.showImage.image = [UIImage imageNamed:[images objectAtIndex:indexPath.row]];
NSInteger rowIndex = indexPath.row;
NSInteger imageCount = self->images.count-1;
if(rowIndex<imageCount){
    rowIndex = rowIndex+1;

}else{
    rowIndex = 0;
}
NSIndexPath *indexToPass=[NSIndexPath indexPathForRow:rowIndex inSection:indexPath.section];
scrollingTimer = [NSTimer scheduledTimerWithTimeInterval: 5.0
            target: self
            selector:@selector(startTimer:)
            userInfo:indexToPass repeats:YES];
return cell;