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 通过子类 UIViews 的实例化完成其循环(打印“完成”),但随后似乎返回并再次无限循环它们。我是否缺少与加载此自定义 xib 相关的内容?

4

1 回答 1

0

尝试将其更改为:

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

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

   if (self) {
       // blah
   }


   return self;

}

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

于 2013-09-23T08:59:22.163 回答