【问题标题】:Automatic placing of UITextviews under each other自动将 UITextviews 放置在彼此下方
【发布时间】:2013-07-24 15:38:36
【问题描述】:

我有一个 UITextfield,我想为每个插入一个新的 UITextview 创建一个带有来自 UITextfield 的输入文本。但是正如您所看到的,我的问题是每个新创建的 Textview 都位于旧 Textview 之上,如果我创建一个新的,我不知道如何将 Textviews 自动放置在彼此之下。

我的 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];

在完成文本字段的输入后,我在这里创建了新的 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 个部分!

我的问题在哪里?

【问题讨论】:

    标签: iphone objective-c uitextfield uitextview uitextfielddelegate


    【解决方案1】:

    只是一个建议,将您的答案添加到每行都是答案的 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是空的。

    【讨论】:

    • 感谢您的精彩提示! ,我已经用 UITableView 试过了,但现在我遇到了一些问题....查看我的编辑
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    • 1970-01-01
    相关资源
    最近更新 更多