ios学习--tableview 自定义cell扩展

1.自定义group中的cell个数

// 这里根据section 的下标  返回不同的cell数量
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 2 {
            return 3
        }else{
            return 1
        }
    }

2.自定义cell高度(根据分组或者cell的下标)

// 设置行高
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if(indexPath.section == 0){
            return 290
        }
        if (indexPath.section == 1) {
            return 100
        }
        if (indexPath.section == 2 && indexPath.row == 0) {
            return 30
        }
        return 120
    }

3. 根据section和row自定义每个cell

// 这里根据分组和每组下的row 分别对cell 进行了样式的调整
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // 创建单元格的复用标志
        let identifier = "reusedCell"
        var cell = tableView.dequeueReusableCell(withIdentifier: identifier)
        // 判断是否可重用
        if (cell == nil) {
            // 没有重用的则创建
            cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: identifier)
        }
        // 默认标签
        //        cell?.textLabel?.text = "Cell title here"
        // 默认描述标签
        //        cell?.detailTextLabel?.text = "Detail info here"
        
        cell?.backgroundColor = UIColor.white
        tableView.separatorStyle = UITableViewCellSeparatorStyle.none
        cell?.selectionStyle = UITableViewCellSelectionStyle.none
        
        if indexPath.section == 0 {
            let teacherImage: UIView = UIView(frame: CGRect(x: 10, y: 10, width: 50, height: 50))
            teacherImage.layer.contents = UIImage(named: "teacher1")?.cgImage
            
            let teacherName: UILabel = UILabel(frame: CGRect(x: 70, y: 15, width: 100, height: 40))
            teacherName.text = "阿珍老师"
            teacherName.font = UIFont.boldSystemFont(ofSize: 17)
            
            let workIntroduce: UILabel = UILabel(frame: CGRect(x: 10, y: 70, width: currentPhone.getWidth()-20, height: 180))
            workIntroduce.text = "  制冷,是使某一空间或物体的温度降到低于周围环境温度,并保持在规定低温状态的一门科学技术,它随着人们对低温条件的要求和社会生产力的提高而不断发展。制冷就是使某一空间或某物体达到低于其周围环境介质的温度,并维持这个低温的过程。(制冷技术与应用)制冷系统由4个基本部分即压缩机、冷凝器、节流部件、蒸发器组成... "
            workIntroduce.font = UIFont.systemFont(ofSize: 15)
            workIntroduce.textColor = UIColor.init(red: 80/255, green: 80/255, blue: 80/255, alpha: 1)
            workIntroduce.lineBreakMode = NSLineBreakMode.byWordWrapping
            workIntroduce.numberOfLines = 0
            
            let finishLabel: UILabel = UILabel(frame: CGRect(x: 180, y: 250, width: 100, height: 30))
            finishLabel.text = "已完成:30/50"
            finishLabel.font = UIFont.systemFont(ofSize: 14)
            finishLabel.textColor = UIColor.gray

            
            let readLabel: UILabel = UILabel(frame: CGRect(x: 300, y: 250, width: 100, height: 30))
            readLabel.text = "已读:40/50"
            readLabel.font = UIFont.systemFont(ofSize: 14)
            readLabel.textColor = UIColor.gray

            
            cell?.addSubview(readLabel)
            cell?.addSubview(finishLabel)
            cell?.addSubview(workIntroduce)
            cell?.addSubview(teacherName)
            cell?.addSubview(teacherImage)
        }
        
        if indexPath.section == 1 {
            let readButton: UIButton = UIButton(frame: CGRect(x: 10, y: 0, width: 50, height: 30))
            readButton.setTitle("已读", for: .normal)
            readButton.setTitleColor(UIColor.black, for: .normal)
            readButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            readButton.titleLabel?.textAlignment = .left
            
            let unreadButton: UIButton = UIButton(frame: CGRect(x: 70, y: 0, width: 50, height: 30))
            unreadButton.setTitle("未读", for: .normal)
            unreadButton.setTitleColor(UIColor.black, for: .normal)
            unreadButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            unreadButton.titleLabel?.textAlignment = .left
            
            let finishButton: UIButton = UIButton(frame: CGRect(x: 140, y: 0, width: 50, height: 30))
            finishButton.setTitle("已完成", for: .normal)
            finishButton.setTitleColor(UIColor.black, for: .normal)
            finishButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            finishButton.titleLabel?.textAlignment = .left
            
            selectSign.frame = CGRect(x: 10, y: 30, width: 50, height: 3)
            selectSign.backgroundColor = UIColor.init(red: 119/255, green: 173/255, blue: 233/255, alpha: 1)
            
            let studentShowView: UIView = UIView(frame: CGRect(x: 10, y: 35, width: currentPhone.getWidth()-20, height: 55))
            studentShowView.backgroundColor = UIColor.white
            
            let backSign = UILabel(frame: CGRect(x: 0, y: 30, width: currentPhone.getWidth(), height: 3))
            backSign.backgroundColor = UIColor.init(red: 241/255, green: 241/255, blue: 241/255, alpha: 1)
            
            let footSign = UILabel(frame: CGRect(x: 0, y: 100, width: currentPhone.getWidth(), height: 3))
            footSign.backgroundColor = UIColor.init(red: 241/255, green: 241/255, blue: 241/255, alpha: 1)
            
            cell?.addSubview(backSign)
            cell?.addSubview(selectSign)
            cell?.addSubview(studentShowView)
            cell?.addSubview(finishButton)
            cell?.addSubview(unreadButton)
            cell?.addSubview(readButton)
            cell?.addSubview(footSign)
        }
        if indexPath.section == 2 {
            if indexPath.row == 0 {
                let commentLabel = UILabel(frame: CGRect(x: 10, y: 5, width: 50, height: 30))
                commentLabel.text = "评论"
                commentLabel.textColor = normalColor
                commentLabel.font = UIFont.systemFont(ofSize: 14)
                cell?.addSubview(commentLabel)
            }else{
                let footSign = UILabel(frame: CGRect(x: 0, y: 120, width: currentPhone.getWidth(), height: 1))
                footSign.backgroundColor = UIColor.init(red: 153/255, green: 153/255, blue: 153/255, alpha: 1)
                
                cell?.addSubview(footSign)
            }
        }
        
        return cell!
    }

4. 根据section和row 自定义head和foot

同样的方法可以自动以footview

//  自定义headview
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headView = UIView()
        if section == 0{
            headView.frame = CGRect(x: headView.frame.origin.x, y: headView.frame.origin.y, width: headView.frame.width, height: 1)
            headView.backgroundColor = UIColor.gray
        }else {
            headView.backgroundColor = UIColor.orange
        }
        return headView
    }

也可以直接使用默认的,调整大小就好

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0{
        //返回高度
        }
        // 返回高度
    }

你可能感兴趣的:(iOS学习)