Tidak dapat memperluas tampilan teks dengan lancar di iOS 11 Swift

Saya mencoba memperluas tampilan teks ketika saya mengklik Tombol Baca Lebih Lanjut. Ini berfungsi pada perangkat di bawah iOS 11 tetapi tidak dapat diperluas dengan mulus di versi iOS 11. Mengapa?

Ketika saya mengetuk tombol baca lebih lanjut akan menjalankan kode ini.

tableView.beginUpdates()
let txtView = cell2.aboutTxtView!
let txtStr = cell2.aboutTxtView.text!
let btn = cell2.aboutReadBtn!

if self.aboutTag == 0{
    let height = getRowHeightFromTextView(strText: txtStr, txtView: txtView)
    cell2.aboutTxtView_HeightConstraint.constant = height
    self.view.layoutIfNeeded()
    btn.setTitle("Show Less", for: .normal)
    self.aboutTag = 1
    self.aboutHeight = 140.0 + height - 84.0
    tableView.endUpdates()
}
else
{
    cell2.aboutTxtView_HeightConstraint.constant = 84
    self.view.layoutIfNeeded()
    btn.setTitle("Read More", for: .normal)
    self.aboutTag = 0
    self.aboutHeight = 140.0
    tableView.endUpdates()
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    if indexPath.section == 1
    {
        return aboutHeight
    }
}

Di bawah Versi iOS 11

masukkan deskripsi gambar di sini

Versi iOS 11

masukkan deskripsi gambar di sini

Apa yang telah saya coba.

(1)

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

(2)

override func viewDidAppear(_ animated: Bool)
{
    super.viewDidAppear(animated)

    if #available(iOS 11, *)
    {
        self.tableView.contentInsetAdjustmentBehavior = .never
    }
}

person Lester    schedule 02.03.2018    source sumber


Jawaban (3)


Jika jawaban @ Sid tidak menyelesaikan masalah Anda.

Anda harus memanggil layoutIfNeeded, self.view.layoutIfNeeded() setelah pembaruan apa pun di UI. Juga, gunakan antrian utama untuk itu.

DispatchQueue.main.async {
   self.view.layoutIfNeeded()
}

Ini akan memperbarui bingkai tampilan.

person Naresh    schedule 02.03.2018
comment
Saya juga mencoba self.tableView.layoutIfNeeded() dan cell2.layoutIfNeeded() - person Lester; 02.03.2018
comment
ya, cell2.aboutTxtView_HeightConstraint.constant, ini adalah batasan ketinggian - person Lester; 02.03.2018

Jangan perbarui tampilan tabel, cukup perbarui satu sel di dalam tampilan tabel dan atur tinggi sel.

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    if indexPath.section == 1
    {
        return aboutHeight
    }
    return UITableViewAutomaticDimension
}

Kemudian Perbarui hanya sel Tentang Saya. Ini hanya akan memperbarui satu sel sehingga perluasan sel akan lancar.

// Add your code to update cell height 
self.tableView.beginUpdates()
let indexPath = IndexPath(row: 0, section: 1)
self.tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
self.tableView.endUpdates()

Pembaruan :

self.tableView.beginUpdates()
let indexPath = IndexPath(row: 0, section: 1)

let txtView = cell2.aboutTxtView!
let txtStr = cell2.aboutTxtView.text!
let btn = cell2.aboutReadBtn!

if self.aboutTag == 0{
    let height = getRowHeightFromTextView(strText: txtStr, txtView: txtView)
    cell2.aboutTxtView_HeightConstraint.constant = height
    btn.setTitle("Show Less", for: .normal)
    self.aboutTag = 1
    self.aboutHeight = 140.0 + height - 84.0
}
else
{
    cell2.aboutTxtView_HeightConstraint.constant = 84
    btn.setTitle("Read More", for: .normal)
    self.aboutTag = 0
    self.aboutHeight = 140.0
}
tableView.endUpdates()

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    if indexPath.section == 1
    {
        return aboutHeight
    }
    return UITableViewAutomaticDimension
}
person Sid Mhatre    schedule 02.03.2018
comment
Coba hapus estimatedHeightForRowAt. - person Sid Mhatre; 02.03.2018
comment
Periksa Perbarui dan gunakan kode yang sama dan hapus self.view.layoutIfNeeded() dan Apa yang telah saya coba 1 dan 2. - person Sid Mhatre; 02.03.2018
comment
Saya telah menghapus kode yang saya miliki. dan tinggi batasan tampilan teks tidak akan diperbarui jika saya menghapus self.view.layoutIfNeeded() , dan juga efek 'laggy' atau 'scroll to top' masih ada saat saya mengetuk tombol - person Lester; 02.03.2018
comment
self.view.layoutIfNeeded() Tidak perlu memperluas sel di Uitableview . Jika Anda mengubah tinggi dan isi sel maka itu akan diperbarui. - person Sid Mhatre; 02.03.2018

Saya memperbaikinya dengan menambahkan tiga kode ini.

self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;

Sumber: https://stackoverflow.com/a/46350318/8393564
Sumber: https://stackoverflow.com/a/46257601/8393564

person Lester    schedule 02.03.2018