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)文章
Swift中的可選項(xiàng)Optional解包方式實(shí)現(xiàn)原理
這篇文章主要為大家介紹了Swift中的可選項(xiàng)Optional解包方式實(shí)現(xiàn)原理示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Swift仿微信語(yǔ)音通話最小化時(shí)后的效果實(shí)例代碼
這篇文章主要介紹了Swift仿微信語(yǔ)音通話最小化時(shí)后的效果的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Swift利用指紋識(shí)別或面部識(shí)別為應(yīng)用添加私密保護(hù)功能
這篇文章主要給大家介紹了關(guān)于Swift利用指紋識(shí)別或面部識(shí)別為應(yīng)用添加私密保護(hù)功能的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用swift具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友下面隨著小編來(lái)一起看看吧2018-05-05
Swift 3.0基礎(chǔ)學(xué)習(xí)之枚舉類(lèi)型
枚舉在編程中很多時(shí)候要用到,在 Swift 中,枚舉具有更多的特性。下面這篇文章主要介紹了Swift 3.0基礎(chǔ)學(xué)習(xí)之枚舉類(lèi)型的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-03-03

