From eca254c82e0426bf065f343f42a390b4e187e8ba Mon Sep 17 00:00:00 2001 From: Xin Hong Date: Wed, 24 Feb 2016 19:37:26 +0800 Subject: [PATCH] improve tableView example --- .../EmptyDataDemoTableViewController.swift | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/Example/EmptyDataDemoTableViewController.swift b/Example/EmptyDataDemoTableViewController.swift index 954b86d..eeca238 100644 --- a/Example/EmptyDataDemoTableViewController.swift +++ b/Example/EmptyDataDemoTableViewController.swift @@ -18,6 +18,7 @@ class EmptyDataDemoTableViewController: UITableViewController, TBEmptyDataSetDat // MARK: - Properties var indexPath = NSIndexPath() private var isLoading = false + private var dataCount = 0 // MARK: - View life cycle override func viewDidLoad() { @@ -39,6 +40,7 @@ class EmptyDataDemoTableViewController: UITableViewController, TBEmptyDataSetDat 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.dataCount = 7 self.tableView.reloadData() self.refreshControl?.endRefreshing() } @@ -53,25 +55,33 @@ class EmptyDataDemoTableViewController: UITableViewController, TBEmptyDataSetDat } } - // MARK: - Table view data source + // MARK: - Table view data source and delegate override func numberOfSectionsInTableView(tableView: UITableView) -> Int { - return 0 + return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return 0 + return dataCount } 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 = UITableViewCell(style: .Value1, reuseIdentifier: CellIdentifier.reuseIdentifier) } - cell!.selectionStyle = .None - + cell!.textLabel?.text = "Cell" + cell!.detailTextLabel?.text = "Click to delete" return cell! } + override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { + tableView.deselectRowAtIndexPath(indexPath, animated: true) + tableView.beginUpdates() + dataCount-- + tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) + tableView.endUpdates() + } + // MARK: - TBEmptyDataSet data source func titleForEmptyDataSet(scrollView: UIScrollView!) -> NSAttributedString? { let title = EmptyData.titles[indexPath.row]