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

相关文章

MySQL分区表的具体使用

《MySQL分区表的具体使用》MySQL分区表通过规则将数据分至不同物理存储,提升管理与查询效率,本文主要介绍了MySQL分区表的具体使用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、分区的类型1. Range partition(范围分区)2. List partition(列表分区)3. H

Java中实现线程的创建和启动的方法

《Java中实现线程的创建和启动的方法》在Java中,实现线程的创建和启动是两个不同但紧密相关的概念,理解为什么要启动线程(调用start()方法)而非直接调用run()方法,是掌握多线程编程的关键,... 目录1. 线程的生命周期2. start() vs run() 的本质区别3. 为什么必须通过 st

使用SpringBoot整合Sharding Sphere实现数据脱敏的示例

《使用SpringBoot整合ShardingSphere实现数据脱敏的示例》ApacheShardingSphere数据脱敏模块,通过SQL拦截与改写实现敏感信息加密存储,解决手动处理繁琐及系统改... 目录痛点一:痛点二:脱敏配置Quick Start——Spring 显示配置:1.引入依赖2.创建脱敏

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

Go语言中Recover机制的使用

《Go语言中Recover机制的使用》Go语言的recover机制通过defer函数捕获panic,实现异常恢复与程序稳定性,具有一定的参考价值,感兴趣的可以了解一下... 目录引言Recover 的基本概念基本代码示例简单的 Recover 示例嵌套函数中的 Recover项目场景中的应用Web 服务器中

C#之List集合去重复对象的实现方法

《C#之List集合去重复对象的实现方法》:本文主要介绍C#之List集合去重复对象的实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C# List集合去重复对象方法1、测试数据2、测试数据3、知识点补充总结C# List集合去重复对象方法1、测试数据

SpringBoot读取ZooKeeper(ZK)属性的方法实现

《SpringBoot读取ZooKeeper(ZK)属性的方法实现》本文主要介绍了SpringBoot读取ZooKeeper(ZK)属性的方法实现,强调使用@ConfigurationProperti... 目录1. 在配置文件中定义 ZK 属性application.propertiesapplicati

Java Multimap实现类与操作的具体示例

《JavaMultimap实现类与操作的具体示例》Multimap出现在Google的Guava库中,它为Java提供了更加灵活的集合操作,:本文主要介绍JavaMultimap实现类与操作的... 目录一、Multimap 概述Multimap 主要特点:二、Multimap 实现类1. ListMult

CnPlugin是PL/SQL Developer工具插件使用教程

《CnPlugin是PL/SQLDeveloper工具插件使用教程》:本文主要介绍CnPlugin是PL/SQLDeveloper工具插件使用教程,具有很好的参考价值,希望对大家有所帮助,如有错... 目录PL/SQL Developer工具插件使用安装拷贝文件配置总结PL/SQL Developer工具插

MyBatis设计SQL返回布尔值(Boolean)的常见方法

《MyBatis设计SQL返回布尔值(Boolean)的常见方法》这篇文章主要为大家详细介绍了MyBatis设计SQL返回布尔值(Boolean)的几种常见方法,文中的示例代码讲解详细,感兴趣的小伙伴... 目录方案一:使用COUNT查询存在性(推荐)方案二:条件表达式直接返回布尔方案三:存在性检查(EXI