无法通过 UICollectionView 和 NSTimer 在 Xcode 的 objective-c 中制作幻灯片图像
Cannot make a slide image in objective-c of Xcode by UICollectionView and 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;
这是因为你在 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;
您好,当我将参数传递给 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;
这是因为你在 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;