初めまして、質問が長くなってしまい申し訳ありません。しかし、私は答えをずっと探していました。2 つの UIScrollView で構成されるタブ付きアプリケーションでビューを作成しました。私はそれらを topScrollView と bottomScrollView と呼びました。これが.hです。
@property (weak, nonatomic) IBOutlet UIScrollView *topScrollView;
@property (weak, nonatomic) IBOutlet UIScrollView *bottomScrollView;
コンセントはすべて正しく配線されています。私が始めたとき、私は最初にトップスクロールビューを作成しました。スクロールビューは写真でいっぱいです。すべてがうまくいきました。ページングでうまくスクロールします。スクロール時に変化するラベルもいくつか付けました。これは、viewDidLoad メソッドからの画像を保持する配列です。
_golfShirtArray = [NSArray arrayWithObjects:
[UIImage imageNamed:@"GT018B-RSB-5961.jpg"],
[UIImage imageNamed:@"GT042-HDT-7234.jpg"],
[UIImage imageNamed:@"gt063-rst-7294.jpg"],
[UIImage imageNamed:@"GT045-CBT3-7203.jpg"],
[UIImage imageNamed:@"GT018-SDT-5971.jpg"],
nil];
次に、viewWillAppear で contentSize を設定し、loadVisiblePages メソッドを呼び出します。スクロール ビューに関する Ray Wenderlich の指示に従いました。これが私のバージョンです。長いコードで申し訳ありません。
- (void)loadVisiblePages
{
CGFloat pageWidth = 528.0f;
NSInteger page = (NSInteger)floor((_topScrollView.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
_topPageControl.currentPage = page;
NSInteger firstPage = page - 1;
NSInteger lastPage = page + 1;
for (NSInteger i = 0; i < firstPage; i++)
{
[self purgePage:i];
}
for (NSInteger i = firstPage; i <= lastPage; i++)
{
[self loadPage:i];
}
for (NSInteger i = lastPage+1; i < _showablePictureArray.count; i++)
{
[self purgePage:i];
}
}
- (void)loadPage:(NSInteger)page
{
if (page < 0 || page >= _showablePictureArray.count)
{
return;
}
UIView *pageView = [_pageViews objectAtIndex:page];
if ((NSNull *)pageView == [NSNull null])
{
CGRect frame = CGRectMake(120, 93, 528, 380); //_topScrollView.bounds;
//NSLog(@"scrollviewbounds is %@", NSStringFromCGRect(frame));
frame.origin.x = frame.size.width * page;
frame.origin.y = 0.0f;
UIImageView *newPageView = [[UIImageView alloc] initWithImage:[_showablePictureArray objectAtIndex:page]];
newPageView.contentMode = UIViewContentModeScaleAspectFit;
newPageView.frame = frame;
[_topScrollView addSubview:newPageView];
[_pageViews replaceObjectAtIndex:page withObject:newPageView];
CGFloat y = 0.0;
for (NSInteger i = 0; i < _showablePictureArray.count; i++)
{
UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(y, 0, 200.0, 20.0)];
UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(y, 23.0, 200.0, 20.0)];
NSString *text1 = [_showableDescriptionArray objectAtIndex:i];
NSString *text2 = [_showablePriceArray objectAtIndex:i];
[descriptionLabel setText:text1];
[priceLabel setText:text2];
descriptionLabel.layer.borderWidth = 2.0f;
descriptionLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
descriptionLabel.layer.cornerRadius = 6.0f;
priceLabel.layer.borderWidth = 2.0f;
priceLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
priceLabel.layer.cornerRadius = 6.0f;
[_topScrollView addSubview:descriptionLabel];
[_topScrollView addSubview:priceLabel];
y = y + 528;
}
}
}
- (void)purgePage:(NSInteger)page
{
if (page < 0 || page >= _showablePictureArray.count)
{
return;
}
UIView *pageView = [_pageViews objectAtIndex:page];
if ((NSNull *)pageView != [NSNull null])
{
[pageView removeFromSuperview];
[_pageViews replaceObjectAtIndex:page withObject:[NSNull null]];
}
}
ユーザーが見るものを変更できるため、作成したすべての配列は _showable... に転送されました。私が言ったように、すべてがうまく機能します。一番下のスクロールビューを追加するまで。私はすべて同じコードを使用しましたが、すべての変数とメソッドの後に下を付けました。また、loadVisiblePages と一緒に loadVisiblePagesBottom も呼び出しました。
- (void)loadVisiblePagesBottom
{
CGFloat pageWidthBottom = 528.0f;
NSInteger pageBottom = (NSInteger)floor((_bottomScrollView.contentOffset.x * 2.0f + pageWidthBottom) / (pageWidthBottom * 2.0f));
NSLog(@"page bottom is %ld", (long)pageBottom);
_bottomPageControl.currentPage = pageBottom;
NSInteger firstPageBottom = pageBottom -1;
NSInteger lastPageBottom = pageBottom +1;
for (NSInteger i = 0; i < firstPageBottom; i++)
{
[self purgePageBottom:i];
}
for (NSInteger i = firstPageBottom; i <= lastPageBottom; i++)
{
[self loadPageBottom:i];
}
for (NSInteger i = lastPageBottom + 1; i < _showablePictureArrayBottom.count; i++)
{
[self purgePageBottom:i];
}
}
- (void)loadPageBottom:(NSInteger)pageBottom
{
NSLog(@"page bottom 2 is %ld", (long)pageBottom);
if (pageBottom < 0 || pageBottom >= _showablePictureArrayBottom.count)
{
return;
}
UIView *pageViewBottom = [_pageViewsBottom objectAtIndex:pageBottom];
if ((NSNull *)pageViewBottom == [NSNull null])
{
CGRect frameBottom = CGRectMake(120, 482, 528, 380);
frameBottom.origin.x = frameBottom.size.width * pageBottom;
frameBottom.origin.y = 0.0f;
UIImageView *bottomView = [[UIImageView alloc] initWithImage:[_showablePictureArrayBottom objectAtIndex:pageBottom]];
//UIImageView *bottomView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"GB007-SPD-5918.jpg"]];
bottomView.contentMode = UIViewContentModeScaleAspectFit;
bottomView.frame = frameBottom;
[_bottomScrollView addSubview:bottomView];
[_pageViewsBottom replaceObjectAtIndex:pageBottom withObject:bottomView];
}
}
- (void)purgePageBottom:(NSInteger)pageBottom
{
if (pageBottom < 0 || pageBottom >= _showablePictureArrayBottom.count)
{
return;
}
UIView *pageViewBottom = [_pageViewsBottom objectAtIndex:pageBottom];
if ((NSNull *)pageViewBottom != [NSNull null])
{
[pageViewBottom removeFromSuperview];
[_pageViewsBottom replaceObjectAtIndex:pageBottom withObject:[NSNull null]];
}
}
ただ、まだラベルを貼っていません。画像の一番下の配列の作成は.
_golfShortsArray = [NSArray arrayWithObjects:
[UIImage imageNamed:@"GB005-nvy-7235.jpg"],
[UIImage imageNamed:@"GB005-RSB-7297.jpg"],
[UIImage imageNamed:@"gb007-bgm-7320.jpg"],
[UIImage imageNamed:@"GB007-NVY-5916.jpg"],
[UIImage imageNamed:@"GB007-SPD-5918.jpg"],
nil];
トップスクロールビューで行ったのと同じです。ただし、アプリケーションは上記のエラーでクラッシュします。いくつかの調査を行ったところ、実際には文字列である画像を解凍しようとしているとのことでしたが、配列は同じで、すべての画像です。NSZombies も実行しましたが、同じエラーが発生しました。
[__NSCFConstantString _isDecompressing]: unrecognized selector sent to instance 0x72668
私はbtを使用し、以下を得ました。
* thread #1: tid = 0x2503, 0x3ad79498 libc++abi.dylib`__cxa_throw, stop reason = breakpoint 1.2
frame #0: 0x3ad79498 libc++abi.dylib`__cxa_throw
frame #1: 0x3b32b9be libobjc.A.dylib`objc_exception_throw + 94
frame #2: 0x33498e06 CoreFoundation`-[NSObject(NSObject) doesNotRecognizeSelector:] + 170
frame #3: 0x33497530 CoreFoundation`___forwarding___ + 392
frame #4: 0x333eef68 CoreFoundation`__forwarding_prep_0___ + 24
frame #5: 0x352f966a UIKit`-[UIImageView initWithImage:] + 66
frame #6: 0x0006d9d4 JoFitTest`-[SecondViewController loadPageBottom:](self=0x1e575840, _cmd=0x0006f25e, pageBottom=0) + 456 at SecondViewController.m:358
frame #7: 0x0006d788 JoFitTest`-[SecondViewController loadVisiblePagesBottom](self=0x1e575840, _cmd=0x0006f236) + 388 at SecondViewController.m:334
frame #8: 0x0006d238 JoFitTest`-[SecondViewController scrollViewDidScroll:](self=0x1e575840, _cmd=0x357119e4, scrollView=0x1e5a5c50) + 164 at SecondViewController.m:286
frame #9: 0x352bf83a UIKit`-[UIScrollView setContentOffset:] + 618
frame #10: 0x353da138 UIKit`-[UIScrollView _updatePanGesture] + 2456
frame #11: 0x353bad88 UIKit`_UIGestureRecognizerSendActions + 128
frame #12: 0x353bad88 UIKit`_UIGestureRecognizerSendActions + 128
frame #13: 0x353823f4 UIKit`-[UIGestureRecognizer _updateGestureWithEvent:] + 392
frame #14: 0x3556fa38 UIKit`___UIGestureRecognizerUpdate_block_invoke_0543 + 48
frame #15: 0x352a682e UIKit`_UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 218
frame #16: 0x352a5292 UIKit`_UIGestureRecognizerUpdate + 1274
frame #17: 0x352b01e6 UIKit`-[UIWindow _sendGesturesForEvent:] + 766
frame #18: 0x352afdb2 UIKit`-[UIWindow sendEvent:] + 90
frame #19: 0x3529d800 UIKit`-[UIApplication sendEvent:] + 380
frame #20: 0x3529d11a UIKit`_UIApplicationHandleEvent + 6154
frame #21: 0x36f915a2 GraphicsServices`_PurpleEventCallback + 590
frame #22: 0x36f911d2 GraphicsServices`PurpleEventCallback + 34
frame #23: 0x3346a172 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 34
frame #24: 0x3346a116 CoreFoundation`__CFRunLoopDoSource1 + 138
frame #25: 0x33468f98 CoreFoundation`__CFRunLoopRun + 1384
frame #26: 0x333dbebc CoreFoundation`CFRunLoopRunSpecific + 356
frame #27: 0x333dbd48 CoreFoundation`CFRunLoopRunInMode + 104
frame #28: 0x36f902ea GraphicsServices`GSEventRunModal + 74
frame #29: 0x352f1300 UIKit`UIApplicationMain + 1120
frame #30: 0x0006a034 JoFitTest`main(argc=1, argv=0x2fd98d00) + 116 at main.m:16
frame #31: 0x3b762b20 libdyld.dylib`start + 4
そして、それが何を意味するのか本当にわかりません。どこで間違ったのかわかりません。下部ではなく上部でどのように機能するのでしょうか。すべての変数が異なるため、割り当て解除されたオブジェクトのメモリをどのように使用できるかわかりません。どんな助けでも大歓迎です。ここからどこへ行けばいいのかわからない。チェックしていただきありがとうございます。また、ここに scrollViewDidScroll メソッドがあります。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView == _topScrollView)
{
[self loadVisiblePages];
}else if (scrollView == _bottomScrollView)
{
NSLog(@"Bottom Scroll");
[self loadVisiblePagesBottom];
}
}
再度、感謝します。