繁体   English   中英

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

[英]how to increase the textview size according to text in static table view in swift 3

我在静态TableView中有3个自定义单元格。 所有单元格都有不同的高度。 但是我需要根据其内容大小使newsDetail TextView的高度动态变化。 在表格视图中动态制作标签很容易。 但是,对于静态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
}
}

首先,您需要确保UITextView的滚动被禁用。 commentsTextView.isScrollEnabled = false

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

您应该实现UITableViewDataSource方法heightForRowAtestimatedHeightForRowAt或使用UITableViewController对应的属性。

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

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 77
}

如果您的UITextView是可编辑的,并且您希望在键入时动态调整它的大小,则使它成为控制器的委托,并实现textViewDidChange ,如下所示:

extension ContactDetailsViewController: UITextViewDelegate {

// Resize textview depending on it's content
func textViewDidChange(_ textView: UITextView) {

    // Get textview content size
    let size = textView.bounds.size
    let newSize = textView.sizeThatFits(CGSize(width: size.width, height: .greatestFiniteMagnitude))

    // Resize the cell only when cell's size is changed
    if size.height != newSize.height {
        UIView.setAnimationsEnabled(false)
        tableView.beginUpdates()
        tableView.endUpdates()
        UIView.setAnimationsEnabled(true)

        let indexPath = IndexPath(row: 2, section: 0)
        tableView.scrollToRow(at: indexPath, at: .bottom, animated: false)
    }
}

您的TextView不会使您的单元格高度变大,因为textView的内容大小会增加。 我的意思是这增加了他的滚动而不是它的高度。 因此,如果您的textView没有增加他的高度,那么动态单元格的高度也不会增加。

解决方案:使用行数= 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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM