swift自定义表格控件(UITableView)

 更新时间:2022年01月27日 08:37:46   作者:PandaMohist  
这篇文章主要为大家详细介绍了swift自定义表格控件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

本文实例为大家分享了swift自定义表格控件的具体代码,供大家参考,具体内容如下

1、效果图

2、控件

storyboard上的控件就2个:UIButton。

3、为按钮添加点击事件

通过辅助编辑器为这2个按钮添加按钮单击事件:分别为 generalBtnClick 和   groupBtnClick

4、完整代码

import UIKit

enum UIControlType{
? ? case Basic
? ? case Advanced
}

class ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource{
? ??
? ? var tableView:UITableView?
? ??
? ? var ctrlnames:[String]? = ["按钮", "文本框", "标签"];
? ??
? ? var allnames:Dictionary<Int, [String]>?
? ??
? ? var adHeaders:[String]?
? ??
? ? var ctype:UIControlType!
? ??
? ? override func loadView() {
? ? ? ? super.loadView()
? ? }
? ??
? ? override func viewDidLoad() {
? ? ? ? super.viewDidLoad()
? ? ? ??
? ? ? ? // ? ? ? ?//初始化数据,这一次数据,我们放在属性列表文件里
? ? ? ? // ? ? ? ?self.ctrlnames = ?NSArray(contentsOfFile: NSBundle.mainBundle().pathForResource("Controls", ofType:"plist")!) as? Array
? ? ? ? //
? ? ? ? // ? ? ? ?print(self.ctrlnames, terminator: "")
? ? ? ??
? ? ? ? //初始化数据,这一次数据,我们放在属性列表文件里
? ? ? ? self.allnames = ?[ 0:[String](self.ctrlnames!),1:[String]([
? ? ? ? ? ? "日期选择器",
? ? ? ? ? ? "网页选择器",
? ? ? ? ? ? "工具条",
? ? ? ? ? ? "表格视图"])
? ? ? ? ];
? ? ? ??
? ? ? ? // ? ? ? ?print(self.allnames, terminator: "")
? ? ? ??
? ? ? ? self.adHeaders = [
? ? ? ? ? ? "常见UIKit控件",
? ? ? ? ? ? "高级UIKit控件"
? ? ? ? ]
? ? }
? ??
? ? @IBAction func generalBtnClicked(sender: UIButton) {
? ? ? ? self.ctype = UIControlType.Basic
? ? ? ??
? ? ? ??
? ? ? ? //创建表视图
? ? ? ? self.tableView = UITableView(frame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height - 100), style:UITableViewStyle.Plain)
? ? ? ? self.tableView!.delegate = self
? ? ? ? self.tableView!.dataSource = self
? ? ? ? //创建一个重用的单元格
? ? ? ? self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell")
? ? ? ? self.view.addSubview(self.tableView!)
? ? ? ??
? ? ? ??
? ? ? ? //创建表头标签
? ? ? ? let headerLabel = UILabel(frame: CGRectMake(0, 0, self.view.bounds.size.width, 30))
? ? ? ? headerLabel.backgroundColor = UIColor.blackColor()
? ? ? ? headerLabel.textColor = UIColor.whiteColor()
? ? ? ? headerLabel.numberOfLines = 0
? ? ? ? headerLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
? ? ? ? headerLabel.text = "常见 UIKit 控件"
? ? ? ? headerLabel.font = UIFont.italicSystemFontOfSize(20)
? ? ? ? self.tableView!.tableHeaderView = headerLabel
? ? }
? ??
? ? @IBAction func groupBtnClicked(sender: UIButton) {
? ? ? ? self.ctype = UIControlType.Advanced
? ? ? ??
? ? ? ? //创建表视图
? ? ? ??
? ? ? ??
? ? ? ? self.tableView = UITableView(frame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height - 100), style:UITableViewStyle.Grouped)
? ? ? ? self.tableView!.delegate = self
? ? ? ? self.tableView!.dataSource = self
? ? ? ? //创建一个重用的单元格
? ? ? ? self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell")
? ? ? ? self.view.addSubview(self.tableView!)
? ? ? ??
? ? ? ? //创建表头标签
? ? ? ? let headerLabel = UILabel(frame: CGRectMake(0, 0, self.view.bounds.size.width, 30))
? ? ? ? headerLabel.backgroundColor = UIColor.blackColor()
? ? ? ? headerLabel.textColor = UIColor.whiteColor()
? ? ? ? headerLabel.numberOfLines = 0
? ? ? ? headerLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
? ? ? ? headerLabel.text = "高级 UIKit 控件"
? ? ? ? headerLabel.font = UIFont.italicSystemFontOfSize(20)
? ? ? ? self.tableView!.tableHeaderView = headerLabel
? ? }
? ??
? ??
? ? //在本例中,只有一个分区
? ? func numberOfSectionsInTableView(tableView: UITableView) -> Int {
? ? ? ? return self.ctype == UIControlType.Basic ? 1:2;
? ? }
? ??
? ? //返回表格行数(也就是返回控件数)
? ? func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
? ? ? ? let data = self.allnames?[section]
? ? ? ? return data!.count
? ? }
? ??
? ??
? ? // UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的头部
? ? func tableView(tableView:UITableView, titleForHeaderInSection
? ? ? ? section:Int)->String?
? ? {
? ? ? ? var headers = ?self.adHeaders!;
? ? ? ? return headers[section];
? ? }
? ? // UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的尾部
? ? func tableView(tableView:UITableView, titleForFooterInSection
? ? ? ? section:Int)->String?
? ? {
? ? ? ? let data = self.allnames?[section]
? ? ? ? return "有\(data!.count)个控件"
? ? }
? ??
? ??
? ? //创建各单元显示内容(创建参数indexPath指定的单元)
? ? func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
? ? {
? ? ? ? let identify:String = "SwiftCell";
? ? ? ??
? ? ? ? /// 同一形式的单元格重复使用。
? ? ? ? let secno = indexPath.section;
? ? ? ? var data = self.allnames?[secno];
? ? ? ? if (0 == secno)
? ? ? ? {
? ? ? ? ? ? let cell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath);
? ? ? ? ? ? cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator;
? ? ? ? ? ??
? ? ? ? ? ? cell.imageView?.image = UIImage(named: "1");
? ? ? ? ? ? cell.textLabel?.text = data![indexPath.row];
? ? ? ??
? ? ? ? ? ? return cell;
? ? ? ? }
? ? ? ??
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? let adcell = UITableViewCell(style: .Subtitle, reuseIdentifier: "SwiftCell");
? ? ? ? ? ? adcell.textLabel?.text = data![indexPath.row];
? ? ? ? ? ? adcell.detailTextLabel?.text = "这是\(data![indexPath.row])的说明";
? ? ? ? ? ??
? ? ? ? ? ? return adcell;
? ? ? ? }
? ? }
? ??
? ? // UITableViewDelegate 方法,处理列表项的选中事件
? ? func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
? ? {
? ? ? ? self.tableView!.deselectRowAtIndexPath(indexPath, animated: true)
? ? ? ??
? ? ? ? let itemString = self.ctrlnames![indexPath.row]
? ? ? ??
? ? ? ? let ?alert = UIAlertController(title: "提示", message: "你选择了:\(itemString)", preferredStyle: UIAlertControllerStyle.Alert);
? ? ? ? let sureAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.Default, handler: {(action)->Void in});
? ? ? ? alert.addAction(sureAction);
? ? ? ??
? ? ? ? presentViewController(alert,animated:true, completion:nil);
? ? ? ??
? ? }
? ??
? ? override func didReceiveMemoryWarning() {
? ? ? ? super.didReceiveMemoryWarning()
? ? ? ??
? ? ? ? // Dispose of any resources that can be recreated.
? ? }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。

