服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Swift - swift自定义表格控件(UITableView)

swift自定义表格控件(UITableView)

2022-07-22 11:17PandaMohist Swift

这篇文章主要为大家详细介绍了swift自定义表格控件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了swift自定义表格控件的具体代码,供大家参考,具体内容如下

1、效果图

swift自定义表格控件(UITableView)

2、控件

storyboard上的控件就2个:UIButton。

3、为按钮添加点击事件

通过辅助编辑器为这2个按钮添加按钮单击事件:分别为 generalBtnClick 和   groupBtnClick

4、完整代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import UIKit
 
enum UIControlType{
    case Basic
    case Advanced
}
 
class ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource{
    
    var tableView:UITableView?
    
    var ctrlnames:[String]? = ["按钮", "文本框", "标签"];
    
    var allnames:Dictionary<Int, [String]>?
    
    var adHeaders:[String]?
    
    var ctype:UIControlType!
    
    override func loadView() {
        super.loadView()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //        //初始化数据,这一次数据,我们放在属性列表文件里
        //        self.ctrlnames =  NSArray(contentsOfFile: NSBundle.mainBundle().pathForResource("Controls", ofType:"plist")!) as? Array
        //
        //        print(self.ctrlnames, terminator: "")
        
        //初始化数据,这一次数据,我们放在属性列表文件里
        self.allnames =  [ 0:[String](self.ctrlnames!),1:[String]([
            "日期选择器",
            "网页选择器",
            "工具条",
            "表格视图"])
        ];
        
        //        print(self.allnames, terminator: "")
        
        self.adHeaders = [
            "常见UIKit控件",
            "高级UIKit控件"
        ]
    }
    
    @IBAction func generalBtnClicked(sender: UIButton) {
        self.ctype = UIControlType.Basic
        
        
        //创建表视图
        self.tableView = UITableView(frame:CGRectMake(0, 100, self.view.frame.size.width, self.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, self.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)
        self.tableView!.tableHeaderView = headerLabel
    }
    
    @IBAction func groupBtnClicked(sender: UIButton) {
        self.ctype = UIControlType.Advanced
        
        //创建表视图
        
        
        self.tableView = UITableView(frame:CGRectMake(0, 100, self.view.frame.size.width, self.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, self.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)
        self.tableView!.tableHeaderView = headerLabel
    }
    
    
    //在本例中,只有一个分区
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return self.ctype == UIControlType.Basic ? 1:2;
    }
    
    //返回表格行数(也就是返回控件数)
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let data = self.allnames?[section]
        return data!.count
    }
    
    
    // UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的头部
    func tableView(tableView:UITableView, titleForHeaderInSection
        section:Int)->String?
    {
        var headers =  self.adHeaders!;
        return headers[section];
    }
    // UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的尾部
    func tableView(tableView:UITableView, titleForFooterInSection
        section:Int)->String?
    {
        let data = self.allnames?[section]
        return "有\(data!.count)个控件"
    }
    
    
    //创建各单元显示内容(创建参数indexPath指定的单元)
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let identify:String = "SwiftCell";
        
        /// 同一形式的单元格重复使用。
        let secno = indexPath.section;
        var data = self.allnames?[secno];
        if (0 == secno)
        {
            let cell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath);
            cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator;
            
            cell.imageView?.image = UIImage(named: "1");
            cell.textLabel?.text = data![indexPath.row];
        
            return cell;
        }
        
        else
        {
            let adcell = UITableViewCell(style: .Subtitle, reuseIdentifier: "SwiftCell");
            adcell.textLabel?.text = data![indexPath.row];
            adcell.detailTextLabel?.text = "这是\(data![indexPath.row])的说明";
            
            return adcell;
        }
    }
    
    // UITableViewDelegate 方法,处理列表项的选中事件
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        self.tableView!.deselectRowAtIndexPath(indexPath, animated: true)
        
        let itemString = self.ctrlnames![indexPath.row]
        
        let  alert = UIAlertController(title: "提示", message: "你选择了:\(itemString)", preferredStyle: UIAlertControllerStyle.Alert);
        let sureAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.Default, handler: {(action)->Void in});
        alert.addAction(sureAction);
        
        presentViewController(alert,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/50808625

延伸 · 阅读

精彩推荐