3

我有一个自定义单元格UITextView。我正在加载的文本长度UITextView从非常短变为非常长。所以请不要说使用标签代替”。我想将我的单元格保持在相同的高度,但UITextView如果文本太长则使用垂直滚动。

我已经尝试过这里的方法,但是由于我在我的cellForRowAtIndexPath事件中使用自定义单元初始化,所以观察者没有工作。我在initWithStyle函数下的单元格类中尝试了相同的方法,也没有工作。

你还有什么建议?或者我应该以不同的方式工作这个功能?任何帮助表示赞赏。

提前致谢。

4

3 回答 3

2

用这个 -

CGSize textSize = [myText sizeWithFont:whateverFont constrainedToSize:myTextViewBox.frame.size lineBreakMode:UILineBreakModeWordWrap];

...然后找出 contentOffset 并在将 UITextView 添加到表时进行设置。

于 2012-04-13T10:27:33.343 回答
1

在您的 TableView 数据源中 -

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger row = [indexPath row];

    static NSString *CellIdentifier = @"myCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    } else { // if there is already a subview, remove it
        while ([[cell.contentView subviews] count] > 0) {
            UIView *labelToClear = [[cell.contentView subviews] objectAtIndex:0];
            [labelToClear removeFromSuperview];
        }
    }


    UITextView *myTextView = // initialise your textView here, including setting its contentOffset

    [cell.contentView addSubview:myTextView];

    return cell;

}
于 2012-04-13T10:35:31.050 回答
1

这是我用来展示如何垂直对齐 UITextView的要点

于 2014-01-09T06:53:38.923 回答