hello friends,
if you would like to Load custom cell(Xib) in UITableView in swift ,you may use the following code ->
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count; //This function returns number of rows in table view
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:YourTableViewCell! = tableView.dequeueReusableCellWithIdentifier("YourTableViewCell", forIndexPath:indexPath)as! YourTableViewCell
// This function actually loads the xib
if cell == nil{
let cellnib = [NSBundle.mainBundle().loadNibNamed("YourTableViewCell", owner:self, options: nil)]
cell = cellnib.first! as! YourTableViewCell
}
// Do additional code here
}
Dont forget to register nib in viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
// registering your nib
let yourNibName = UINib(nibName: "YourTableViewCell", bundle: nil)
tableView.registerNib(yourNibName, forCellReuseIdentifier: "YourTableViewCell")
// Do any additional setup after loading the view.
}
0 Comment(s)