Swift3.0 UITableView详细代码

///项目新建UINavigationController

import UIKit

class YXffViewController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBar.barTintColor = UIColor.init(colorLiteralRed: 86/255.0, green: 192/255.0, blue: 248/255.0, alpha: 1.0)///navigationBar背景及整体颜色
        
        navigationBar.tintColor = UIColor.white ///navigationBar左右Item颜色
        
        let dict:NSDictionary = [NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName : UIFont.boldSystemFont(ofSize: 16.0)]///navigationBar标题及字体颜色、大小
        navigationBar.titleTextAttributes = dict as? [String : Any]
        ///去掉navigationBar下面横线,
        #if true
        ///方法一:
        navigationBar.barStyle = UIBarStyle.blackTranslucent
        ///方法二:
        #else
        let listViews = navigationBar.subviews
        for (_,value) in listViews.enumerated() {
            if value.isKind(of: UIView.self) {
                let subViews = value.subviews
                for (_,imageV) in subViews.enumerated() {
                    if imageV.isKind(of: UIImageView.self) {
                        imageV.isHidden = true
                    }
                }
            }
        }
        #endif
    }

///AppDelegate代码

import UIKit
import CoreData

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.backgroundColor = UIColor.white
        let oneVc = ViewController()
        
        //创建导航控制器
        
        let YXffNavc = YXffViewController.init(rootViewController: oneVc)
        
        window?.rootViewController = YXffNavc
        window?.makeKeyAndVisible()
        return true
    }

///正式进入UIViewController

import UIKit


class ViewController: UIViewController , UITableViewDataSource, UITableViewDelegate{
    var tableView = UITableView()
    let Kwidth = UIScreen.main.bounds.size.width
    let Kheight = UIScreen.main.bounds.size.height
override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "首页"
        tableView = UITableView.init(frame: CGRect(x: 0.0, y: 0, width: Kwidth, height: Kheight), style: UITableViewStyle.grouped)
        tableView.delegate = self
        tableView.dataSource = self
        view.addSubview(tableView)
    }

///UITableView代理方法

   func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identfier = "cell"
        var cell = tableView.dequeueReusableCell(withIdentifier: identfier)
        if (cell == nil) {
            cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: identfier)
            cell?.selectionStyle = UITableViewCellSelectionStyle.none
        }
        cell?.textLabel?.text = "第\(indexPath.section)组-第\(indexPath.row)行"
        return cell!
    }

///行高、区头、区尾高度

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 55.0
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 8.0
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 45.0
    }

////区头、区尾实现方法及点击事件

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headView = UIView.init(frame: CGRect(x: 0.0, y: 0.0, width: Kwidth, height: 8.0))
        headView.backgroundColor = UIColor.init(colorLiteralRed: 200/255.0, green: 200/255.0, blue: 200/255.0, alpha: 1.0)
        
        return headView
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footView = UIView.init(frame: CGRect(x: 0.0, y: 0.0, width: Kwidth, height: 45.0))
        let footButton = UIButton.init(frame: CGRect(x: 0.0, y: 0.0, width: Kwidth, height: 45.0))
        footView.addSubview(footButton)
        footButton.backgroundColor = UIColor.init(colorLiteralRed: 245/255.0, green: 245/255.0, blue: 245/255.0, alpha: 1.0)
        footButton.setTitleColor(UIColor.black, for: UIControlState.normal)
        footButton.setTitle("\(section)组,进入下一页", for: UIControlState.normal)
        footButton.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 150, bottom: 0, right: 0)
        footButton.addTarget(self, action: #selector(footButtonClick(sender:)), for: UIControlEvents.touchUpInside)
        
        return footView
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        print("+++++++++++++++",indexPath)
    }
    /// Mark:Button点击事件
    func footButtonClick(sender:UIButton)  {
        
        navigationController?.pushViewController(SecondTableViewController(), animated: true)
//        present(SecondTableViewController(), animated: true, completion: nil)
    }

///用xib创建cell,创建区头

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = "yxffTableViewCell"
        var cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? yxffTableViewCell
        #if true
            if (cell == nil) {
                tableView.register(UINib(nibName: "yxffTableViewCell", bundle: nil), forCellReuseIdentifier: identifier)
                cell = (tableView.dequeueReusableCell(withIdentifier: identifier) as? yxffTableViewCell)
            }
        #else
            if (cell == nil) {
                cell = UINib(nibName: "yxffTableViewCell", bundle: nil).instantiate(withOwner: nil, options: nil).first as? yxffTableViewCell
            }
        #endif
        cell?.cellLabel.text = "第\(indexPath.row)个"
        return cell!
    }
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headView = UINib(nibName: "HeaderSectionView", bundle: nil).instantiate(withOwner: nil, options: nil).first as! HeaderSectionView
        headView.frame = CGRect(x: 0, y: 0, width: Kwidth, height: 46.0)
        headView.backgroundColor = UIColor.init(colorLiteralRed: 248/255.0, green: 248/255.0, blue: 248/255.0, alpha: 1.0)
        headView.headLabel.text = "我是\(section)组头"
        return headView
    }

你可能感兴趣的:(Swift3.0 UITableView详细代码)