Reputation: 449
I'm not sure the best way to show the issue here with multiple files in an xCode project, so forgive me if this isn't the best way--and please let me know what else I can show!
In any case, I have a custom table view displaying from a nib (.xib), and have multiple dynamic height labels within that custom table view. Everything is displaying great, except for the fact that the table cell height overall is not accommodating for the increased height of the elements within.
What I can see happening is that the row height value for the Table View is being used as an absolute, and I'm not sure how to override that. I have that set as 200, and it's fixed for every table, even if the content overflows that height:
I'm using auto-layout, which is working great among the cell elements themselves... it's just the cell height that's not being set.
Here's my code for the elements within the cell:
class EventTableViewCell: UITableViewCell {
func loadItem (#date:String, title:String, description:String, image:UIImage) {
dateLabel.text = date
titleLabel.text = title
eventImage.image = image
eventDescription.text = description
titleLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
titleLabel.numberOfLines = 0
self.contentView.addSubview(titleLabel)
eventDescription.setTranslatesAutoresizingMaskIntoConstraints(false)
eventDescription.numberOfLines = 0
self.contentView.addSubview(eventDescription)
}
@IBOutlet weak var eventImage: UIImageView!
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var eventDescription: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
And the class that handles the data:
class Contacts: UITableViewController, UITableViewDataSource, UITableViewDelegate {
var eventData:[AnyObject] = []
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return eventData.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as EventTableViewCell
if (eventData.count != 0) {
var event: AnyObject? = eventData[indexPath.row]
if let unwrappedEvent: AnyObject = event {
let eventTitle = unwrappedEvent["title"] as? String
let eventDate = unwrappedEvent["date"] as? String
let eventDescription = unwrappedEvent["description"] as String
let eventImage = unwrappedEvent["image"] as? UIImage
if (eventImage != nil){
cell.loadItem(date: eventDate!, title: eventTitle!, description: eventDescription, image: eventImage!)}
else {
let testImage: UIImage = UIImage(named: "test-image.png")
cell.loadItem(date: eventDate!, title: eventTitle!, description: eventDescription, image: testImage )
}
}
}
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
var nib = UINib(nibName: "CustomTableViewCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "customCell")
let manager = AFHTTPRequestOperationManager()
manager.GET( "http://[my-json-file].json",
parameters: nil,
success: {
(operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
println("JSON: " + responseObject.description)
self.eventData = responseObject!["events"] as [AnyObject]
self.tableView.reloadData()
},
failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
println("Error: " + error.localizedDescription)
})
}
I'm just not sure what else I can do at this point to get the cell height to work dynamically. I've tried just about everything. Any help would be greatly appreciated, and again, please let me know if there's anything else I need to show from my code, etc.
Thanks so much in advance!
Upvotes: 0
Views: 331
Reputation: 33967
I notice that in your Contacts class, you are not implementing the tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath)
method. I suspect that if you do that, then the code will work.
Upvotes: 0