Skip to content

MangoMade/StaticCellKit

Repository files navigation

About StaticCellKit

Dequeue a static cell of indexPath,

and the cell will never be reused by other indexPath.

Requirements

  • iOS 8.0
  • XCode 8.0
  • Swift 3.0

Cocoapods

// Swift 3.2
pod 'StaticCellKit', '~> 0.0.3’
// Swift 4.0
pod 'StaticCellKit', '~> 0.0.4’

Why do I use StaticCellKit?

Sometimes we need to make a page like Settings. In this page, every row has different subviews.

It's no need to create many UITableViewCell subclasses and to reuse cells.

We just need a table view with static cells and add subviews directly in view controller's implementment.

As we known, we can create a static table view in StoryBoard.

By code, we can use this kit.


当我们要开发一个类似“设置”的页面时,每一个cell上的子视图都不相同。

从逻辑上我们不需要重用cell。为每一行都创建一个tableView的子类又太麻烦。

这时候,如果有一个static table view,我们直接在view controller的实现中为cell添加子视图就方便很多。

StoryBoard中我们可以直接创建static table view

用代码我们只需要这个kit。

Usage:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: CustomTableViewCell = tableView.sck.dequeueStaticCell(indexPath)
    
    return cell
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    guard section == 0 else { return nil }
    let header = tableView.sck.dequeueStaticHeaderView(section)
  
    return header
}
    
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    guard section == 0 else { return nil }
    let footer = tableView.sck.dequeueStaticFooterView(section)
    
    return footer
}

It will dequeue a cell which is CustomTableView type.

You don't need to call UITableView.registerClass(_:).

tableView.sck.delegate = self  

// `self` may be your view controller which conforming StaticTableViewDelegate.

extension ViewController: StaticTableViewDelegate {

   func tableView(_ tableView: UITableView, initStaticCell cell: UITableViewCell, ofIndexPath indexPath: IndexPath) {
	
   }

   func tableView(_ tableView: UITableView, initStaticHeader header: UITableViewHeaderFooterView, ofSection section: Int) {
	
   }
	
   func tableView(_ tableView: UITableView, initStaticFooter footer: UITableViewHeaderFooterView, ofSection section: Int) {
    
   }
   
}

You can use StaticTableViewDelegate to configure cell or header / footer.