swiftt之表格控件(UITableView)详解,出错,求指南
2016-03-03 11:13
381 查看
1、问题描述
最后的界面上什么都没有,空白的。如下图:
2、敢问:问题出在什么地方?
3、代码如下:
import UIKit
enum UIControlType
{
case Basic;
case Advanced;
}
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
var tableView:UITableView?;
var ctrNames:[String] = ["按钮UIButton", "标签UILabel", "文本框UITextField"];
var allNames:Dictionary<Int,[String]>?;
var adHeader:[String]?;
var controlType:UIControlType?;
override func loadView()
{
super.loadView();
}
override func viewDidLoad() {
super.viewDidLoad()
/// 初始化数据,把这次数据放在属性列表文件里
allNames = [0:ctrNames, 1:[String]([
"UIDatePicker 日期选择器",
"UIWebView 网页选择器",
"UIToolBar 工具条"])];
self.adHeader = ["常见 UIkit 控件", "高级UIKIt控件"];
}
@IBAction func plainClicked(sender:UIBarButtonItem)
{
controlType = UIControlType.Basic;
/// 创建表视图
self.tableView = UITableView(frame: CGRectMake(0, 100, view.frame.size
.width, 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, view.bounds.size
.width, 30));
headerLabel.backgroundColor = UIColor.blackColor();
headerLabel.textColor = UIColor.whiteColor();
headerLabel.numberOfLines = 0;
headerLabel.lineBreakMode = NSLineBreakMode.ByCharWrapping;
headerLabel.text = "常见UIKit控件";
headerLabel.font = UIFont.italicSystemFontOfSize(20);
/// 将label显示到TableView中
self.tableView!.tableHeaderView = headerLabel;
}
@IBAction func groupClickd(sender:UIBarButtonItem)
{
controlType = UIControlType.Advanced;
/// 创建视图
self.tableView = UITableView(frame: CGRectMake(0, 100, view.frame.size.width, 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, view.bounds.size
.width, 30));
headerLabel.backgroundColor = UIColor.blackColor();
headerLabel.textColor = UIColor.whiteColor();
headerLabel.numberOfLines = 0;
headerLabel.lineBreakMode = NSLineBreakMode.ByCharWrapping;
headerLabel.text = "高级UIKit控件";
headerLabel.font = UIFont.italicSystemFontOfSize(20);
self.tableView!.tableHeaderView = headerLabel;
}
/// 创建分区
func numberOfSectionsInTableView(table:UITableView)->Int
{
return controlType == UIControlType.Basic ? 1:2;
}
/// 返回行数
func tableView(tableView:UITableView, numberOfRowsInSection section:Int)->Int
{
let data = allNames![section];
return data!.count;
}
/// 协议中得方法,该方法的返回值决定指定分区的头部
func tableView(table:UITableView, titleForHeaderInSection section:Int)->String?
{
return adHeader?[section];
}
/// 协议中的方法,该方法的返回值决定指定分区的尾部
func tableView(table:UITableView, titleForFooterInSection section:Int)->String?
{
return "有\(allNames![section]!.count)个控件";
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let identify = "SwiftCell";
let cell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath);
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator;
let secno = indexPath.section;
let data = allNames![secno];
cell.textLabel?.text = data![indexPath.row];
return cell;
}
/// UItableViewDelegate 方法,处理列表项的选中事件
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
tableView.deselectRowAtIndexPath(indexPath, animated: true);
let itemString = ctrNames[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.
}
}4、UITableView控件详解
最后的界面上什么都没有,空白的。如下图:
2、敢问:问题出在什么地方?
3、代码如下:
import UIKit
enum UIControlType
{
case Basic;
case Advanced;
}
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
var tableView:UITableView?;
var ctrNames:[String] = ["按钮UIButton", "标签UILabel", "文本框UITextField"];
var allNames:Dictionary<Int,[String]>?;
var adHeader:[String]?;
var controlType:UIControlType?;
override func loadView()
{
super.loadView();
}
override func viewDidLoad() {
super.viewDidLoad()
/// 初始化数据,把这次数据放在属性列表文件里
allNames = [0:ctrNames, 1:[String]([
"UIDatePicker 日期选择器",
"UIWebView 网页选择器",
"UIToolBar 工具条"])];
self.adHeader = ["常见 UIkit 控件", "高级UIKIt控件"];
}
@IBAction func plainClicked(sender:UIBarButtonItem)
{
controlType = UIControlType.Basic;
/// 创建表视图
self.tableView = UITableView(frame: CGRectMake(0, 100, view.frame.size
.width, 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, view.bounds.size
.width, 30));
headerLabel.backgroundColor = UIColor.blackColor();
headerLabel.textColor = UIColor.whiteColor();
headerLabel.numberOfLines = 0;
headerLabel.lineBreakMode = NSLineBreakMode.ByCharWrapping;
headerLabel.text = "常见UIKit控件";
headerLabel.font = UIFont.italicSystemFontOfSize(20);
/// 将label显示到TableView中
self.tableView!.tableHeaderView = headerLabel;
}
@IBAction func groupClickd(sender:UIBarButtonItem)
{
controlType = UIControlType.Advanced;
/// 创建视图
self.tableView = UITableView(frame: CGRectMake(0, 100, view.frame.size.width, 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, view.bounds.size
.width, 30));
headerLabel.backgroundColor = UIColor.blackColor();
headerLabel.textColor = UIColor.whiteColor();
headerLabel.numberOfLines = 0;
headerLabel.lineBreakMode = NSLineBreakMode.ByCharWrapping;
headerLabel.text = "高级UIKit控件";
headerLabel.font = UIFont.italicSystemFontOfSize(20);
self.tableView!.tableHeaderView = headerLabel;
}
/// 创建分区
func numberOfSectionsInTableView(table:UITableView)->Int
{
return controlType == UIControlType.Basic ? 1:2;
}
/// 返回行数
func tableView(tableView:UITableView, numberOfRowsInSection section:Int)->Int
{
let data = allNames![section];
return data!.count;
}
/// 协议中得方法,该方法的返回值决定指定分区的头部
func tableView(table:UITableView, titleForHeaderInSection section:Int)->String?
{
return adHeader?[section];
}
/// 协议中的方法,该方法的返回值决定指定分区的尾部
func tableView(table:UITableView, titleForFooterInSection section:Int)->String?
{
return "有\(allNames![section]!.count)个控件";
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let identify = "SwiftCell";
let cell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath);
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator;
let secno = indexPath.section;
let data = allNames![secno];
cell.textLabel?.text = data![indexPath.row];
return cell;
}
/// UItableViewDelegate 方法,处理列表项的选中事件
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
tableView.deselectRowAtIndexPath(indexPath, animated: true);
let itemString = ctrNames[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.
}
}4、UITableView控件详解
相关文章推荐
- Apple Swift学习教程
- 介绍 Fedora 上的 Swift
- Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
- Swift编程中的泛型解析
- Swift中定义二维数组的方法及遍历方法示例
- 简单分析Swift语言的一些基本特征
- Swift与C语言指针结合使用实例
- Swift心得笔记之控制流
- 用Swift构建一个简单的iOS邮件应用的方法
- 苹果公司推出的新编程语言Swift简介和入门教程
- 探讨Swift数组和字典
- Swift中的指针操作和使用详细介绍
- Swift中使用正则表达式的一些方法
- Swift心得笔记之运算符
- Swift中使用可选类型完美解决占位问题
- Swift学习笔记之构造器重载
- Swift中的Access Control权限控制介绍
- Swift类型创建之自定义一个类型详解
- Swift教程之控制流详解