ไม่สามารถขยายมุมมองข้อความได้อย่างราบรื่นใน iOS 11 Swift

ฉันพยายามขยายมุมมองข้อความเมื่อฉันคลิกปุ่มอ่านเพิ่มเติม มันใช้งานได้บนอุปกรณ์ที่ต่ำกว่า iOS 11 แต่ไม่สามารถขยายได้อย่างราบรื่นมากในเวอร์ชัน iOS 11 ทำไม

เมื่อฉันแตะปุ่มอ่านเพิ่มเติมก็จะเรียกใช้โค้ดนี้

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
    }
}

ต่ำกว่าเวอร์ชัน iOS 11

ป้อนคำอธิบายรูปภาพที่นี่

เวอร์ชัน iOS 11

ป้อนคำอธิบายรูปภาพที่นี่

สิ่งที่ฉันได้ลอง

(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 แหล่งที่มา


คำตอบ (3)


หากคำตอบของ @Sid ไม่สามารถแก้ปัญหาของคุณได้

คุณควรเรียกlayoutIfNeeded, self.view.layoutIfNeeded() หลังจากการอัพเดตใดๆ ใน UI ใช้คิวหลักด้วย

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

มันจะอัพเดตเฟรมมุมมอง

person Naresh    schedule 02.03.2018
comment
ฉันยังลอง self.tableView.layoutIfNeeded() และ cell2.layoutIfNeeded() - person Lester; 02.03.2018
comment
ใช่แล้ว cell2.aboutTxtView_HeightConstraint.constant นี่คือข้อจำกัดด้านความสูง - person Lester; 02.03.2018

อย่าอัปเดตมุมมองตารางเพียงอัปเดตเซลล์เดียวภายในมุมมองตารางและตั้งค่าความสูงของเซลล์

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

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

จากนั้นอัปเดตเฉพาะเซลล์เกี่ยวกับฉัน มันจะอัปเดตเพียงเซลล์เดียวดังนั้นการขยายเซลล์จะราบรื่น

// 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()

อัปเดต :

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
ลองลบ estimatedHeightForRowAt - person Sid Mhatre; 02.03.2018
comment
ตรวจสอบการอัปเดตและใช้รหัสเดียวกันและลบ self.view.layoutIfNeeded() และสิ่งที่ฉันได้ลอง 1 และ 2 - person Sid Mhatre; 02.03.2018
comment
ฉันได้ลบรหัสในสิ่งที่ฉันมีแล้ว และความสูงที่จำกัดของมุมมองข้อความจะไม่อัปเดตหากฉันลบ self.view.layoutIfNeeded() และเอฟเฟกต์ 'laggy' หรือ 'เลื่อนไปด้านบน' ยังคงอยู่ที่นั่นเมื่อฉันแตะปุ่ม - person Lester; 02.03.2018
comment
self.view.layoutIfNeeded() ไม่จำเป็นสำหรับการขยายเซลล์ใน Uitableview หากคุณเปลี่ยนความสูงและเนื้อหาของเซลล์ ระบบจะอัปเดต - person Sid Mhatre; 02.03.2018

ฉันแก้ไขสิ่งนี้โดยเพิ่มรหัสสามรหัสนี้

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

ที่มา: https://stackoverflow.com/a/46350318/8393564
ที่มา: https://stackoverflow.com/a/46257601/8393564

person Lester    schedule 02.03.2018