• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

swift中UITableView的简单使用

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

 

import UIKit

/// TableViewDataSource展示
class YJTableViewDataSourceVC: UIViewController, UITableViewDataSource {
    
    /// 数据源
    var data = [[Int]]()
    /// UITableView
    @IBOutlet weak var tableView: UITableView!
    
    // MARK: - view
    override func viewDidLoad() {
        super.viewDidLoad()
        // 以组演示,填充相关测试数据
        var section = [Int]()
        for _ in 0..<5 {
            section.removeAll()
            for row in 0..<10 {
                section.append(row)
            }
            self.data.append(section)
        }
    }
    
    // MARK: - 开起和关闭tableView编辑状态
    @IBAction func onClickEdit(_ sender: AnyObject) {
        self.tableView.setEditing(!self.tableView.isEditing, animated: true)
    }
    
    // MARK: - UITableViewDataSource
    // MARK: 有几组
    func numberOfSections(in tableView: UITableView) -> Int  {
        print(#function)
        return self.data.count
    }
    
    // MARK: 每一组有几个元素
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print(#function)
        return self.data[section].count
    }
    
    // MARK: 生成Cell
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        print(#function)
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
        }
        cell?.textLabel?.text = "\(self.data[indexPath.section][indexPath.row])"
        return cell!
    }
    
    // MARK: 组Header
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        print(#function)
        return "\(section)--Header"
    }
    
    // MARK: 组Footer
    func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
        print(#function)
        return "\(section)--Footer"
    }
    
    // MARK: 索引
    func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        print(#function)
        var sectionTitles = [String]()
        for i in 0..<self.data.count {
            sectionTitles.append("\(i)")
        }
        return sectionTitles
    }
    
    // MARK: 索引对应的组
    func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
        print(#function)
        return Int(title) ?? 0
    }
    
    // MARK: 能否编辑
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        print(#function)
        return true
    }
    
    // MARK: 增加和删除
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        print(#function)
        if editingStyle == .delete {
            // Delete the row from the data source
            self.data[indexPath.section].remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }
    }
    
    // MARK: 能否移动
    func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        print(#function)
        return true
    }
    
    // MARK: 移动cell
    func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        print(#function)
        // 处理源数据
        let sourceData = self.data[sourceIndexPath.section][sourceIndexPath.row]
        self.data[sourceIndexPath.section].remove(at: sourceIndexPath.row)
        self.data[destinationIndexPath.section].insert(sourceData, at: destinationIndexPath.row)
    }
    
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift中的&quot;dealloc&quot;发布时间:2022-07-13
下一篇:
[Swift]LeetCode1219.黄金矿工|PathwithMaximumGold发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap