如何根据swift 3中静态表视图中的文本增加textview大小

时间:2017-09-04 12:02:10

标签: ios uitableview

我在Static TableView中有3个自定义单元格。所有细胞都有不同的高度。但我需要根据内容大小使newsDetail TextView的高度动态化。在tableview中很容易为标签制作动态。但它在静态TableView中没有为TextView工作。我搜索过但无法找到合适的解决方案,有人请你快速帮助我吗?我的代码:

import UIKit



class DetailTableView: UITableViewController {

@IBOutlet weak var myImageView: UIImageView!


@IBOutlet weak var newsHeadline: UILabel!


@IBOutlet weak var newsDetail: UITextView!

override func viewDidLoad() {
    super.viewDidLoad()

   newsDetail.text = "Barack Hussein Obama II (US: /bəˈrɑːk huːˈseɪn oʊˈbɑːmə/ (About this sound listen) bə-RAHK hoo-SAYN oh-BAH-mə;[1][2] born August 4, 1961) is an American politician who served as the 44th President of the United States from 2009 to 2017. He is the first African American to have served as president. He previously served in the U.S. Senate representing Illinois from 2005 to 2008 and in the Illinois State Senate. Obama was born in 1961 in Honolulu, Hawaii, two years after the territory was admitted to the Union as the 50th state. Raised largely in Hawaii, Obama also spent one year of his childhood in Washington State and four years in Indonesia. After graduating from Columbia University in 1983, he worked as a community organizer in Chicago. In 1988 Obama enrolled in Harvard Law School, where he was the first black president of the Harvard Law Review. After graduation, he became a civil rights attorney and professor, and taught constitutional law at the University of Chicago Law School from 1992 to 2004. Obama represented the 13th District for three terms in the Illinois Senate from 1997 to 2004, when he ran for the U.S. Senate."


    tableView.estimatedRowHeight = 44
    tableView.rowHeight = UITableViewAutomaticDimension


}

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 3
}

public override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}
}

3 个答案:

答案 0 :(得分:2)

首先,您需要确保为UITextView禁用滚动。 Authentication auth = Mockito.mock(Authentication.class); SecurityContext secCont = Mockito.mock(SecurityContext.class); Mockito.when(secCont.getAuthentication()).thenReturn(auth); SecurityContextHolder.setContext(secCont);

然后,在您的故事板中为您的UITextView设置自动布局约束,但请确保不要为它的高度设置一个。

您应该实现UITableViewDataSource方法commentsTextView.isScrollEnabled = falseheightForRowAt或使用UITableViewController对应的属性。

estimatedHeightForRowAt

如果您的UITextView是可编辑的,并且您希望它在您键入时动态调整大小,请将您的控制器作为委托,并按以下方式实施override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableViewAutomaticDimension } override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { return 77 }

textViewDidChange

答案 1 :(得分:0)

您的TextView不会使您的单元格高度更大,因为textView将以其内容大小增长。我的意思是它增加了他的卷轴而不是它的高度。 因此,如果你的textView没有增加他的身高,你的动态细胞高度也不会。

解决方案:使用编号为row = 0的标签

答案 2 :(得分:0)

您可以使用PTSMessagingCell

在资源中添加PTSMessagingCell.h和PTSMessagingCell.m文件。

#import "PTSMessagingCell.h"在您的标头文件中。

然后实现 UITableViewDataSource 方法 heightForRowAt ,如下所示:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
    let textSize = PTSMessagingCell.messageSize(newsDetail.text)
    return textSize.height + 2*PTSMessagingCell.textMarginVertical() + 5.0
}