陈晓萍 發表於 2022-1-27 09:19:36

Swift使用表格组件实现单列表

<p>本文实例为大家分享了Swift使用表格组件实现单列表的具体代码,供大家参考,具体内容如下</p>
<h2>1、样例说明:</h2>
<p>(1)列表内容从Controls.plist文件中读取,类型为Array 。<br />(2)点击列表项会弹出消息框显示该项信息。<br />(3)按住列表项向左滑动,会出现删除按钮。点击删除即可删除该项。</p>
<h2>2、效果图</h2>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202201/202212791119746.jpg?202202791139" /></p>
<h2>3、单元格复用机制</h2>
<p>由于普通的表格视图中对的单元格形式一般都是相同的,所以本例采用了单元格复用机制,可以大大提高程序性能。<br />实现方式是初始化创建 &nbsp;UITableView 实例时使用 &nbsp;registerClass(UITableViewCell.self, forCellReuseIdentifier: &quot;SwiftCell&quot;) 创建一个可供重用的 &nbsp;UITableViewCell。并将其注册到UITableView,ID为 SwiftCell。<br />下次碰到形式(或结构)相同的单元就可以直接使用UITableView的dequeueReusableCellWithIdentifier 方法从UITableView中取出。</p>
<h2>4、示例代码</h2>
<p>--- ViewController.swift ---</p>
<div class="jb51code"><pre class="brush:cpp;">import  UIKit
 
class  ViewController :  UIViewController ,  UITableViewDelegate ,  UITableViewDataSource  {
     
     var  ctrlnames:[ String ]?
     var  tableView: UITableView ?
     
     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)
         
         //创建表视图
         self .tableView =  UITableView (frame:  self .view.frame, 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
     }
     
     //在本例中,只有一个分区
     func  numberOfSectionsInTableView(tableView:  UITableView ) -&gt;  Int  {
         return  1;
     }
     
     //返回表格行数(也就是返回控件数)
     func  tableView(tableView:  UITableView , numberOfRowsInSection section:  Int ) -&gt;  Int  {
         return  self .ctrlnames!.count
     }
     
     //创建各单元显示内容(创建参数indexPath指定的单元)
     func  tableView(tableView:  UITableView , cellForRowAtIndexPath indexPath:  NSIndexPath )
         -&gt;  UITableViewCell
     {
         //为了提供表格显示性能,已创建完成的单元需重复使用
         let  identify: String  =  "SwiftCell"
         //同一形式的单元格重复使用,在声明时已注册
         let  cell = tableView.dequeueReusableCellWithIdentifier(identify,
             forIndexPath: indexPath)  as  UITableViewCell
         cell.accessoryType =  UITableViewCellAccessoryType . DisclosureIndicator
         cell.textLabel?.text =  self .ctrlnames!
         return  cell
     }
     
     // UITableViewDelegate 方法,处理列表项的选中事件
     func  tableView(tableView:  UITableView , didSelectRowAtIndexPath indexPath:  NSIndexPath )
     {
         self .tableView!.deselectRowAtIndexPath(indexPath, animated:  true )
         
         let  itemString =  self .ctrlnames!
         
         let  alertController =  UIAlertController (title:  "提示!" ,
             message:  "你选中了【\(itemString)】" , preferredStyle: . Alert )
         let  okAction =  UIAlertAction (title:  "确定" , style: . Default ,handler:  nil )
         alertController.addAction(okAction)
         self .presentViewController(alertController, animated:  true , completion:  nil )
     }
     
     //滑动删除必须实现的方法
     func  tableView(tableView:  UITableView ,
         commitEditingStyle editingStyle:  UITableViewCellEditingStyle ,
         forRowAtIndexPath indexPath:  NSIndexPath ) {
             print ( "删除\(indexPath.row)" )
             let  index = indexPath.row
             self .ctrlnames?.removeAtIndex(index)
             self .tableView?.deleteRowsAtIndexPaths(,
                 withRowAnimation:  UITableViewRowAnimation . Top )
     }
     
     //滑动删除
     func  tableView(tableView:  UITableView ,
         editingStyleForRowAtIndexPath indexPath:  NSIndexPath )
         -&gt;  UITableViewCellEditingStyle  {
             return  UITableViewCellEditingStyle . Delete
     }
     
     //修改删除按钮的文字
     func  tableView(tableView:  UITableView ,
         titleForDeleteConfirmationButtonForRowAtIndexPath indexPath:  NSIndexPath )
         -&gt;  String ? {
             return  "删"
     }
     
     override  func  didReceiveMemoryWarning() {
         super .didReceiveMemoryWarning()
     }
}</pre></div>
<p>以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持琼殿技术社区。</p>
                           
                            <div class="art_xg">
                              <b>您可能感兴趣的文章:</b><ul><li>swift自定义表格控件(UITableView)</li><li>swift表格控件使用方法详解(UITableview)</li><li>Swift实现表格视图单元格单选(2)</li><li>Swift实现表格视图单元格单选(1)</li><li>Swift实现表格视图单元格多选</li></ul>
                            </div>

                        </div>
                        <!--endmain-->
頁: [1]
查看完整版本: Swift使用表格组件实现单列表