To create a tableView in swift please follow following steps:
Step 1: Add a tableView in viewController.
Step 2: set the dataSource and delegate
Step 3: Create a prototype cell
Step 4: Change the class of prototypecell to "TableViewCell"
Step 5: To create the "TableViewCell" add a new file with subclass of "UITableViewCell" class and Uncheck the "Also Create the xib file"
Step 6: Write down the these line of code
import UIKit
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate
{
@IBOutlet weak var MyTableView: UITableView! //Create the outlet of the tableView
let myRowArray = ["Ray Wenderlich", "NSHipster", "iOS Developer Tips", "Jameson Quave", "Natasha The Robot", "Coding Explorer", "That Thing In Swift", "Andrew Bancroft", "iAchieved.it", "Airspeed Velocity"];
let myRowArray2 = ["Andrei", "Silviu", "Claudiu","Andrei", "Silviu", "Claudiu","Andrei", "Silviu", "Claudiu","Silviu"];
let cellidentifier="MyCellIDentifier";
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1;
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myRowArray.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell=tableView.dequeueReusableCellWithIdentifier(cellidentifier, forIndexPath: indexPath) as TableViewCell;
cell.CellData(myRowArray[indexPath.row], dest: myRowArray2[indexPath.row]);
return cell;
}
}
Step 7: In TableViewCell file add a method like this to set the data in the cell
func CellData(personName: NSString,dest:NSString)->Void
{
cellLabel.text=personName;
CellLabel2.text=dest;
}
0 Comment(s)