Reputation: 91
I'm in trouble.I have a city list, I want to display cities in UITableView
and I have disabled UITableView's scroll because i don't want to display scroll.
when I changed to UITableView's height dynamically, it doesn't changed and UITableView does not display all cities. Below the code. Please look it.
import UIKit
class FlightDetailsOneWayViewController: UIViewController {
var flightDetailArr:[FlightDetailsOneWay] = [FlightDetailsOneWay]()
@IBOutlet var tableView: UITableView!
@IBOutlet var scrollview: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
flightDetailArr.append(FlightDetailsOneWay(depCity: "London"))
flightDetailArr.append(FlightDetailsOneWay(depCity: "China"))
flightDetailArr.append(FlightDetailsOneWay(depCity: "Singapore"))
flightDetailArr.append(FlightDetailsOneWay(depCity: "Dubai"))
self.tableView.scrollEnabled = false
var cellHeight = CGFloat(self.tableView.rowHeight)
var totalCity = CGFloat(flightDetailArr.count)
var totalHeight = cellHeight * totalCity
self.tableView.frame.size.height = CGFloat(totalHeight)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return flightDetailArr.count
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
let cell:FlightDetailsCell = tableView.dequeueReusableCellWithIdentifier("FlightDetail") as FlightDetailsCell
let flight = flightDetailArr[indexPath.row]
cell.setCell(flight.depCity)
return cell
}
}
class FlightDetailsOneWay
{
var depCity = ""
init(depCity: String)
{
self.depCity = depCity
}
}
class FlightDetailsCell: UITableViewCell {
@IBOutlet var depCity: UILabel!
override func awakeFromNib()
{
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool)
{
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func setCell(depCity: String)
{
self.depCity.text = depCity
}
}
below the preview of above code.
Upvotes: 2
Views: 12890
Reputation: 23053
As explanation required to this answer is very detailed.
Follow my other answer "Moving views with constraints" to get idea about how to update layout constraint.
And as per your comment about how to connect NSLayoutConstraint variable with height constraints? as below:
UIView
in which you have added constraint. Click on Constraints icon to see all constraints. Right now in below image I have added only one constraint you may find more than that. Check for which constraint you have change it's properties.heightConstraint
.Upvotes: 5
Reputation: 855
You can use prototype cell if all your cell will have the same look:
a complete tutorial to make a project using prototype cell
or
a second which it more quick to read
You can can also use
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 42.0
}
This function is perfect if you resize dynamically the height cell.
You just need to use: beginUpdates()
and endUpdates()
for make it works.
Upvotes: 2