首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSMutableArray of UIViews

NSMutableArray of UIViews
EN

Stack Overflow用户
提问于 2016-09-28 07:46:59
回答 3查看 522关注 0票数 0

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

ViewController.h

代码语言:javascript
复制
@interface ViewController : UIViewController

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

@property UIScrollView *questionScrollView;

ViewController.m

代码语言:javascript
复制
@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));



}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-09-28 08:22:06

如果将数组init更改为以下内容,

代码语言:javascript
复制
self.viewArray = [[NSMutableArray alloc] init];

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

代码语言:javascript
复制
_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];

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

代码语言:javascript
复制
for(int i = 0; i < numberOfQuestions; i++) {
    UIView *view = viewArray[i];
    view.frame = CGRectMake(currentViewPossitionX, currentViewPossitionY, CGRectGetWidth(_questionScrollView.bounds), viewHeight);
....

在这些更改之后,它应该可以工作。

票数 0
EN

Stack Overflow用户

发布于 2016-09-28 08:34:34

代码语言:javascript
复制
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;
}
票数 0
EN

Stack Overflow用户

发布于 2016-09-28 08:56:53

我认为您应该使用自定义UITableViewCell来实现这一点。优点是-容易定制-不需要管理框架大小,因为表视图有自己的滚动视图-为您的所有问题创建模型数组,并只需在UITableView上迭代它

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39741143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档