大佬教程收集整理的这篇文章主要介绍了swift之表格控件简单使用(UITableview),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
A、ViewController 不单单继承于 UIViewController,还有 UITableViewDelegate,UITableViewDatasource。
B、要自己重新实现UITableView的3个方法。分别是:numberOfSectionInTableView(table:UITableView), tableView(table:UITableView,numberOfRowsInSetion section:int),tableView(tableView: UITableView,cellForRowATindexPath indexPath: NSIndexPath)
import UIKit class ViewController: UIViewController,UITableViewDelegate,UITableViewDatasource{ let ctrlName = ["按钮","标签","文本框","提示框","开关按钮"]; var tableView:UITableView?; override func loadView() { super.loadView(); } override func viewDidLoad() { super.viewDidLoad() /// 创建视图 tableView = UITableView(frame: view.frame,style: .Plain); tableView!.datasource = self; tableView!.delegate = self; tableView?.registerClass(UITableViewCell.self,forCellReusEIDentifier: "SwiftCell"); view.addSubview(tableView!); /// 创建标签 let headerLabel = UILabel(frame: CGRectMake(0,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); /// 在tableView的顶部添加该label tableView!.tableHeaderView = headerLabel; } /// 设置分区 func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1; } /// 返回表格行数 func tableView(tableView: UITableView,numberOfRowsInSection section: int) -> Int { return ctrlName.count; } /// 创建单元格显示内容,(创建indexPath指定的单元) func tableView(tableView: UITableView,cellForRowATindexPath indexPath: NSIndexPath) -> UITableViewCell { /// 为了提高表格显示性能,以创建完成的单元重复使用 let identify = "SwiftCell"; /// 同一形式的单元格重复使用,在声明时已注册 let cell = tableView.dequeueReusableCellWithIdentifier(identify,forIndexPath: indexPath); cell.accessoryType = UITableViewCellAccessoryType.DisclosureInDicator; cell.textLabel?.text = ctrlName[indexPath.row]; return cell; } /// UITableViewDelegate方法,处理列表项选中事件 func tableView(tableView: UITableView,didSELEctRowATindexPath indexPath: NSIndexPath) { tableView.deSELEctRowATindexPath(indexPath,animated: truE); let itemString = ctrlName[indexPath.row]; /// 创建提示框 let alertView = UIAlertController(title: "提示",message: "你选中了\(itemString)",preferredStyle: .Alert); /// 向提示框中增加按钮 let alertAction = UIAlertAction(title: "确定",style: UIAlertActionStyle.Default,handler: {(action)->Void in}); alertView.addAction(alertAction); presentViewController(alertView,animated:true,completion:nil); } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
以上是大佬教程为你收集整理的swift之表格控件简单使用(UITableview)全部内容,希望文章能够帮你解决swift之表格控件简单使用(UITableview)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。