我有一个UITextfield,我想为每个插件创建一个新的UITextview,其中包含来自UITextfield的类型文本。但正如你可以看到我的问题是每个新创建的Textview都在旧Textview之上,我不知道如果我创建一个新文本视图,如何将Textviews自动放在彼此之下。
My Tetxfield:
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];
在这里,我在完成文本字段的输入后立即创建新的Textviews:
- (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 = @"";
}
我只想要一节!
我的问题在哪里?
答案 0 :(得分:0)
只是一个建议,将你的答案添加到UITableView并不是更容易,每个行都是答案。
如果你不想走这条路:
它一直在彼此之上的原因是你的y坐标总是一样的(self.messageView.frame.origin.y + self.messageView.frame.size.height) - 身高不高不会增加。此外,您每次插入新视图时都要设置containerCommentView的实例。
**编辑UITableView更改**
你只有一个部分,部分是0,所以你的部分是实际部分0.如果你有2个部分,第1部分将是0,2将是1。
尝试移动
int rowIndex = self.commentArray.count;
[self.commentArray insertObject:[[NSString alloc] initWithFormat:@"%@", self.inputComment.text]
atIndex:rowIndex];
在[self.tableView beginUpdates];
以上我不是100%肯定,但我相信beginUpdates在插入动画之前拍摄行数的快照,并且由于你在更新中添加了数组,它仍然认为commentArray是空的。