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使用Mail构建邮件功能的完整指南

《Java使用Mail构建邮件功能的完整指南》JavaMailAPI是一个功能强大的工具,它可以帮助开发者轻松实现邮件的发送与接收功能,本文将介绍如何使用JavaMail发送和接收邮件,希望对大家有所... 目录1、简述2、主要特点3、发送样例3.1 发送纯文本邮件3.2 发送 html 邮件3.3 发送带

Linux虚拟机不显示IP地址的解决方法(亲测有效)

《Linux虚拟机不显示IP地址的解决方法(亲测有效)》本文主要介绍了通过VMware新装的Linux系统没有IP地址的解决方法,主要步骤包括:关闭虚拟机、打开VM虚拟网络编辑器、还原VMnet8或修... 目录前言步骤0.问题情况1.关闭虚拟机2.China编程打开VM虚拟网络编辑器3.1 方法一:点击还原VM

Android WebView无法加载H5页面的常见问题和解决方法

《AndroidWebView无法加载H5页面的常见问题和解决方法》AndroidWebView是一种视图组件,使得Android应用能够显示网页内容,它基于Chromium,具备现代浏览器的许多功... 目录1. WebView 简介2. 常见问题3. 网络权限设置4. 启用 JavaScript5. D

使用DeepSeek搭建个人知识库(在笔记本电脑上)

《使用DeepSeek搭建个人知识库(在笔记本电脑上)》本文介绍了如何在笔记本电脑上使用DeepSeek和开源工具搭建个人知识库,通过安装DeepSeek和RAGFlow,并使用CherryStudi... 目录部署环境软件清单安装DeepSeek安装Cherry Studio安装RAGFlow设置知识库总

Java捕获ThreadPoolExecutor内部线程异常的四种方法

《Java捕获ThreadPoolExecutor内部线程异常的四种方法》这篇文章主要为大家详细介绍了Java捕获ThreadPoolExecutor内部线程异常的四种方法,文中的示例代码讲解详细,感... 目录方案 1方案 2方案 3方案 4结论方案 1使用 execute + try-catch 记录

Python FastAPI入门安装使用

《PythonFastAPI入门安装使用》FastAPI是一个现代、快速的PythonWeb框架,用于构建API,它基于Python3.6+的类型提示特性,使得代码更加简洁且易于绶护,这篇文章主要介... 目录第一节:FastAPI入门一、FastAPI框架介绍什么是ASGI服务(WSGI)二、FastAP

Spring-AOP-ProceedingJoinPoint的使用详解

《Spring-AOP-ProceedingJoinPoint的使用详解》:本文主要介绍Spring-AOP-ProceedingJoinPoint的使用方式,具有很好的参考价值,希望对大家有所帮... 目录ProceedingJoinPoijsnt简介获取环绕通知方法的相关信息1.proceed()2.g

Python中Windows和macOS文件路径格式不一致的解决方法

《Python中Windows和macOS文件路径格式不一致的解决方法》在Python中,Windows和macOS的文件路径字符串格式不一致主要体现在路径分隔符上,这种差异可能导致跨平台代码在处理文... 目录方法 1:使用 os.path 模块方法 2:使用 pathlib 模块(推荐)方法 3:统一使

SpringBoot项目启动错误:找不到或无法加载主类的几种解决方法

《SpringBoot项目启动错误:找不到或无法加载主类的几种解决方法》本文主要介绍了SpringBoot项目启动错误:找不到或无法加载主类的几种解决方法,具有一定的参考价值,感兴趣的可以了解一下... 目录方法1:更改IDE配置方法2:在Eclipse中清理项目方法3:使用Maven命令行在开发Sprin

Maven pom.xml文件中build,plugin标签的使用小结

《Mavenpom.xml文件中build,plugin标签的使用小结》本文主要介绍了Mavenpom.xml文件中build,plugin标签的使用小结,文中通过示例代码介绍的非常详细,对大家的学... 目录<build> 标签Plugins插件<build> 标签<build> 标签是 pom.XML