UITableViewCell title positioning - swift

I am creating an app with UITableViewCells that expand when selected. However, when the cell expands, the title (and subtitle) are moving downwards to stay centered vertically in the cell. I'd like them to stay where they were originally but can't figure out how to do it.
I have tried setting the frame manually with cell.textLabel?.frame = myRect, but this does nothing. I am unable to find any constraints, as print(cell.textLabel?.constraints) returns optional([]).
Here are my normal cells and my expanded cells.
My code for creating the cells:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier")
if cell == nil {
cell = UITableViewCell(style: .value1, reuseIdentifier: "reuseIdentifier")
}
cell!.textLabel?.text = content[indexPath.row]
cell!.clipsToBounds = true
cell!.selectionStyle = .none
cell!.detailTextLabel?.text = detailContent[indexPath.row]
return cell!
}
Any help would be greatly appreciated.

You are using a standardized cell, the title and detail are fixed, i think you need make a custom cell for your project
Look this project I make a simple test with customized cell
https://github.com/TexugoProgramador/Teste-Celula-Customizada

Related

How to pin Cell on select in Tableview?

I would like the selected cell to be pinned to the Tableview top and stay there while scrolling like section header when .plain style is used but for cell.
I don't know how to approach the problem. I know that the code should be placed at didSelectRowAt.
If your tableView cells are static, and you want the selected cell to be displayed at the top, You should try:
optional func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedRow = tableView.cellForRow(at: indexPath) as! yourTableViewCell // this wont apply if your cells are static
let firstVisibleIndexPath = tableView.indexPathsForVisibleRows.first
tableView.moveRow(at: indexPath, to: firstVisibleIndexPath)
}

textAlignent to right in cellForRowAt in Swift 3.0

I cannot manage to get my text to align to the right side of my table view. My code sets the text alignment to right but it only justifies the text if there is more that 1 line. Here is my cellforRowAt code
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellId)
cell.textLabel?.text = self.messages[indexPath.row]
cell.textLabel?.numberOfLines = 0
cell.textLabel?.textAlignment = .right
return cell
}
How can I fix this?
You're using the subtitle UITableViewCell style aren't you? This is a style that for what ever reason don't allow you to override the text label's alignment.
To solve this problem, you can make a simple subclass of UITableViewCell with a label in the same position.
Let change the style of cell to default:
let cell = UITableViewCell(style: .default, reuseIdentifier: cellId)
I created a subtitle cell in the storyboard and I realized the textLabel's size is fit to its content. So you can't align the text of textLabel. But you can do this with basic cell, its size is matched to the cell.

Swift TableView Prevent Button color change on selection

I am having a hard time preventing color change on my background color for my IBAction button when I select a row in my tableView. The background of my button is set to green, but when I tap the table view cell, it becomes gray. I have tried to change the color through different functions (tableView didSelectRowAtIndexPath etc..). Any ideas on how I would prevent the background of the button to change?
Thanks in advance!
I have faced same problem with button backgroundColor in TableView and solved it this way.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CustomCell
cell.button.backgroundColor = UIColor.greenColor()
}
If you want to disable color change on selection entirely, change the selectionStyle attribute in your custom cell:
selectionStyle = .none
I solved the problem with the code Nirav provided me above:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CustomCell
cell.button.backgroundColor = UIColor.green
}
However, I noticed that the button background color continued to turn gray when I tapped the row, and held the 'tap' without releasing. I solved this by adding the following code as well:
func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CustomCell
cell.exploreButton.backgroundColor = UIColor.green
}

Center text around specific word in table cell using Swift

I have a UITableView with multiple cells, each containing a sentence of attributed text. I would like to know how to centre the text in each of these cells around a specific word contained in those sentences.
The result would be a central column of the word but each cell containing different surrounding words.
I'm guessing the place to start is to centre the text in the cell array but doing so in the "cellForRowAtIndexPath" delegate method with the line:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
self.cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")
if(cell.isEqual(NSNull)) {
self.cell = NSBundle.mainBundle().loadNibNamed("cell", owner: self, options: nil)[0] as! UITableViewCell;
}
self.cell.detailTextLabel?.text = self.synonymsCategories[indexPath.row]!
self.cell.textLabel?.attributedText = self.synonymsContext[indexPath.row]
self.cell.detailTextLabel?.textAlignment = .Right
self.cell.textLabel?.textAlignment = NSTextAlignment.Center
self.cell.textLabel?.backgroundColor = UIColor.greenColor()
return self.cell as UITableViewCell
}
But no changes I make to "self.cell.textLabel" produce any visual changes at run time.
Any ideas? Any help much appreciated.
Felix

Swift - UITableViewCell label position is shifted after scrolling

The Table View Cells are laid out correctly during the initial load, but when I scroll up or down, the labels are shifted to the left. I'm not sure what is going on here.
My code for the tableview cell is below.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let workout = self.workouts[indexPath.row] as? Workout
let cell = tableView.dequeueReusableCellWithIdentifier("WorkoutCell") as? WorkOutCell
cell!.textCell?.text = workout?.title
cell!.backgroundColor = workout?.color
cell!.countLabel.text = "\(indexPath.row+1)"
cell!.selectionStyle = UITableViewCellSelectionStyle.None
return cell!
}
Image:
Thanks much!
Is there any changes if use this instead?
let cell = tableView.dequeueReusableCellWithIdentifier("WorkoutCell", forIndexPath: indexPath) as? WorkOutCell