i have a UITextfield, an i want to create for each insert a new UITextview with the typed Text from the UITextfield. But as you can see my Problem is that each new created Textview lays above the old Textview and i don´t know how to place the Textviews automatically under each other if i create a new one.
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];
An here i create the new Textviews, right after finishing the input from the textfield:
- (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];
}
I hope you can help me :)
EDIT:
I now use a UITableView but i get an Error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 1 into section 0, but there are only 0 rows in section 0 after the update'
My Code:
- (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 = @"";
}
I only want 1 Section!
Where is my Problem?
Just a suggestion, wouldn't it beasier to add your answers to an UITableView where each row is an answer.
If you don't want to go this route:
The reason its going on top of each other all the time is your y-Coordinate is always the same (self.messageView.frame.origin.y + self.messageView.frame.size.height) -- The height isn't increasing. Also, you're setting the instance of containerCommentView every insert of a new view.
** Edit for UITableView Changes **
You do only have one section, the sections are 0 based, so your section is actual section 0. If you had 2 sections, section 1 would be 0, 2 would be 1.
Try moving
int rowIndex = self.commentArray.count;
[self.commentArray insertObject:[[NSString alloc] initWithFormat:@"%@", self.inputComment.text]
atIndex:rowIndex];
above the [self.tableView beginUpdates];
I'm not 100% sure but I believe the beginUpdates is taking a snapshot of the row count before the insert animations, and since you're adding to the array within the updates it still thinks the commentArray is empty.