【发布时间】:2011-09-27 17:25:22
【问题描述】:
我正在尝试创建一个简单的日记应用。在其中我有一个 ShowPostTableViewController,我想在其中显示每个帖子。在我的 tableView 中,我有一个包含两个单元格的部分,一个用于标题,一个用于帖子的正文。标题在 UITextField 内,正文在 UITextView 内。我都这样声明:
UITextField * postHeadline;
UITextView * postText;
然后我在我的实现文件中合成它们。我在 willDisplayCell 方法中设置了 tableView 的单元格。它看起来像这样:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
tableView.allowsSelection = NO;
CGRect wholeWindow = [self.view bounds];
float headlineHeight = 40;
CGRect headlineRect = CGRectMake(10, 10, wholeWindow.size.width, headlineHeight);
CGRect bodyRect = CGRectMake(wholeWindow.origin.y, wholeWindow.origin.x,
wholeWindow.size.width, wholeWindow.size.height);
postHeadline = [[UITextField alloc] initWithFrame:headlineRect];
postText = [[UITextView alloc] initWithFrame:bodyRect];
NSString * headline = [[NSString alloc] initWithFormat:@"%@",[currentPostArray
objectAtIndex:0]];
NSString * body = [[NSString alloc] initWithFormat:@"%@",[currentPostArray
objectAtIndex:1]];
postHeadline.text = headline;
postHeadline.font = [UIFont fontWithName:@"Helvetica-Bold" size:24.0];
postText.text = body;
postText.backgroundColor = [UIColor colorWithRed:254.0/255.0 green:255.0/255.0
blue:237.0/255.0 alpha:1];
postText.font = [UIFont fontWithName:@"Georgia" size:17.0];
postText.scrollEnabled = NO;
postText.delegate = self;
if (indexPath.row == 0) {
[cell.contentView addSubview:postHeadline];
}
if (indexPath.row == 1) {
[cell.contentView addSubview:postText];
CGRect frame = postText.frame;
frame.size.height = postText.contentSize.height;
postText.frame = frame;
}
}
是的,我是一个初学者,可能会疯狂地泄漏内存。一个与此相关的问题。似乎我的 UITextField postHeadline 设置了两次,我无法删除它,因为它是相同文本的两层。我该如何解决?
回到我原来的问题。我的 cellForRowAtIndexPath 几乎完好无损。我已经为我的 UITextView 设置了委托方法(认为问题就在这里)。我找到了一个几乎可以在这里工作的解决方案:UITextView change height instead of scroll。
它们看起来像这样:
- (void)textViewDidBeginEditing:(UITextView *)textView
{
textView.frame =CGRectMake(textView.frame.origin.x,textView.frame.origin.y,textView.
frame.size.width,textView.frame.size.height + 100);
}
这个方法应该调整textView的大小:
- (void)textViewDidChange:(UITextView *)textView
{
CGFloat fontHeight = (textView.font.ascender - textView.font.descender) + 1;
CGRect newTextFrame = textView.frame;
newTextFrame.size = textView.contentSize;
newTextFrame.size.height = newTextFrame.size.height + fontHeight;
textView.frame = newTextFrame;
}
我这样设置行高:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath
{
NSString * body = [[NSString alloc] initWithFormat:@"%@",[currentPostArray
objectAtIndex:1]];
CGSize bodySize = [body sizeWithFont:[UIFont fontWithName:@"Georgia" size:17.0]
constrainedToSize:CGSizeMake(self.view.frame.size.width,CGFLOAT_MAX)];
if (indexPath.section == 0 && indexPath.row == 1) {
return bodySize.height + 100;
}
return 50;
}
这件事几乎奏效了。当用户点击返回时,文本视图似乎展开了,但它只工作了 14 次,然后光标隐藏在键盘后面。
任何解决这个问题的想法和技巧都会很棒!
谢谢 // 安德斯
编辑
我通过将大部分 willDisplayCell 方法代码移动到我的 viewDidLoad 方法解决了“标题设置两次”的问题。而且我认为我已经本地化了调整问题的位置。我认为它在我的 heightForRowAtIndexPath 方法中。目前看起来是这样的:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath
{
NSString * body = [[NSString alloc] initWithFormat:@"%@",[currentPostArray
objectAtIndex:1]];
CGSize bodySize = [body sizeWithFont:[UIFont fontWithName:@"Georgia" size:17.0]
constrainedToSize:CGSizeMake(self.view.frame.size.width,CGFLOAT_MAX)];
if (indexPath.section == 0 && indexPath.row == 1) {
return bodySize.height + 100;
}
return 50;
}
由于数组及其内容在 viewDidLoad 方法中设置,因此大小保持不变。因此,我认为我也需要在 TextView 的内容之后动态增加单元格的大小。因为当我这样写的时候:
if (indexPath.section == 0 && indexPath.row == 1) {
return 1000;
}
单元格的大小会增加,用户可以在键盘妨碍之前多次点击“返回”。有没有办法动态增加单元格的高度?
【问题讨论】:
标签: iphone objective-c ios