相关文章

  • 详解Swift面向对象编程中的方法(method)

    详解Swift面向对象编程中的方法(method)

    既然面向对象那就一定会有method,方法和面向过程语言中的function函数并没什么区别,只不过方法在面向对象语言中可以被类来约束作用域,这里我们就来详解Swift面向对象编程中的方法(method)
    2016-07-07
  • swift表格控件使用方法详解(UITableview)

    swift表格控件使用方法详解(UITableview)

    这篇文章主要为大家详细介绍了swift表格控件的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • Swift自定义UITableViewCell背景色

    Swift自定义UITableViewCell背景色

    这篇文章主要为大家详细介绍了Swift自定义UITableViewCell背景色,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • 深入理解Swift语言中的闭包机制

    深入理解Swift语言中的闭包机制

    这篇文章主要介绍了Swift语言中的闭包机制,是Swift入门学习中的基础知识,需要的朋友可以参考下
    2015-11-11
  • swift语言AutoreleasePool原理及使用场景

    swift语言AutoreleasePool原理及使用场景

    这篇文章主要为大家介绍了swift语言AutoreleasePool原理及使用场景详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • 利用Swift实现一个响应式编程库

    利用Swift实现一个响应式编程库

    最近在学习swift,最近有空所以总结一下最近学习的内容,下面这篇文章主要给大家介绍了关于利用Swift实现一个响应式编程库的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-12-12
  • 用Swift构建一个简单的iOS邮件应用的方法

    用Swift构建一个简单的iOS邮件应用的方法

    这篇文章主要介绍了用Swift构建一个简单的iOS邮件应用的方法,包括查看和标记已读等基本的邮件应用功能,需要的朋友可以参考下
    2015-07-07
  • Swift中defer关键字推迟执行示例详解

    Swift中defer关键字推迟执行示例详解

    这篇文章主要给大家介绍了关于Swift中defer关键字推迟执行的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-03-03
  • Swift 使用 Observe 监测页面滚动的实现方法

    Swift 使用 Observe 监测页面滚动的实现方法

    这篇文章主要介绍了Swift 使用 Observe 监测页面滚动的实现方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-05-05
  • Swift里的值类型与引用类型区别和使用

    Swift里的值类型与引用类型区别和使用

    这篇文章主要介绍了Swift里的值类型与引用类型区别和使用,本文讲解了值类型与引用类型的区别、如何选择类型、什么时候该用值类型、什么时候该用引用类型等内容,需要的朋友可以参考下
    2015-05-05

最新评论

?


http://www.vxiaotou.com