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

`- 1 我們看看效果


tab.gif

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

import UIKit
import SWTableViewCell      //如果需要有向左滑動的功能 需要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)
        //注冊自定義(帶滑動的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次后模擬沒有更多數據
        index = index + 1
        if index > 2 {
            footer.resetNoMoreData()
            //            footer.automaticallyHidden=true
        }
    }
    
    var indeax=0
    //點擊空視圖的tableview
    override func EmptyOverlayClicked() {
        indeax+=3
        self.tab.reloadData()
    }
    
    //返回節的個數
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    
    //返回某個節中的行數
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return indeax
        
    }
    //為表視圖單元格提供數據,該方法是必須實現的方法
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "aabbcc", for: indexPath as IndexPath)
            as! testItemTableViewCell  //自定義滑動功能
        
        cell.textLabel?.text="我是測試的123"
        
        cell.rightUtilityButtons = self.rightButtons()  //自定義滑動功能
        cell.delegate=self  //實現代理
        
        return cell
        
    }
    
    func rightButtons()->[UIButton]{  //自定義滑動功能按鈕
        var btnitem=[UIButton]()
        
        for i in 0...3 {
            let btn = UIButton(type:.custom)
            btn.backgroundColor=CommonFunction.RGBA(23, g: 170, b: 255)
            btn.setTitle("測試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()
    }
    
    //只允許每次滑動一個cell列表
    func swipeableTableViewCellShouldHideUtilityButtonsOnSwipe(cell: SWTableViewCell!) -> Bool {
        return true
    }
    
    func swipeableTableViewCell(cell: SWTableViewCell!, didTriggerLeftUtilityButtonWithIndex index: Int) {
        print("點擊:\(index)" )
    }
    
    func swipeableTableViewCell(cell: SWTableViewCell!, didTriggerRightUtilityButtonWithIndex index: Int) {
        print("點擊:\(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 界面代碼是一樣的,只是在界面拖動一個UITableview 然后托線條到代碼里面,設置初始化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

如需代碼,請移動到--->http://www.lxweimin.com/p/0f950c180cb8

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容