Iphone 自动将UITextView放置在彼此的下面

Iphone 自动将UITextView放置在彼此的下面,iphone,objective-c,uitextfield,uitextview,uitextfielddelegate,Iphone,Objective C,Uitextfield,Uitextview,Uitextfielddelegate,我有一个UITextfield,我想为每个insert创建一个新的UITextview,其中包含UITextfield中键入的文本。但正如你所看到的,我的问题是,每个新创建的文本视图都位于旧文本视图之上,如果我创建一个新的文本视图,我不知道如何自动将文本视图放置在彼此之下 我的特克斯菲尔德: self.inputComment = [[GTTextField alloc]initWithFrame:CGRectMake(0,self.mainView.frame.origin.y + self.

我有一个UITextfield,我想为每个insert创建一个新的UITextview,其中包含UITextfield中键入的文本。但正如你所看到的,我的问题是,每个新创建的文本视图都位于旧文本视图之上,如果我创建一个新的文本视图,我不知道如何自动将文本视图放置在彼此之下

我的特克斯菲尔德:

self.inputComment = [[GTTextField alloc]initWithFrame:CGRectMake(0,self.mainView.frame.origin.y + self.mainView.frame.size.height - 100.0f, self.frame.size.width, 100.0f)];

    self.inputComment.placeholder = @"Answer";
    self.inputComment.font = GTDefaultTextFont;
    self.inputComment.textColor = GTDefaultTextColor;
    self.inputComment.userInteractionEnabled = YES;
    self.inputComment.returnKeyType = UIReturnKeyDone;
    [self.inputComment resignFirstResponder];
    self.inputComment.delegate = self;
    [self.mainView addSubview: self.inputComment];
在这里,我在完成文本字段的输入后立即创建新的文本视图:

- (void)textFieldDidEndEditing:(UITextField *)textField{

NSString *saveText = self.inputComment.text;

self.containerCommentView = [[[GTView alloc] initWithFrame:CGRectMake(0.0f,self.messageView.frame.origin.y + self.messageView.frame.size.height,self.frame.size.width, 100.0f)] autorelease];
self.containerCommentView.backgroundColor = [UIColor lightGrayColor];
[self.scrollView addSubview: self.containerCommentView];

self.commentImageView = [[[GTImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,50, 50.0f)] autorelease];
[self.containerCommentView addSubview:self.commentImageView];

self.commentView = [[[GTTextView alloc] initWithFrame:CGRectMake(50.0f, 0.0f,270.0f, 100.0f)] autorelease];
self.commentView.userInteractionEnabled = NO;
self.commentView.textColor = [UIColor blackColor];
self.commentView.backgroundColor = [UIColor lightGrayColor];
[self.commentView setText: saveText];

[self.containerCommentView addSubview: self.commentView];
}

我希望你能帮助我:)

编辑:

我现在使用UITableView,但出现了一个错误: 由于未捕获异常“NSInternalInconsistencyException”而终止应用程序,原因:“尝试将行1插入节0,但更新后节0中只有0行”

我的代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:    (NSInteger)section {
return [self.commentArray count];
}

}

我只想要一节


我的问题在哪里?

只是一个建议,将您的答案添加到一个UITableView,其中每一行都是一个答案,不是更好吗

如果您不想走这条路线:

它总是相互重叠的原因是y坐标始终相同(self.messageView.frame.origin.y+self.messageView.frame.size.height)——高度没有增加。另外,在每次插入新视图时,都要设置containerCommentView的实例

**编辑UITableView更改**

您只有一个节,这些节是基于0的,因此您的节是实际的节0。如果您有两个分区,那么分区1将是0,分区2将是1

试着移动

int rowIndex = self.commentArray.count;
[self.commentArray insertObject:[[NSString alloc] initWithFormat:@"%@", self.inputComment.text]
                        atIndex:rowIndex];

[self.tableView beginUpdates]上方我不是100%确定,但我相信BeginUpdate会在插入动画之前拍摄行数的快照,并且由于您在更新中添加到数组,它仍然认为commentArray是空的。

感谢您的宝贵提示,我已经用UITableView尝试过了,但是现在我遇到了一些问题…请参阅我的编辑
int rowIndex = self.commentArray.count;
[self.commentArray insertObject:[[NSString alloc] initWithFormat:@"%@", self.inputComment.text]
                        atIndex:rowIndex];