swiftnbsp;UITableView具体使用方法

2024-08-29 14:48

本文主要是介绍swiftnbsp;UITableView具体使用方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

研究了一下tableview的基本功能写了个dome 稍后附上git地址

 

 

效果

 

swift <wbr>UITableView具体使用方法   swift <wbr>UITableView具体使用方法   

代码

 

//

//  ViewController.swift

//  tableview

//

//  Created by admin on 16/6/2.

//  Copyright © 2016 ming. All rights reserved.

//

 

import UIKit

 

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{

 

    //屏幕的宽和高

    let width = UIScreen.mainScreen().bounds.width

    let height = UIScreen.mainScreen().bounds.height

 

    let top:CGFloat = 30

   

    @IBOutlet weak var list: UITableView!

   

    let str = ["这是标题1","这是标题2","这是标题3"]

   

   

    override funcviewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

       

        self.list.delegate = self

        self.list.dataSource = self

       

        let titleFrame:CGRect = CGRectMake(0, top, width, height)

        self.list.frame = titleFrame

       

    }

 

    override funcdidReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    // 返回表格行数(也就是返回控件数)

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return str.count

    }

   

    // 创建各单元显示内容(创建参数indexPath指定的单元)

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)

        -> UITableViewCell

    {

        let h = height/10

        let titleFrame:CGRect = CGRectMake(0, 0, width/2, h)

        let cellFrame:CGRect = CGRectMake(0, 0, width, h)

       

        // 为了提供表格显示性能,已创建完成的单元需重复使用

        let identify:String = "TableViewCell"

        // 同一形式的单元格重复使用,在声明时已注册

        let cell = list.dequeueReusableCellWithIdentifier(identify,forIndexPath: indexPath) as! TableViewCell

       

        cell.title.text = str[indexPath.row]

        cell.title.frame = titleFrame

        cell.frame = cellFrame

        return cell

    }

   

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){

        alert(str[indexPath.row])

    }

    //侧滑选项

    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?

    {

        let a = UITableViewRowAction(style: .Normal, title: "选项一") { action, index in

            self.alert("选项一")

        }

       

        a.backgroundColor = UIColor.redColor()

       

        let b = UITableViewRowAction(style: .Normal, title: "选项2") { (action: UITableViewRowAction!, indexPath: NSIndexPath) -> Void in

            self.alert("选项二")

        }

       

        b.backgroundColor = UIColor.grayColor()

       

        let c = UITableViewRowAction(style: .Normal, title: "选项3") { action, index in

            self.alert("选项三")

        }

        c.backgroundColor = UIColor.blueColor()

       

        return [a, b, c]

    }

    //返回cell高度

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

       

        return height/10

    }

   

    func alert(str:String){

       

        //提示窗

        let alertViewController:UIAlertController = UIAlertController(title:"提示", message:str, preferredStyle: UIAlertControllerStyle.Alert)

        let alertView = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)

        alertViewController.addAction(alertView)

        self.presentViewController(alertViewController, animated: true, completion: nil)

    }

 

}

 

 

 

 

 

TableViewCell代码

 

import UIKit

 

class TableViewCell: UITableViewCell {

 

 

    @IBOutlet weak var title: UILabel!

   

    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

    }

 

}

 

补充 设置cell没有点击效果

cell.selectionStyle = UITableViewCellSelectionStyle.None

这篇关于swiftnbsp;UITableView具体使用方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1118165

相关文章

Java中流式并行操作parallelStream的原理和使用方法

《Java中流式并行操作parallelStream的原理和使用方法》本文详细介绍了Java中的并行流(parallelStream)的原理、正确使用方法以及在实际业务中的应用案例,并指出在使用并行流... 目录Java中流式并行操作parallelStream0. 问题的产生1. 什么是parallelS

MySQL数据库双机热备的配置方法详解

《MySQL数据库双机热备的配置方法详解》在企业级应用中,数据库的高可用性和数据的安全性是至关重要的,MySQL作为最流行的开源关系型数据库管理系统之一,提供了多种方式来实现高可用性,其中双机热备(M... 目录1. 环境准备1.1 安装mysql1.2 配置MySQL1.2.1 主服务器配置1.2.2 从

Linux join命令的使用及说明

《Linuxjoin命令的使用及说明》`join`命令用于在Linux中按字段将两个文件进行连接,类似于SQL的JOIN,它需要两个文件按用于匹配的字段排序,并且第一个文件的换行符必须是LF,`jo... 目录一. 基本语法二. 数据准备三. 指定文件的连接key四.-a输出指定文件的所有行五.-o指定输出

Linux jq命令的使用解读

《Linuxjq命令的使用解读》jq是一个强大的命令行工具,用于处理JSON数据,它可以用来查看、过滤、修改、格式化JSON数据,通过使用各种选项和过滤器,可以实现复杂的JSON处理任务... 目录一. 简介二. 选项2.1.2.2-c2.3-r2.4-R三. 字段提取3.1 普通字段3.2 数组字段四.

Linux kill正在执行的后台任务 kill进程组使用详解

《Linuxkill正在执行的后台任务kill进程组使用详解》文章介绍了两个脚本的功能和区别,以及执行这些脚本时遇到的进程管理问题,通过查看进程树、使用`kill`命令和`lsof`命令,分析了子... 目录零. 用到的命令一. 待执行的脚本二. 执行含子进程的脚本,并kill2.1 进程查看2.2 遇到的

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三

k8s按需创建PV和使用PVC详解

《k8s按需创建PV和使用PVC详解》Kubernetes中,PV和PVC用于管理持久存储,StorageClass实现动态PV分配,PVC声明存储需求并绑定PV,通过kubectl验证状态,注意回收... 目录1.按需创建 PV(使用 StorageClass)创建 StorageClass2.创建 PV

Python版本信息获取方法详解与实战

《Python版本信息获取方法详解与实战》在Python开发中,获取Python版本号是调试、兼容性检查和版本控制的重要基础操作,本文详细介绍了如何使用sys和platform模块获取Python的主... 目录1. python版本号获取基础2. 使用sys模块获取版本信息2.1 sys模块概述2.1.1

Python实现字典转字符串的五种方法

《Python实现字典转字符串的五种方法》本文介绍了在Python中如何将字典数据结构转换为字符串格式的多种方法,首先可以通过内置的str()函数进行简单转换;其次利用ison.dumps()函数能够... 目录1、使用json模块的dumps方法:2、使用str方法:3、使用循环和字符串拼接:4、使用字符