日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

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

作者:PandaMohist ? 更新時間: 2022-04-08 編程語言

本文實例為大家分享了swift表格控件的簡單使用,供大家參考,具體內容如下

1、效果圖

2、該控件(UITableView) 代碼注意的地方:

A、ViewController 不單單繼承于 UIViewController,還有 UITableViewDelegate,UITableViewDataSource。

B、要自己重新實現UITableView的3個方法。分別是: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 = ["按鈕", "標簽", "文本框", "提示框", "開關按鈕"];
? ? 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, 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.
? ? }


}

原文鏈接:https://blog.csdn.net/HK_5788/article/details/50785693

欄目分類
最近更新