前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Swift 2.0 UItableView 的简单使用

Swift 2.0 UItableView 的简单使用

作者头像
Mr.RisingSun
发布2018-01-09 17:07:59
1.7K0
发布2018-01-09 17:07:59
举报
文章被收录于专栏:移动端开发移动端开发

在IOS开发中,UItableView 的使用真的是最常见最普通的了,现在在自学swift 今天也是这用Swift 写了写 UItableview的使用,还有一些经常出错的地方。下面我先把整个控制器的代理列出来,大家可以顺便看看 swift 是怎样遵守协议的。

import UIKit
// 遵守两个协议
class HomeViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor=UIColor.whiteColor()
        
        // 创建tableview
        creatTableview()
        
        // Do any additional setup after loading the view.
    }
    
  //  创建tableView的方法
    func creatTableview()
    {
        
        let tableview:UITableView = UITableView(frame: self.view.frame, style: UITableViewStyle.Plain)
        tableview.delegate = self
        tableview.dataSource = self
    
        // 这里要不注册,用下面的   dequeueReusableCellWithIdentifier 这个方法的时候会崩溃,这里和大家说一下,dequeueReusableCellWithIdentifier("SwiftCell", forIndexPath: indexPath)  要使用这个方法,在这里你就必须得先注册一个复用的cell,然后使用的时候 dequeueReusableCellWithIdentifier 会去调用能复用的cell 

        tableview.registerClass(UITableViewCell.self,
           forCellReuseIdentifier: "SwiftCell")
        self.view.addSubview(tableview)
        
    }
    
    //  组数
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
         return 3
    }
    
    //  每组的个数
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

     switch  section
     {
          case 0:
        
              return 5
        
          case  1:
           
              return 3
           
          case  2:
        
               return 4
        
          default:
        
                return 0
     }
        
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("SwiftCell", forIndexPath: indexPath)
        cell.textLabel?.text="你真的很帅"
        return cell
        
        
    }
    
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        return 20
    }
    

    // tableView是否能编辑,true 返回,就是能编辑
    func tableView(tableView: UITableView, shouldIndentWhileEditingRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        
        return true
    }
    
    // 你要删除cell的时候,这个方法会自己调用的,这里一般就是在数据源里面删除cell 对应的数据。
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        
        print("删了这条数据了")
        
    }
    
    //  这里是编辑的类型
    func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
        
        return UITableViewCellEditingStyle.Delete
        
    }
    
   //  这里是给 删除按钮上的 按钮文字进行重命名,这里是改成“别删呀”的名字。
    func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
        
       return "别删呀"
        
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

特别说明几点内容:

1.自定义cell时,

若使用nib,使用 registerNib: 注册,dequeue时会调用 cell 的 -(void)awakeFromNib 不使用nib,使用 registerClass: 注册, dequeue时会调用 cell 的 - (id)initWithStyle:withReuseableCellIdentifier:

2.需不需要注册? 使用dequeueReuseableCellWithIdentifier:可不注册,但是必须对获取回来的cell进行判断是否为空,若空则手动创建新的cell; 使用dequeueReuseableCellWithIdentifier:forIndexPath:必须注册,但返回的cell可省略空值判断的步骤。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-03-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档