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)個"
        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)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,237評論 6 537
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,957評論 3 423
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,248評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,356評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,081評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,485評論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,534評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,720評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,263評論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,025評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,204評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,787評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,461評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,874評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,105評論 1 289
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,945評論 3 395
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,205評論 2 375

推薦閱讀更多精彩內(nèi)容