在UIScrollView的子视图中加载xib时出现无限循环错误

时间:2013-09-19 13:04:12

标签: ios6 uitableview uiscrollview xib

我创建了一个带有xib(DHSwipeView)的UIView的自定义子类,并且我尝试以编程方式将此子类的多个实例添加到包含在自定义UITableViewCell中的UIScrollView(处理上传到此处的github项目:{{ 3}})。最终目标是使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通过子类UIViews的实例化完成其循环(打印“完成”),但似乎又返回并无限循环遍历它们。我有什么东西与加载这个自定义xib有关吗?

1 个答案:

答案 0 :(得分:0)

尝试将其更改为:

- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

   self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

   if (self) {
       // blah
   }


   return self;

}

然后调用这个init并通过你的xib代替?