首页 > 解决方案 > UITableView 在模拟器中运行良好,但在我的物理设备中运行良好

问题描述

我目前正在构建一个应用程序,其中显示了我大学中所有可用的学院。我在 UITableView 中读取并填充了 .plist 文件中的数据。我目前正在尝试在单元格中显示一些详细信息,例如前缀和名称,当单击展开以显示有关教师的更多详细信息时。

我的 TableViewController 代码如下所示:

struct cellData {
    var opened = Bool()
    var Name = String()
    var Prefix = String()
    var sectionData = [String]()
}

class TableViewController: UITableViewController {

    //MARK: - Initialize variables
    var dataManager = DataManager()
    private var header = [String]()
    private var facultyDetails = [[String: String]]()
    var tableViewData = [cellData]()
    @IBOutlet var facultyTableView: UITableView!


    //MARK: - view did load

    override func viewDidLoad() {
        super.viewDidLoad()

        for data in dataManager.allSheet1s() {
            let dataInCell = cellData(opened: false, Name: data.item2!,
                                      Prefix: data.item1!, sectionData  [data.item0!,
                                                                         data.item2!,
                                                                         data.item1!,
                                                                         data.item3!,
                                                                         data.item4!,
                                                                         data.item5!,
                                                                         data.item6!,
                                                                         data.item7!,
                                                                         data.item8!,
                                                                         data.item9!,
                                                                         data.item10!
                ])

            tableViewData.append(dataInCell)

            facultyTableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "CustomCell")
        }


    }

    // MARK: - Table view delegate

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

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 1
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell

        if indexPath.row == 0 {


            cell.EmpID.text = tableViewData[indexPath.section].sectionData[0]
            cell.Name.text = tableViewData[indexPath.section].sectionData[1]
            cell.Prefix.text = tableViewData[indexPath.section].sectionData[2]
            cell.SchoolName.text = tableViewData[indexPath.section].sectionData[3]
            cell.BuildingName.text = tableViewData[indexPath.section].sectionData[4]
            cell.FloorNo.text = tableViewData[indexPath.section].sectionData[5]
            cell.CabinLocation.text = tableViewData[indexPath.section].sectionData[6]
            cell.RoomNo.text = tableViewData[indexPath.section].sectionData[7]
            cell.CabinNo.text = tableViewData[indexPath.section].sectionData[8]
            cell.IntercomNumber.text = tableViewData[indexPath.section].sectionData[9]
            cell.Email.text = tableViewData[indexPath.section].sectionData[10]

            return cell
        }
       return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if tableViewData[indexPath.section].opened == true {
            tableViewData[indexPath.section].opened = false

            let sections = IndexSet.init(integer: indexPath.section)
            tableView.reloadSections(sections, with: .right)

        }
        else {
            tableViewData[indexPath.section].opened = true

            let sections = IndexSet.init(integer: indexPath.section)
            tableView.reloadSections(sections, with: .left)


        }
    }



    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if tableViewData[indexPath.section].opened == true {
            return 400
        }
        else {
            return 70
        }
    }
}

模拟器

物理设备

现在您可以在模拟器中看到它运行良好。但是当我将它加载到物理设备中时,行中每个单元格的高度都会穿过它下面的其他单元格。

标签: swiftuitableview

解决方案


通过添加 cell.clipsToBounds = true 修改行代码的单元格

   override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell

        cell.clipsToBounds = true

        if indexPath.row == 0 {


            cell.EmpID.text = tableViewData[indexPath.section].sectionData[0]
            cell.Name.text = tableViewData[indexPath.section].sectionData[1]
            cell.Prefix.text = tableViewData[indexPath.section].sectionData[2]
            cell.SchoolName.text = tableViewData[indexPath.section].sectionData[3]
            cell.BuildingName.text = tableViewData[indexPath.section].sectionData[4]
            cell.FloorNo.text = tableViewData[indexPath.section].sectionData[5]
            cell.CabinLocation.text = tableViewData[indexPath.section].sectionData[6]
            cell.RoomNo.text = tableViewData[indexPath.section].sectionData[7]
            cell.CabinNo.text = tableViewData[indexPath.section].sectionData[8]
            cell.IntercomNumber.text = tableViewData[indexPath.section].sectionData[9]
            cell.Email.text = tableViewData[indexPath.section].sectionData[10]

            return cell
        }
       return cell
    }

并覆盖一个方法

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

推荐阅读