Swift3.0 UITableView詳細(xì)代碼

///項(xiàng)目新建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標(biāo)題及字體顏色、大小
        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()
        
        //創(chuàng)建導(dǎo)航控制器
        
        let YXffNavc = YXffViewController.init(rootViewController: oneVc)
        
        window?.rootViewController = YXffNavc
        window?.makeKeyAndVisible()
        return true
    }

///正式進(jìn)入U(xiǎn)IViewController

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!
    }

///行高、區(qū)頭、區(qū)尾高度

    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
    }

////區(qū)頭、區(qū)尾實(shí)現(xiàn)方法及點(diǎn)擊事件

    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)組,進(jìn)入下一頁", 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點(diǎn)擊事件
    func footButtonClick(sender:UIButton)  {
        
        navigationController?.pushViewController(SecondTableViewController(), animated: true)
//        present(SecondTableViewController(), animated: true, completion: nil)
    }

///用xib創(chuàng)建cell,創(chuàng)建區(qū)頭

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)個(gè)"
        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
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容