This tutorial i want to shows you how to use “Table view ” in swift:
Create Project
· ·
You need to open Xcode ,create a new project for “single view
Application”
·
you can create a new project with Comand-Shift-N
fill in your information your project
·
Product Name :Tableview – this the name of your project
·
Company Identifier : KienPhamDev It’s actually the domain name
written the other way round.if you have
domain,you can use your own domain name otherwise you may use mine or just fill in
·
Device family :you can chose universal it’s not important
·
Choose swift as Language
Click “Next” to continue .Xcode the asks you where you saves
your project you can save any folder click create to continue
Click here
main.storyboard
Drag in a new Table View Controller from the Object Library
Open the “Assistant editor”
and control-drag from the "Tableview" and make an outlet named myTableview:
Close Assistantant editor
and going to “Viewcontroller.file”
Ok fist thing you need to
know “UITableview ”and “UIpickerview ” like the same
It need delegate and datasourse in the “viewdidload” above we set
both the delegate and dataSource to to self so we can add the required methods
here change the class declaration to this :
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
Add some data to dislay:
Add a property called items as an
Array of Strings and set some values
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
var animal = ["horse","cow","dog"]
…}
after that I
need to emlement:
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return
animal.count
}
Create the cell:
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = animal[indexPath.row]
return cell
}
Then, Build and Run the
application you will get the output like below.






Comments
please post any question and comment right here so that i can support for you
Post a Comment