teambition/TBEmptyDataSet

当原来数据为空我获取好数据调用self.tableView.reloadSections(...)这个方法时,这个控件无法正常用

yiliazhang opened this issue · 1 comments

如果我一开始数据为空,这时候会显示默认设置的空时的状态,然后我获得数据,调用self.tableView.reloadSections(...) 或self.collectionView.reloadSections(...)时,这个第三放库就不能正常。得道的结果是,数据正常显示出来,同事加载数据或空状态却不消失。`//
// EmptyDataDemoTableViewController.swift
// TBEmptyDataSetExample
//
// Created by 洪鑫 on 15/11/24.
// Copyright © 2015年 Teambition. All rights reserved.
//

import UIKit
//import TBEmptyDataSet

class EmptyDataDemoTableViewController: UITableViewController, TBEmptyDataSetDataSource, TBEmptyDataSetDelegate {
// MARK: - Structs
private struct CellIdentifier {
static let reuseIdentifier = "Cell"
}

// MARK: - Properties
var indexPath = NSIndexPath()
private var isLoading = false
private var items = [UInt32]()
// MARK: - View life cycle
override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.title = "TableView"
    tableView.tableFooterView = UIView()
    refreshControl?.addTarget(self, action: "fetchData:", forControlEvents: .ValueChanged)

    tableView.emptyDataSetDataSource = self
    tableView.emptyDataSetDelegate = self

    if indexPath.row != 0 {
        loadData(self)
    }
}

// MARK: - Helper
func fetchData(sender: AnyObject) {
    let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(1.5 * Double(NSEC_PER_SEC)))
    dispatch_after(delayTime, dispatch_get_main_queue()) { () -> Void in
        //            self.tableView.reloadData()
        for _ in 0..<10 {
            self.items.append(arc4random()%1000)
        }
        self.tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .Fade)
        self.refreshControl?.endRefreshing()
    }
}

func loadData(sender: AnyObject) {
    isLoading = true
    let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(1.75 * Double(NSEC_PER_SEC)))
    dispatch_after(delayTime, dispatch_get_main_queue()) { () -> Void in
        self.isLoading = false

// for i in 0..<10 {
// self.items.append(String(stringInterpolationSegment:i))
// }
// self.tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .Fade)
self.tableView.reloadData()
}
}

// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return items.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier.reuseIdentifier)
    if cell == nil {
        cell = UITableViewCell(style: .Default, reuseIdentifier: CellIdentifier.reuseIdentifier)
    }
    cell!.selectionStyle = .None
    cell?.textLabel?.text = String(stringInterpolationSegment: items[indexPath.row])
    return cell!
}

// MARK: - TBEmptyDataSet data source
func titleForEmptyDataSet(scrollView: UIScrollView!) -> NSAttributedString? {
    let title = EmptyData.titles[indexPath.row]
    var attributes: [String : AnyObject]?
    if indexPath.row == 1 {
        attributes = [NSFontAttributeName: UIFont.systemFontOfSize(22.0), NSForegroundColorAttributeName: UIColor.grayColor()]
    } else if indexPath.row == 2 {
        attributes = [NSFontAttributeName: UIFont.systemFontOfSize(24.0), NSForegroundColorAttributeName: UIColor.grayColor()]
    }
    return NSAttributedString(string: title, attributes: attributes)
}

func descriptionForEmptyDataSet(scrollView: UIScrollView!) -> NSAttributedString? {
    let description = EmptyData.descriptions[indexPath.row]
    var attributes: [String : AnyObject]?
    if indexPath.row == 1 {
        attributes = [NSFontAttributeName: UIFont.systemFontOfSize(17.0), NSForegroundColorAttributeName: UIColor(red: 3 / 255, green: 169 / 255, blue: 244 / 255, alpha: 1)]
    } else if indexPath.row == 2 {
        attributes = [NSFontAttributeName: UIFont.systemFontOfSize(18.0), NSForegroundColorAttributeName: UIColor.purpleColor()]
    }
    return NSAttributedString(string: description, attributes: attributes)
}

func imageForEmptyDataSet(scrollView: UIScrollView!) -> UIImage? {
    return EmptyData.images[indexPath.row]
}

func verticalOffsetForEmptyDataSet(scrollView: UIScrollView!) -> CGFloat {
    if let navigationBar = navigationController?.navigationBar {
        return -navigationBar.frame.height * 0.75
    }
    return 0
}

func customViewForEmptyDataSet(scrollView: UIScrollView!) -> UIView? {
    if isLoading {
        let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
        activityIndicator.startAnimating()
        return activityIndicator
    }
    return nil
}

// MARK: - TBEmptyDataSet delegate
func emptyDataSetScrollEnabled(scrollView: UIScrollView!) -> Bool {
    return true
}

func emptyDataSetTapEnabled(scrollView: UIScrollView!) -> Bool {
    return true
}

func emptyDataSetShouldDisplay(scrollView: UIScrollView!) -> Bool {
    return true
}

func emptyDataSetWillAppear(scrollView: UIScrollView!) {
     print("EmptyDataSet Will Appear!")
}

func emptyDataSetDidAppear(scrollView: UIScrollView!) {
    print("EmptyDataSet Did Appear!")
}

func emptyDataSetWillDisappear(scrollView: UIScrollView!) {
    print("EmptyDataSet Will Disappear!")
}

func emptyDataSetDidDisappear(scrollView: UIScrollView!) {
    print("EmptyDataSet Did Disappear!")
}

func emptyDataSetDidTapView(scrollView: UIScrollView!) {
    let alert = UIAlertController(title: nil, message: "Did Tap EmptyDataView!", preferredStyle: .Alert)
    let cancelAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
    alert.addAction(cancelAction)
    presentViewController(alert, animated: true, completion: nil)
}

}
`

Thanks for the feedback!

According to the method swizzling of TBEmptyDataSet, UITableView will check the data source when calling reloadData or endUpdates, and UICollectionView will only check when calling reloadData.

As a result, it will not refresh the empty data set when you call reloadSections.

For UITableView, I have improved the example, and you can use it like this:

tableView.beginUpdates()
// delete, insert, reload, etc.
tableView.endUpdates()

For UICollectionView, reloadData is only supported temporarily.