I have programmatically created a TableView using sections in swift. The amount of sections and rows will always be the same, as they're just displaying info that I pull in from Firebase.
I would like to change the textLabel of one of the cells to align to the top, instead of the middle of the cell. All other textLabels I would like to remain centered. The cell in question is section 1, row 1.
It currently looks like:
I've been looking for how to do this for a while, and a lot of people say to do:
cell.textLabel?.numberOfLines = 0
cell.textLabel?.sizeToFit()
But this isn't working. I'm placing this code in my cellForRowAtIndexPath method.
if indexPath.section == 0 {
if indexPath.row == 0 {
cell.textLabel!.text = "Our review"
} else {
cell.textLabel!.text = film?.Main_Review
cell.textLabel?.numberOfLines = 0 // HERE
cell.textLabel?.sizeToFit() // HERE
}
}
Nothing happens, the text still aligns to centre of the cell.
Does anyone know a simple way of doing this? Preferably without having to create a custom cell (as this only needs to effect 1 row)? Thanks
UPDATE - All code for cellForRowAtIndexPath
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: .Value1, reuseIdentifier: cellId)
cell.selectionStyle = .None
cell.textLabel?.font = UIFont(name: "Verdana", size: 14)
cell.textLabel?.textColor = UIColor.darkGrayColor()
cell.detailTextLabel?.font = UIFont(name: "Verdana", size: 14)
if indexPath.section == 0 {
if indexPath.row == 0 {
cell.textLabel!.text = "Share"
}
} else if indexPath.section == 1 {
if indexPath.row == 0 {
cell.textLabel!.text = "Our review"
} else {
cell.textLabel!.text = film?.Main_Review
cell.textLabel?.numberOfLines = 0
cell.textLabel?.sizeToFit()
}
} else if indexPath.section == 2 {
if indexPath.row == 0 {
cell.textLabel!.text = "UK release date"
cell.detailTextLabel!.text = film?.Date_Released?.capitalizedString
} else if indexPath.row == 1 {
cell.textLabel!.text = "Directed by"
cell.detailTextLabel!.text = film?.Directed_By?.capitalizedString
} else if indexPath.row == 2 {
cell.textLabel!.text = "Running time"
cell.detailTextLabel!.text = film?.Running_Time
} else {
cell.textLabel!.text = "Certificate rating"
cell.detailTextLabel!.text = film?.Certificate_Rating
}
}
return cell
}
cell.textLabel is a default label in UITableViewCell. If you want your text to start at the top you should create a subclasse of UITableViewCell and create your own view for this cell.
For example
import Foundation
import UIKit
class ItemCell: UITableViewCell {
var label : UILabel!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.label = UILabel()
self.contentView.addSubview(label)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
self.label.frame = CGRectMake(15,10,200,30)
self.label.adjustsFontSizeToFitWidth = true
self.label.textColor = UIColor.greenColor()
}
}
Then in your viewController dont forget to use the register class method like this
self.tableView.dataSource = self
self.tableView.delegate = self
self.tableView.registerClass(ItemCell.self, forCellReuseIdentifier: "cellIdentifier")
And in your cellForRowAtIndexPath
let cell:ItemCell! = tableView.dequeueReusableCellWithIdentifier("cellIdentifier") as? ItemCell
cell.textLabel?.sizeToFit()
should make your UILabel has the size need it to wrap the content. So a good thing you could do is change the background to see if this line is do the job.
I guess the problem is your constraints in the cell. You should have a look either looking at your code or your storyboards.
Autolayout tutorial
Note: you should edit your question, otherwise you could get some negatives for this. If it is section 0 can not be section 1.
if indexPath.section == 0 {
if indexPath.section == 1 {
/*your code */
}
}
Related
I try to set a string to my detailTextLabel in a tableView but it's returning nil. I have read other posts where I am not the first one but I cannot understand what is going wrong in my case. I am using Swift 4.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") else {
return UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "Cell")
}
return cell
}()
let filtersRow: Bool = (currentSearchType == .all && indexPath.section == 0)
var titleText: String = ""
if filtersRow == true {
titleText = "Filters"
var detailText: String = ""
if currentFilters.count == 0 {
detailText = "None"
}
else if currentFilters.count == 1 {
detailText = currentFilters.first!
}
else {
detailText = "\(currentFilters.count)"
}
cell.textLabel?.text = titleText /// -> shows 'Filters' as expected
cell.detailTextLabel?.text = detailText /// -> shows nothing
print("Detail text: \(cell.detailTextLabel?.text)") --> returns nil
print("cell.textLabel? \(String(describing: cell.textLabel))") /// --> Optional(<UITAbleViewLabel...>)
print("cell.detailTextLabel? \(String(describing: cell.detailTextLabel))") /// ---> nil
cell.accessoryType = .disclosureIndicator
cell.accessoryType = .disclosureIndicator
return cell
}
...
There is definitely something wrong with the way I get my cell, but I do the same thing in an other viewController and it is going well...
Does anyone would have an idea?
This happens when the detailTextLabel isnt created. Mostly a bug in your code or storyboard. So check the creation of the problematic Cell.
Read also this Stackoverflow Q&A about this topic
My tableview cell subtitles aren't showing when I use this:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell:UITableViewCell?
if tableView.tag == 1 {
guard let latestCell = tableView.dequeueReusableCell(withIdentifier: "latestCell") else {
return UITableViewCell(style: .subtitle, reuseIdentifier: "latestCell")
}
latestCell.textLabel?.text = latest[indexPath.row]
latestCell.detailTextLabel?.text = latestSub[indexPath.row]
latestCell.accessoryType = .disclosureIndicator
return latestCell
}
}
But then if I use this:
else if tableView.tag == 2 {
let olderCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "olderCell")
olderCell.textLabel?.text = older[indexPath.row]
olderCell.detailTextLabel?.text = olderSub[indexPath.row]
olderCell.accessoryType = .disclosureIndicator
return olderCell
}
else {
return cell!
}
}
The subtitles load perfectly, but after I close the app and reload the view, the app autoquits without giving a crash log or taking me to the debugging-tab.
I know that the arrays from which the data comes from are fine, and I think that I've set up everything right in the storyboard. A lot of similar questions have already been posted on the subject, but they all seem to come down to forgetting to set the cellStyle to .subtitle. Thanks in advance for any help I get!
BTW. My regular cell titles are working just like I want them to. No problem there.
In your first section, your guard statement is returning before you've set the cell's text and detail text. If you changed it to:
if let latestCell = tableView.dequeueReusableCell(withIdentifier: "latestCell") {
cell = latestCell
} else {
cell = UITableViewCell(style: .subtitle, reuseIdentifier: "latestCell")
}
cell.textLabel?.text = latest[indexPath.row]
cell.detailTextLabel?.text = latestSub[indexPath.row]
cell.accessoryType = .disclosureIndicator
return cell
The labels will now get set.
And your crash is caused by this:
return cell!
If cell == nil, then cell! will attempt to unwrap it. And really, what you should be doing is calling super's implementation:
return super.tableView(tableView, cellForRowAt: indexPath)
Good luck.
I'm having some trouble implementing a UISwitch in a UITableViewCell. My tableViewCell:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
cell.textLabel?.text = "Hello Magic Switch buyers!"
cell.textLabel?.textColor = UIColor.whiteColor()
cell.backgroundColor = UIColor.clearColor()
lightSwitch = UISwitch(frame: CGRectZero) as UISwitch
lightSwitch.on = false
lightSwitch.addTarget(self, action: "switchTriggered", forControlEvents: .ValueChanged );
cell.accessoryView = lightSwitch
return cell
}
This creates the switch, everything goes right, until the point the function gets called.. What you would normally do with for example a button, is just using it's indexPath.row to make a difference between all the cells, but as this is an accessory view, I wasn't able to get this working! The switchTriggered function:
func switchTriggered() {
if lightSwitch.on {
let client:UDPClient = UDPClient(addr: "192.168.1.177", port: 8888)
let (_) = client.send(str: "HIGH" )
print(String(indexPath.row) + " set to high")
} else {
let client:UDPClient = UDPClient(addr: "192.168.1.177", port: 8888)
let (_) = client.send(str: "LOW" )
print(String(indexPath.row) + " set to low")
}
}
The function doesn't know which lightSwitch was switched and what the indexPath is.. How could I fix this? If it was a button, I could use accessoryButtonTappedForRowWithIndexPath but this is not the case.
Some help would be appreciated, as all the information about UISwitches in TableViewCells is in Objective-C.
Thank you very much!
The easiest solution is to use tag property of the switch. When creating switch, assign a tag
lightSwitch = UISwitch(frame: CGRectZero) as UISwitch
lightSwitch.tag = 2000
lightSwitch.addTarget(self, action: Selector("switchTriggered:"), forControlEvents: .ValueChanged );
Then modify your handler method to have a sender parameter
func switchTriggered(sender: AnyObject) {
let switch = sender as! UISwitch
if switch.tag == 2000 {
// It's the lightSwitch
}
}
I've put an ImageView in a UITableViewCell on my Storyboard but at run time it is not showing up on my simulator. How do I solve this?
My storyboard:
The object overview of my storyboard:
The Swift code that fills up the cells:
func tableView(gamesListTableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = gamesListTableView.dequeueReusableCellWithIdentifier("Game", forIndexPath: indexPath) as! UITableViewCell
if (indexPath.row < GamesList.count) {
cell.detailTextLabel?.text = GamesList[indexPath.row]["game_guid"].string!;
cell.textLabel?.text = GamesList[indexPath.row]["opponent_name"].string!;
if (GamesList[indexPath.row]["self_turn"] == false) {
cell.textLabel?.textColor = UIColor.greenColor();
} else if (GamesList[indexPath.row]["game_idle_time"] <= 60) {
cell.textLabel?.textColor = UIColor.yellowColor();
} else {
cell.textLabel?.textColor = UIColor.redColor();
}
} else {
cell.detailTextLabel?.text = InvitesList[indexPath.row - GamesList.count]["invite_guid"].string!;
cell.textLabel?.text = InvitesList[indexPath.row - GamesList.count]["invite_text"].string!;
cell.textLabel?.textColor = UIColor.blueColor();
}
return cell
}
What it looks like at runtime:
You should probably pause the execution and view your view hierarchy in Xcode. Check what the frame size it at run time and see if A.) something is overlapping it or B.)If your constraints are wrong.
I have a tableView with 2 sections, the first section is for text the user inputed earlier, the other section for selections based on that text. The first section has a Default tableViewCell style, the second section has a style of Subtitle. The first section is just a single cell, and it sizes dynamically based on the amount of text without issue. The second section is multiple cells, with UITableViewCell.textLabel and UITableViewCell.detailText set. These are the cells that do not auto size properly at all, I don't know what I am doing wrong. Note: 1) I do have tableView.rowHeight = UITableViewAutomaticDimension set in the viewDidLoad() method. 2) I am not using prototype cells in the storyboard.
This article states that I "must have constraints on the contentView". I honestly have no idea what that means. I know what constraints are in terms of setting content on the storyboard. I just don't know what he means in this context or how I would go about that if I don't have prototype cells.
Also, I have to set two re-use identifiers, depending on which section it is. That way it doesn't try to reuse the cell/section I have set aside for the user's input text.
With all that said in mind, here's the code I have. I'm a newbie to Swift and developing for iOS in general so if you have suggestions/advice for refactoring feel free to let me know. I have commented out some things I have tried. Setting the row height to 66 does work, but that's not the goal here. I want it dynamic because I don't know what will change later on.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cellIdentifier = ""
if indexPath.section == 1 {
//tableView.rowHeight = 66
//tableView.rowHeight = UITableViewAutomaticDimension
cellIdentifier = "DistortionItem"
} else {
//tableView.rowHeight = 160
cellIdentifier = "NegativeThought"
}
var cell: UITableViewCell! = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell
if indexPath.section == 1 {
if cell == nil {
cell = UITableViewCell(style: .Subtitle, reuseIdentifier: cellIdentifier)
}
cell.textLabel?.text = distortionslist.distortions[indexPath.row].0
cell.detailTextLabel?.font = UIFont.systemFontOfSize(10)
cell.detailTextLabel!.text = distortionslist.distortions[indexPath.row].1
cell.textLabel?.numberOfLines = 0
cell.detailTextLabel?.numberOfLines = 0
//tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
//cell.textLabel?.sizeToFit()
//cell.detailTextLabel?.sizeToFit()
} else {
if cell == nil {
//println("Cell set to default")
cell = UITableViewCell(style: .Default, reuseIdentifier: cellIdentifier)
}
cell.textLabel?.font = UIFont.systemFontOfSize(12)
cell.textLabel?.text = entry.thoughtText
cell.textLabel?.numberOfLines = 0
//cell.textLabel?.sizeToFit()
}
return cell
}
Example screenshot: