swift表格控件使用方法詳解(UITableview)
本文實(shí)例為大家分享了swift表格控件的簡(jiǎn)單使用,供大家參考,具體內(nèi)容如下
1、效果圖
2、該控件(UITableView) 代碼注意的地方:
A、ViewController 不單單繼承于 UIViewController,還有 UITableViewDelegate,UITableViewDataSource。
B、要自己重新實(shí)現(xiàn)UITableView的3個(gè)方法。分別是:numberOfSectionInTableView(table:UITableView), tableView(table:UITableView, numberOfRowsInSetion section:Int), tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
3、我使用的Xcode版本:7.2;
4、完整 源碼
import UIKit class ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource{ ? ?? ? ? let ctrlName = ["按鈕", "標(biāo)簽", "文本框", "提示框", "開(kāi)關(guān)按鈕"]; ? ? var tableView:UITableView?; ? ?? ? ? override func loadView() { ? ? ? ? super.loadView(); ? ? } ? ?? ? ? override func viewDidLoad() { ? ? ? ? super.viewDidLoad() ? ? ? ?? ? ? ? ? /// 創(chuàng)建視圖 ? ? ? ? tableView = UITableView(frame: view.frame, style: .Plain); ? ? ? ? tableView!.dataSource = self; ? ? ? ? tableView!.delegate = self; ? ? ? ?? ? ? ? ? tableView?.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell"); ? ? ? ? view.addSubview(tableView!); ? ? ? ?? ? ? ? ?? ? ? ? ? /// 創(chuàng)建標(biāo)簽 ? ? ? ? let headerLabel = UILabel(frame: CGRectMake(0, 0, view.bounds.size ? ? ? ? ? ? .width, 30)); ? ? ? ? /// 設(shè)置標(biāo)簽屬性 ? ? ? ? headerLabel.backgroundColor = UIColor.blackColor(); ? ? ? ? headerLabel.textColor = UIColor.whiteColor(); ? ? ? ? headerLabel.numberOfLines = 0; ? ? ? ? headerLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping; ? ? ? ? headerLabel.text = "常見(jiàn)UIKIt控件"; ? ? ? ? headerLabel.font = UIFont.italicSystemFontOfSize(20); ? ? ? ?? ? ? ? ? /// 在tableView的頂部添加該label ? ? ? ? tableView!.tableHeaderView = headerLabel; ? ? } ? ? /// 設(shè)置分區(qū) ? ? func numberOfSectionsInTableView(tableView: UITableView) -> Int { ? ? ? ? return 1; ? ? } ? ?? ? ? /// 返回表格行數(shù) ? ? func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { ? ? ? ? return ctrlName.count; ? ? } ? ?? ? ? /// 創(chuàng)建單元格顯示內(nèi)容,(創(chuàng)建indexPath指定的單元) ? ? func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { ? ? ? ?? ? ? ? ? /// 為了提高表格顯示性能,以創(chuàng)建完成的單元重復(fù)使用 ? ? ? ? let identify = "SwiftCell"; ? ? ? ? /// 同一形式的單元格重復(fù)使用,在聲明時(shí)已注冊(cè) ? ? ? ? let cell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath); ? ? ? ? cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator; ? ? ? ? cell.textLabel?.text = ctrlName[indexPath.row]; ? ? ? ?? ? ? ? ? return cell; ? ? } ? ?? ? ? /// UITableViewDelegate方法,處理列表項(xiàng)選中事件 ? ? func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { ? ? ? ?? ? ? ? ? tableView.deselectRowAtIndexPath(indexPath, animated: true); ? ? ? ? let itemString = ctrlName[indexPath.row]; ? ? ? ?? ? ? ? ? /// 創(chuàng)建提示框 ? ? ? ? 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. ? ? } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
理解二叉堆數(shù)據(jù)結(jié)構(gòu)及Swift的堆排序算法實(shí)現(xiàn)示例
二插堆即是完全二叉樹(shù),對(duì)于排序可以按構(gòu)建最大堆或最小堆的方式來(lái)實(shí)現(xiàn),這里我們就來(lái)共同理解二叉堆數(shù)據(jù)結(jié)構(gòu)及Swift的堆排序算法實(shí)現(xiàn)示例2016-07-07IOS 實(shí)現(xiàn)簡(jiǎn)單的彈幕功能
本文主要介紹IOS 實(shí)現(xiàn)彈幕功能,這里給大家一個(gè)實(shí)例來(lái)展現(xiàn)彈幕功能,有需要的小伙伴可以參考下2016-07-07Swift調(diào)用Objective-C編寫(xiě)的API實(shí)例
這篇文章主要介紹了Swift調(diào)用Objective-C編寫(xiě)的API實(shí)例,介紹的比較全面和詳細(xì),對(duì)Objective-C代碼的重復(fù)利用有極大好處,的朋友可以參考下2014-07-07