我有一个 UITextfield,我想为每个插入一个新的 UITextview 创建一个带有来自 UITextfield 的输入文本。但是正如您所看到的,我的问题是每个新创建的 Textview 都位于旧 Textview 之上,如果我创建一个新的,我不知道如何将 Textviews 自动放置在彼此之下。
我的特克斯菲尔德:
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];
在这里,我在完成来自文本字段的输入后立即创建新的 Textview:
- (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];
}
- (void)buttonTapped:(id)sender {
[self.tableView beginUpdates];
int rowIndex = self.commentArray.count;
[self.commentArray insertObject:[[NSString alloc] initWithFormat:@"%@", self.inputComment.text]
atIndex:rowIndex];
// Notify UITableView that updates have occurred
NSArray *insertIndexPaths = [NSArray arrayWithObject:
[NSIndexPath indexPathForRow:rowIndex inSection:0]];
[self.tableView insertRowsAtIndexPaths:insertIndexPaths
withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];
self.inputComment.text = @"";
}
我只想要1节!
我的问题在哪里?