Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a header to the UITableView programatically using swift 2.2

Tags:

ios

swift

I am trying to add a header to the table view which is a NSLocalized String. I tried doing this ,

 self.tableView.tableHeaderView = NSLocalizedString("title", comment : "") as? UIView

But an error appeared that "Cast from type String to unrelated type UIView always fails". I tried without type casting as well and then the error was "Cannot convert type 'String' to the type UIView ". can anyone help me in adding this header to the tableView ? Thank you in advance.

like image 882
Maneesh Sharma Avatar asked Mar 28 '16 05:03

Maneesh Sharma


People also ask

What is Uitableview in Swift?

A view that presents data using rows in a single column.


1 Answers

You need to set the view not string.

You can set label in view and set that view in table header.

var view1: UIView = UIView.init(frame: CGRectMake(0, 0, 320, 600));
var label: UILabel = UILabel.init(frame: CGRectMake(0, 0, 320, 600))
view1.addSubview(label);
self.tableView.tableHeaderView = view1;
like image 192
Jatin Chauhan Avatar answered Sep 21 '22 13:09

Jatin Chauhan