美文网首页iOS学习笔记iOS 程序员iOS Developer
【iOS】让StaticCell和PrototypeCell“共

【iOS】让StaticCell和PrototypeCell“共

作者: 清無 | 来源:发表于2017-03-24 15:39 被阅读92次

1.需求场景

类似于静态固定功能性cell + 下边动态数据展示性cell,如图所示:

班级列表是动态数量的cell

2.问题分析

众所周知Storyboard支持TableViewController上的StaticCells类型,可实现上部功能性的cell,但下部的数量变化的cell就无法在该TableViewController上共存了

3.一种思路

注册多个PrototypeCells,但对顶部和下部的cell进行不同场景的复用,然后将复用的cell存储成属性,以模拟达到静态的作用

4.具体实现

Storyboard布局
// 属性
private var phoneCell: UITableViewCell!{
        didSet{
            // 做一些初始化设置
        }
    }
    private var passwordCell: UITableViewCell!{
        didSet{
             // 做一些初始化设置
        }
    }

// cell 复用
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let section = indexPath.section
        let row = indexPath.row
        
        if section == 0 && row == 0{ //手机号
            phoneCell = phoneCell ?? tableView.dequeueReusableCell(withIdentifier: "PhoneCell", for: indexPath)
            return phoneCell
        }
        else if section == 0 && row == 1{ //密码
            passwordCell = passwordCell ?? tableView.dequeueReusableCell(withIdentifier: "PasswordCell", for: indexPath)
            return passwordCell
        }
        
// 班级
        let cell = tableView.dequeueReusableCell(withIdentifier: "SchoolCell", for: indexPath)

        return cell
    }

5.最终效果

“静态”cell表现良好,不受影响

6.源码

https://github.com/BackWorld/HybridStaticPrototypeCellDemo

如果对你有帮助,别忘了给个⭐️或点个❤️哦。

相关文章

网友评论

    本文标题:【iOS】让StaticCell和PrototypeCell“共

    本文链接:https://www.haomeiwen.com/subject/kpkfottx.html