Swift快速集成控件篇(集成自定義UITableview的使用)

`- 1 我們看看效果


tab.gif

-2 支持代碼和sb/xib 我們先看看代碼是如何創(chuàng)建的

import UIKit
import SWTableViewCell      //如果需要有向左滑動(dòng)的功能 需要import SWTableViewCell

//需要繼承已寫好的類
class aadViewController: CustomTableViewViewController {
    
    let tab=UITableView()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tab.frame=CGRect.init(x: 0, y: 64, width: self.view.frame.width, height: self.view.frame.height)
        //注冊(cè)自定義(帶滑動(dòng)的Cell
        tab.register(testItemTableViewCell.self, forCellReuseIdentifier: "aabbcc")
        self.InitCongif(tab)
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    // 頂部刷新
    override func headerRefresh() {
        debugPrint("下拉刷新")
        
    }
    
    // 底部刷新
    var index = 0
    override   func footerRefresh(){
        debugPrint("上拉刷新")
        
        
        // 2次后模擬沒有更多數(shù)據(jù)
        index = index + 1
        if index > 2 {
            footer.resetNoMoreData()
            //            footer.automaticallyHidden=true
        }
    }
    
    var indeax=0
    //點(diǎn)擊空視圖的tableview
    override func EmptyOverlayClicked() {
        indeax+=3
        self.tab.reloadData()
    }
    
    //返回節(jié)的個(gè)數(shù)
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    
    //返回某個(gè)節(jié)中的行數(shù)
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return indeax
        
    }
    //為表視圖單元格提供數(shù)據(jù),該方法是必須實(shí)現(xiàn)的方法
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "aabbcc", for: indexPath as IndexPath)
            as! testItemTableViewCell  //自定義滑動(dòng)功能
        
        cell.textLabel?.text="我是測(cè)試的123"
        
        cell.rightUtilityButtons = self.rightButtons()  //自定義滑動(dòng)功能
        cell.delegate=self  //實(shí)現(xiàn)代理
        
        return cell
        
    }
    
    func rightButtons()->[UIButton]{  //自定義滑動(dòng)功能按鈕
        var btnitem=[UIButton]()
        
        for i in 0...3 {
            let btn = UIButton(type:.custom)
            btn.backgroundColor=CommonFunction.RGBA(23, g: 170, b: 255)
            btn.setTitle("測(cè)試1\(i)", for: .normal)
            btn.setTitleColor(UIColor.white, for: .normal)
            btn.titleLabel?.adjustsFontSizeToFitWidth=true
            btnitem.append(btn)
        }
        
        
        return btnitem
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        indeax=0
        //        self.tab.reloadData()
    }
    
    //只允許每次滑動(dòng)一個(gè)cell列表
    func swipeableTableViewCellShouldHideUtilityButtonsOnSwipe(cell: SWTableViewCell!) -> Bool {
        return true
    }
    
    func swipeableTableViewCell(cell: SWTableViewCell!, didTriggerLeftUtilityButtonWithIndex index: Int) {
        print("點(diǎn)擊:\(index)" )
    }
    
    func swipeableTableViewCell(cell: SWTableViewCell!, didTriggerRightUtilityButtonWithIndex index: Int) {
        print("點(diǎn)擊:\(index)" )
    }
    
    
}

class  testItemTableViewCell: SWTableViewCell {
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}
  • 3 界面代碼是一樣的,只是在界面拖動(dòng)一個(gè)UITableview 然后托線條到代碼里面,設(shè)置初始化self.InitCongif(tab) 跟平常用(UITableview)sb/xib一樣
@IBOutlet weak var tab: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        tab.frame=CGRectMake(0, 64, self.view.frame.width, self.view.frame.height) 
        self.InitCongif(tab)
aad.png
ddds.png

如需代碼,請(qǐng)移動(dòng)到--->http://www.itdecent.cn/p/0f950c180cb8

最后編輯于
?著作權(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)容