Original Post: November 9, 2015
This example shows how to:
1. Refresh the table view by adding ten extra rows while pulling the table up to the bottom.
2. Change the table background color for every 10 rows.
Note: This example does not include the activity indicator.
Edit ViewController.swift as below:
Xcode 8.1 and Swift 3.0.1:
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var tableView : UITableView!
var tableRowNumber : Int = 10
//Colors used in the table
var colorArray = [UIColor.yellow, UIColor.orange, UIColor.cyan, UIColor.lightGray, UIColor.white]
override func viewDidLoad() {
super.viewDidLoad()
tableView = UITableView(frame: CGRect(x: 0, y: 30, width: view.bounds.width, height: 300))
tableView.dataSource = self
tableView.delegate = self
//Disable the default bouncing feature.
tableView.bounces = false
view.addSubview(tableView)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableRowNumber
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "myIdentifier")
cell.textLabel?.text = "Row \(indexPath.row)"
//Change the table background color for every 10 rows.
let color = colorArray[(indexPath.row/10)%colorArray.count]
cell.backgroundColor = color
return cell
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
//If we reach the end of the table.
if ((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height)
{
//Add ten more rows and reload the table content.
tableRowNumber += 10
tableView.reloadData()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Original Post: November 9, 2015
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var tableView : UITableView!
var tableRowNumber : Int = 10
//Colors used in the table
var colorArray = [UIColor.yellowColor(), UIColor.orangeColor(), UIColor.cyanColor(), UIColor.lightGrayColor(), UIColor.whiteColor()]
override func viewDidLoad() {
super.viewDidLoad()
tableView = UITableView(frame: CGRectMake(0, 30, view.bounds.width, 300))
tableView.dataSource = self
tableView.delegate = self
//Disable the default bouncing feature.
tableView.bounces = false
view.addSubview(tableView)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableRowNumber
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "myIdentifier")
cell.textLabel?.text = "Row \(indexPath.row)"
//Change the table background color for every 10 rows.
let color = colorArray[(indexPath.row/10)%colorArray.count]
cell.backgroundColor = color
return cell
}
func scrollViewDidScroll(scrollView: UIScrollView) {
//If we reach the end of the table.
if ((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height)
{
//Add ten more rows and reload the table content.
tableRowNumber += 10
tableView.reloadData()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Result:
==========================
Related Post:
how to reload a tableview in swift 3.
ReplyDeleteand how to list last few cells in tableview where to write a code plz give a idea....!
This post has been updated to Swift 3.
Delete