使用多种单元格类型重构TableView

时间:2019-02-06 21:56:24

标签: swift uitableview

关于分解具有多个像元类型,像元大小的大型视图控制器的任何建议。由于每个单元格都有自己的委托,因此在浏览文件时,视图控制器会变得非常混乱。

这是针对多种细胞类型的标准方法吗?您将如何使用多个部分和单元构建多个对象。非常感谢!

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

    if section == 0 && sectionArray[0].isExpanded {
        return sectionArray.count
    } else if section == 0 && sectionArray[0].isExpanded == false {
        return sectionArray[0].section.count - 1
    }

    if section == 1 {
        return 3
    }
    return 0
}

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

    if indexPath.section == 0 && indexPath.row == 0 {

        let cell = tableView.dequeueReusableCell(withIdentifier: "CreateFavoriteIconCell", for: indexPath) as! CreateFavoriteIconCell
        cell.delegate = self
        return cell
    }

    if indexPath.section == 1 {
        switch indexPath.row {
        case 0:
            let cell = tableView.dequeueReusableCell(withIdentifier: "CreateFavoritePhotoCell", for: indexPath) as! CreateFavoritePhotoCell
            cell.delegate = self
            return cell
        case 1:
            let cell = tableView.dequeueReusableCell(withIdentifier: "CreateFavoriteLocationCell", for: indexPath) as! CreateFavoriteLocationCell
            return cell
        case 2:
            let cell = tableView.dequeueReusableCell(withIdentifier: "CreateFavoriteDescriptionCell", for: indexPath) as! CreateFavoriteDescriptionCell
            return cell
        default:
            return tableView.cellForRow(at: indexPath)!
        }
    }
    return tableView.cellForRow(at: indexPath)!
}

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

    if indexPath.section == 0 {
        switch indexPath.row {
        case 0:
            tableView.rowHeight = 58
        default:
            tableView.rowHeight = 0
        }
    }

    if indexPath.section == 1 {
        switch indexPath.row {
        case 0:
            tableView.rowHeight = 120
        case 1:
            tableView.rowHeight = 60
        case 2:
            tableView.rowHeight = 160
        default:
            tableView.rowHeight = 0
        }
    }

    return tableView.rowHeight
}

0 个答案:

没有答案
相关问题