0

У меня есть UITextfield, я хочу создать для каждой вставки новое UITextview с введенным текстом из поля UITextfield. Но, как вы видите, моя проблема в том, что каждый новый созданный Textview лежит над старым Textview, и я не знаю, как разместить Textviews автоматически друг под другом, если я создаю новый.Автоматическое размещение UITextviews под друг друга

Мои 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]; 

}

Я надеюсь, что вы можете мне помочь:)

EDIT:

Теперь я использую 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 секцию!

Где моя проблема?

ответ

0

Просто предложение, не было бы лишним добавить ответы на UITableView, где каждая строка является ответом.

Если вы не хотите идти по этому пути:

Причина его происходит поверх друг друга все время ваш у-координата всегда одинакова (self.messageView.frame.origin.y + self.messageView.frame.size.height). Высота не увеличивается. Кроме того, вы устанавливаете экземпляр containerCommentView для каждой вставки нового представления.

** Edit для 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 принимает снимок количество строк перед вставкой анимации, и так как вы добавление в массив обновлений nks commentArray пуст.

+0

Спасибо за отличный совет !! , я попробовал его с UITableView, но теперь я получаю некоторые проблемы .... см. мое редактирование – Davis

+0

Ответ обновлен, чтобы отразить ваши изменения. –

+0

Спасибо, теперь он отлично работал :) – Davis