0

xib (DHSwipeView) を使用して UIView のカスタム サブクラスを作成し、カスタム UITableViewCell 内に含まれる UIScrollView にこのサブクラスの複数のインスタンスをプログラムで追加しようとしています (ここにアップロードされた github プロジェクトで作業しています: https://github .com/derrh/SwipeAwayCell/blob/master/SwipeAwayCell/DHSwipeAwayCell.h )。最終的な目標は、水平方向にスクロールできるエントリを持つ UITableView を作成し、UIScrollView の各ページがカスタム UIView サブクラスのインスタンスを表すようにすることです。

カスタム UIView を初期化し、プログラムによる書式設定 (ラベルの追加など) を追加すると、プログラムは正常に動作します。ただし、UIScrollView がカスタム UIView を初期化してから xib をロードすると、無限ループに入ります。

UIScrollView のコンテナ ビュー コードは次のとおりです。

#import "DHSwipeAwayCell.h"
#import "DHSwipe.h"
#import <QuartzCore/QuartzCore.h>

@implementation DHSwipeAwayCell

- (void)layoutSubviews
{
    [super layoutSubviews];

    self.backgroundColor = [UIColor whiteColor];

    self.scrollView = [[UIScrollView alloc]
                         initWithFrame:CGRectMake(0, 0,
                                                  self.frame.size.width,
                                                  self.frame.size.height)];
    self.scrollView.pagingEnabled = YES;
    [self.scrollView setShowsHorizontalScrollIndicator:NO];
    self.scrollView.backgroundColor = [UIColor whiteColor];;

    NSInteger numberOfViews = 5;
    for (int i = 0; i < numberOfViews; i++) {

        CGFloat myOrigin = i * self.frame.size.width;

        //create the sub view and allocate memory
        DHSwipe *myView = [[DHSwipe alloc] initWithFrame:CGRectMake(myOrigin, 0, self.frame.size.width, self.frame.size.height)];

        self.scrollView.delegate = self;
        [self.scrollView addSubview:myView];
    }

    self.scrollView.contentSize = CGSizeMake(self.frame.size.width * numberOfViews,
                                           self.frame.size.height);

    CGPoint scrollPoint = CGPointMake(self.frame.size.width * 2, 0);
    [self.scrollView setContentOffset:scrollPoint animated:YES];

    [self addSubview:self.scrollView];
    NSLog(@"Finish");

}

@end

私のカスタム UIView サブクラスは以下のとおりです。

#import "DHSwipe.h"
#import <QuartzCore/QuartzCore.h>

@implementation DHSwipe

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
//When I include the following commands, the UIScrollView program goes into an infinite loop   
    if (self) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DHSwipeView" owner:self options:nil];
        self = [nib objectAtIndex:0];
        self.frame = frame; 
        self.cellTitle.text = @"Good!";
        NSLog(@"%@", self.cellTitle);

//This formatting works
        UIView *roundedCornerView = [[UIView alloc] initWithFrame:CGRectMake(10, 5, 300, self.frame.size.height - 10)];
        roundedCornerView.layer.masksToBounds = NO;
        roundedCornerView.layer.cornerRadius = 3.0;
        roundedCornerView.layer.shadowOffset = CGSizeMake(-1, 1);
        roundedCornerView.layer.shadowOpacity = 0.5;
        roundedCornerView.backgroundColor = [UIColor blueColor];

        [self addSubview:roundedCornerView];
        [self sendSubviewToBack:roundedCornerView];

    }
    return self;
}

@end

IB ではすべてが正常に接続されているようで、UILabel サブクラスを出力する NSLog コマンドは、ラベルが正しいテキストで存在することを示しています。ただし、ログを確認すると、UIScrollView はサブクラスの UIView のインスタンス化を通じてループ (「Finish」を出力) を完了しますが、戻ってそれらすべてを再び無限にループするようです。このカスタム xib の読み込みに関連して、私が見逃しているものはありますか?

4

1 に答える 1