UITableView在模拟器中工作正常,但在我的物理设备中却无法正常工作

时间:2019-08-04 12:58:27

标签: swift 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
        }
    }
}

Simulator

Physical Device

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

1 个答案:

答案 0 :(得分:0)

通过添加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
    }