UIView 的 NSMutableArray

NSMutableArray of UIViews

在这个小应用程序中,我只是想制作一个问卷,在 UIView 上显示每个问题。由于问卷的大小可能会根据我的需要而改变,我想通过使用 for 循环和 NSMutableArray 来操作这些 UIView 来使代码非常灵活。我已经使用下面的代码创建了数组和视图,但是除了紫色的 UIScrollView 之外什么也没有显示。有人可以指出为什么没有显示绿色 UIView 吗?

ViewController.h

@interface ViewController : UIViewController

@property (nonatomic, strong) NSMutableArray *viewArray;
@property UIView *question1View;
@property UIView *question2View;
@property UIView *question3View;

@property UIScrollView *questionScrollView;

ViewController.m

@implementation ViewController

@synthesize viewArray;

float viewHeight = 75.0;
float openViewHeight = 225.0;
float currentViewPossitionX = 0.0;
float currentViewPossitionY = 0.0;
float viewSpacing = 10.0;
int numberOfQuestions = 3;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpQuestionnaire];

}


- (void) setUpQuestionnaire{

    self.viewArray[numberOfQuestions] = [[NSMutableArray alloc] init];
    [viewArray insertObject: _question1View atIndex:0];
    [viewArray insertObject: _question2View atIndex:1];
    [viewArray insertObject: _question3View atIndex:2];

    _question1View.backgroundColor = [UIColor greenColor];
    _question2View.backgroundColor = [UIColor greenColor];
    _question3View.backgroundColor = [UIColor greenColor];

    float viewCount = 0;

    _questionScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
    _questionScrollView.backgroundColor = [UIColor purpleColor];
    _questionScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.bounds),(CGRectGetHeight(self.view.bounds)+100));
    [self.view addSubview:_questionScrollView];

    for(int i = 0; i < numberOfQuestions; i++){
        viewArray[i] = [[UIView alloc] initWithFrame:CGRectMake(currentViewPossitionX, currentViewPossitionY, CGRectGetWidth(_questionScrollView.bounds), viewHeight)];

        [_questionScrollView addSubview:viewArray[i]];
        viewCount++;
        currentViewPossitionY += viewHeight + viewSpacing;
    }

    _questionScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.bounds),(viewHeight * viewCount + 500));



}

如果您将数组初始化更改为以下内容,

self.viewArray = [[NSMutableArray alloc] init];

在将视图添加到数组之前初始化您的视图,

_question1View = [[UIView alloc] init];
_question2View = [[UIView alloc] init];
_question3View = [[UIView alloc] init];
[viewArray insertObject: _question1View atIndex:0];
[viewArray insertObject: _question2View atIndex:1];
[viewArray insertObject: _question3View atIndex:2];

最后只需在循环中设置框架,

for(int i = 0; i < numberOfQuestions; i++) {
    UIView *view = viewArray[i];
    view.frame = CGRectMake(currentViewPossitionX, currentViewPossitionY, CGRectGetWidth(_questionScrollView.bounds), viewHeight);
....

经过这些更改后它应该可以工作。

for(int i = 0; i < numberOfQuestions; i++){
    UIView *aview=viewArray[i];
    aview = [[UIView alloc] initWithFrame:CGRectMake(currentViewPossitionX, currentViewPossitionY, CGRectGetWidth(_questionScrollView.bounds), viewHeight)];
    aview.backgroundColor = [UIColor greenColor];
    [_questionScrollView addSubview:aview];
    viewCount++;
    currentViewPossitionY += viewHeight + viewSpacing;
}

我认为您应该使用自定义 UITableViewCell 来实现此目的。优点是 - 轻松定制 - 无需管理帧大小,因为 table 视图有自己的滚动视图 - 为您的所有问题创建模型数组,然后在 UITableView

上迭代它

如果您想以列表方式显示相同类型的 UI(数据将根据项目位置更改),请选择 table 查看。