swift中UITableView的简单使用

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

}

原文地址:https://www.cnblogs.com/hualuoshuijia/p/11637127.html

时间: 2024-08-01 13:32:18

swift中UITableView的简单使用的相关文章

Swift中协议的简单介绍

熟悉objective-c语言的同学们肯定对协议都不陌生,在Swift中苹果将 protocol 这种语法发扬的更加深入和彻底.Swift语言中的 protocol 不仅能定义方法还能定义属性,配合 extension 扩展的使用还能提供一些方法的默认实现,而且不仅类可以遵循协议,现在的枚举和结构体也能遵循协议了.基于此本文从 1,协议中定义属性和方法 , 2,协议的继承.聚合.关联类型 , 3,协议的扩展 , 4,Swift标准库中常见的协议 , 5,为什么要使用协议 5个方面结合自身的学习经

swift中collectionView的简单用法

之前写过OC中collectionView的用法,现在再看看swift中collectionView的用法,有兴趣的朋友,可以两者前后比较下区别,swift现在没有稳定下来,语法更新的比较快,但是它核心的一些东西,已经定型了.这些还是靠读者们自己去挖掘吧. //这里签署数据源和代理,此时不需要引入layout的代理,也可以.class AmonViewController: UIViewController ,UICollectionViewDataSource,UICollectionView

Swift学习--闭包的简单使用(三)

一.Swift中闭包的简单使用 override func viewDidLoad() { super.viewDidLoad() /** 闭包和OC中的Block非常相似 OC中的block类似于匿名函数 闭包是用来定义函数 作用:Block是用于保存一段点,在需要的时候执行 闭包也是用于保存一段点,在需要的时候执行做一个耗时操作 */ /** 闭包的基本格式: { (形参列表)->() in 需要执行的代码 } */ /** * 闭包的几种格式: 1.将闭包通过实参传递给函数 2.如果闭包是

swift中tableview的使用和注意事项

今天使用swift写了个简单的tableView,语法和用法上跟oc没多大的区别.但是还是有一些细节的地方需要注意一下的. 先上代码 import UIKit class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { var _tableView:UITableView? override func viewDidLoad() { super.viewDidLoad() _tableVie

如何在Swift中创建自定义控件

更新通知:这篇引导教程由Mikael Konutgan使用iOS 8和Swift语言重新制作,在Xcode6和7上测试通过.原始教程是由Colin Eberhardt团队制作的. 用户界面控件是许多应用的重要组成部分.使用这些控件,可以让用户查看应用的内容或与他们的应用进行交互.苹果提供了一个控件集,像UITextField, UIButton 和 UISwitch.灵活使用这些工具箱中已经存在的控件,可以让你创建各种各样的用户界面. 但是,有的时候你可能需要做一些与众不同的事情:库中的控件已经

Swift 中枚举

Swift 中枚举高级用法及实践 字数11017 阅读479 评论0 喜欢20 title: "Swift 中枚举高级用法及实践"date: 2015-11-20tags: [APPVENTURE]categories: [Swift 进阶]permalink: advanced-practical-enum-examples 原文链接=http://appventure.me/2015/10/17/advanced-practical-enum-examples/作者=Benedik

使用Koloda View在Swift中构建类似Tinder(国内的探探社交应用)的卡片

在过去几年中,随着社交网络应用程序的普及,约会应用程序也迅速出现.其中一个最突出的应用是Tinder.它不仅是一个很棒的约会应用程序,而且还在视图动画或过渡方面创建了新的iOS趋势,例如Tinder Card Swipe或Tinder UI 在这个iOS教程中,我们将学习如何在Swift中构建Tinder Swipe Cards,以便您可以将此功能包含在iOS应用程序中.目前有一些图库支持这种类型的可滑动卡片,其中一个是KolodaView.在本教程中,我们将向您展示如何使用代码示例在Swift

Swift 中的基础语法(二)

1.Swift 中的函数 /// 函数的定义 /// /// - Parameters: /// - x: 形参 /// - y: 形参 /// - Returns: 返回值 func sum(x: Int, y: Int) -> Int { return x + y } print(sum(x: 10, y: 20))   /* 外部参数就是在形参前面加了一个字 外部参数不会影响函数内部的细节 外部参数会让外部调用看起来更加直观 外部参数如果使用了'_',在外部调用函数时,会忽略形参的名字 &qu

swift中代理的使用

下面以自定义的UITableViewCell的代理为例,记录一下swift中代理的使用 controller中的代码如 1 // 2 // ViewController.swift 3 // simpleDemo 4 // 5 // Created by liubo on 16/7/25. 6 // Copyright © 2016年 liubo. All rights reserved. 7 // 8 9 import UIKit 10 11 class ViewController: UIV