UITableView的简单使用

显示简单的UITableView


import UIKit

class ViewController: UIViewController, UITableViewDataSource {
  
  var tableView: UITableView?
  
  override func viewDidLoad() {
    super.viewDidLoad()
    tableView = UITableView(frame: view.bounds, style: .Plain)
    
    if let theTableView = tableView{
      
      theTableView.registerClass(UITableViewCell.classForCoder(),
        forCellReuseIdentifier: "identifier")
      
      theTableView.dataSource = self
      theTableView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
      
      view.addSubview(theTableView)
    }
  }
  
  func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 3
  }
  
  func tableView(tableView: UITableView,
    numberOfRowsInSection section: Int) -> Int {
      
      switch section{
      case 0:
        return 3
      case 1:
        return 5
      case 2:
        return 8
      default:
        return 0
      }
      
  }
  
  func tableView(tableView: UITableView,
    cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
      
      let cell = tableView.dequeueReusableCellWithIdentifier("identifier",
        forIndexPath: indexPath) as UITableViewCell
      
      cell.textLabel!.text = "Section \(indexPath.section), " +
      "Cell \(indexPath.row)"
      
      return cell
      
  }
  
  override func prefersStatusBarHidden() -> Bool {
    return true
  }
  
}

效果如下:





滑动删除


import UIKit

class ViewController: UIViewController,
UITableViewDataSource, UITableViewDelegate {
  
  var tableView: UITableView?
  var allRows = [String]()
  
  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    
    for index in 0..<10{
      allRows.append("Cell at index of \(index)")
    }
    
  }
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    navigationItem.setLeftBarButtonItem(editButtonItem(), animated: false)
    
    tableView = UITableView(frame: view.bounds, style: .Plain)
    
    if let theTableView = tableView{
      
      theTableView.registerClass(UITableViewCell.classForCoder(),
        forCellReuseIdentifier: "identifier")
      
      theTableView.dataSource = self
      theTableView.delegate = self
      theTableView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
      
      view.addSubview(theTableView)
    }
  }
  
  func tableView(tableView: UITableView,
    numberOfRowsInSection section: Int) -> Int {
      
      return allRows.count
      
  }
  
  func tableView(tableView: UITableView,
    cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
      
      let cell = tableView.dequeueReusableCellWithIdentifier("identifier",
        forIndexPath: indexPath) as UITableViewCell
      
      cell.textLabel!.text = allRows[indexPath.row]
      
      return cell
      
  }
  
  func tableView(tableView: UITableView,
    editingStyleForRowAtIndexPath indexPath: NSIndexPath)
    -> UITableViewCellEditingStyle{
      return .Delete
  }
  
  override func setEditing(editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)
    tableView!.setEditing(editing, animated: animated)
  }
  
  func tableView(tableView: UITableView,
    commitEditingStyle editingStyle: UITableViewCellEditingStyle,
    forRowAtIndexPath indexPath: NSIndexPath){
    
      if editingStyle == .Delete{
        /* First remove this object from the source */
        allRows.removeAtIndex(indexPath.row)
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Left)
      }
    
  }
  
}



效果如下:









设置Header和Footer


  func tableView(tableView: UITableView,
    titleForHeaderInSection section: Int) -> String?{
    return "Section \(section) Header"
  }
  
  func tableView(tableView: UITableView,
    titleForFooterInSection section: Int) -> String?{
    return "Section \(section) Footer"
  }

效果如下:




下拉刷新


import UIKit

class ViewController: UIViewController, UITableViewDataSource {
  var tableView: UITableView?
  var allTimes = [NSDate]()
  var refreshControl: UIRefreshControl?
  
  override func viewDidLoad() {
    
    super.viewDidLoad()
    
    allTimes.append(NSDate())
    
    tableView = UITableView(frame: view.bounds, style: .Plain)
    
    if let theTableView = tableView{
      
      theTableView.registerClass(UITableViewCell.classForCoder(),
        forCellReuseIdentifier: "identifier")
      
      theTableView.dataSource = self
      theTableView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
      
      /* Create the refresh control */
      refreshControl = UIRefreshControl()
      refreshControl!.addTarget(self,
        action: "handleRefresh:",
        forControlEvents: .ValueChanged)
      
      theTableView.addSubview(refreshControl!)
      
      view.addSubview(theTableView)
    }
  }

  func tableView(tableView: UITableView,
    numberOfRowsInSection section: Int) -> Int {
      
      return allTimes.count
      
  }
  
  func tableView(tableView: UITableView,
    cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
      
      let cell = tableView.dequeueReusableCellWithIdentifier("identifier",
        forIndexPath: indexPath) as UITableViewCell
      
      cell.textLabel!.text = "\(allTimes[indexPath.row])"
      
      return cell
      
  }
  
  func handleRefresh(paramSender: AnyObject){
  
  /* Put a bit of delay between when the refresh control is released
  and when we actually do the refreshing to make the UI look a bit
  smoother than just doing the update without the animation */
    
    let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64(NSEC_PER_SEC))
    dispatch_after(popTime,
      dispatch_get_main_queue(), {
        
        /* Add the current date to the list of dates that we have
        so that when the table view is refreshed, a new item will appear
        on the screen so that the user will see the difference between
        the before and the after of the refresh */
        self.allTimes.append(NSDate())
        self.refreshControl!.endRefreshing()
        let indexPathOfNewRow = NSIndexPath(forRow: self.allTimes.count - 1,
          inSection: 0)
        
        self.tableView!.insertRowsAtIndexPaths([indexPathOfNewRow],
          withRowAnimation: .Automatic)
        
      })

  }
  
  override func prefersStatusBarHidden() -> Bool {
    return true
  }
  
}

效果如下:









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值