iOS Swift - UITableViewCell with left detail and right detail and subtitle? - ios

how to add a right detail to a cell that already have left detail and subtitle. and how to give a textLabel to the right detail?
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let monster = monsters[indexPath.row]
cell.textLabel?.text = monster.name
cell.detailTextLabel?.text = monster.subtitle
return cell
}

Try this
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let monster = monsters[indexPath.row]
cell.textLabel?.text = monster.name
cell.detailTextLabel?.text = monster.subtitle
let label = UILabel.init(frame: CGRect(x:0,y:0,width:100,height:20))
label.text = monster.name
cell.accessoryView = label
return cell
}

Click on your tableView cell. It's easier to do it in document outline.
Change the style attribute in Attribute Inspector from Basic to Custom. You will now be able to add a label. The basic template is set, so you can't alter it. I believe this was the issue you were having.
I'm sure after you place the label you know what to do, but just in case:
Place the new label at the right margin of the cell and pin it to the top, bottom, and right, using Autolayout.
Reconnect your title, subtitle IBOutlets, and create a right label IBOutlet, connect it, and you should be done.

Related

How to center a spinner with cell content view after removing a cell accessory?

I have a tableview in my storyboard where the prototype cell has a disclosure indicator by default.
When I populate my table I want to remove the indicator only from the last cell AND center a spinner on it.
I'm doing it like this:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CharacterCell", for: indexPath) as! CharacterCell
if indexPath.row == charactersViewModel.charactersCount - 1 {
cell.accessoryType = .none
cell.accessoryView = .none
// Spinner
let spinner = UIActivityIndicatorView(style: .large)
spinner.color = .white
spinner.center = cell.contentView.center
cell.contentView.addSubview(spinner)
spinner.startAnimating()
}
return cell
}
The problem is that the spinner is offcenter, a little bit to the left, just like if the accessory is still there, but hidden.
I feel maybe I'm missing the lifecycle of a table cell, maybe it's getting the center value of the content view when the accessory is still there, so when it's removed it is offcenter?
I tried on willDisplay as well but the same thing happens.
Any tips on this?
As #Paulw11 mentioned, I used a second subclass and created another cell prototype in my tableview.
Then when the last position at the table is reached, we can use the second prototype on cellForRowAt.
Here how it is:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row >= charactersViewModel.charactersCount - 1 {
reloadRows(indexPath: indexPath)
let cell = tableView.dequeueReusableCell(withIdentifier: "LoadingCharacterCell", for: indexPath) as! LoadingCharacterCell
cell.startSpinner()
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "CharacterCell", for: indexPath) as! CharacterCell
cell.configureCell(charactersViewModel: charactersViewModel, cell: cell, index: indexPath.row)
return cell
}
}
private func reloadRows(indexPath: IndexPath) {
var indexPathList = [IndexPath]()
indexPathList.append(indexPath)
charactersTableView.reloadRows(at: indexPathList, with: .automatic)
}
And with the reloadRows function, the last cell is updated and removed when the table receives more data.

reloadData for UITableView changes constraints

I have a UITableView set up with cells: (no conflicting constraints)
Storyboard and Constraints
Looks like:
Pre-refresh
However, whenever I refresh using
self.tableView.reloadData()
the constraints seem to change which changes the cell into:
Post-refresh
Any suggestions as to why this happens?
Code for cellForRowAt:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CaptureCell
let capture = tabBar?.captureArray[indexPath.row]
cell.capsuleImage.image = capture?.image
return cell
}
The cell identifier is "cell" and I have CaptureCell which is a subclass of UITableViewCell.

Issue with auto layout - vertical spacing

In Xcode 8 with Swift 3 I'm using custom cell with auto layout. Information into cell inserted from JSON, so my code looks like:
//the method returning each cell of the list
public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = self.tableView.dequeueReusableCell(withIdentifier: "Feedcell", for: indexPath) as! FruitTableViewCell
//getting the hero for the specified position
let hero: List_Feed
hero = heroes[indexPath.row]
cell.labelAnswer.text = hero.answer
cell.labelQuestion.lineBreakMode = .byWordWrapping
cell.labelQuestion.text = hero.question
cell.labelQuestion.sizeToFit()
cell.labelAnswer.sizeToFit()
return cell
}
Problem is that I'm getting equal labelQuestion heigh for each cell, but text size and line size are different. How to solve this? :c
As Cell is reusing the Label You have to empty label before use for next cell
public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = self.tableView.dequeueReusableCell(withIdentifier: "Feedcell", for: indexPath) as! FruitTableViewCell
//EMPTY LABELS
cell.labelAnswer.text = ""
cell.labelQuestion.text = ""
self.layoutIfNeeded()
//getting the hero for the specified position
let hero: List_Feed
hero = heroes[indexPath.row]
cell.labelAnswer.text = hero.answer
cell.labelQuestion.lineBreakMode = .byWordWrapping
cell.labelQuestion.text = hero.question
cell.labelQuestion.sizeToFit()
cell.labelAnswer.sizeToFit()
return cell
}
Hope it is helpful to you
1- set vertical content hugging priority off today label to 1000
2- hook the bottom of the label [ that is under today label ] to the top of the imageView Below it
3- hook bottom of the most-down label to bottom of contentView
Edit :
Add this 2 lines before return cell in cellForRowAt
cell.layoutSubviews()
cell.layoutIfNeeded()

Standard TableView Cell - label text overlaps with uiimage

This is happening but I don't know why. It is a standard tableviewcell with a label and an image on it
Here is the code:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = patientsTableView.dequeueReusableCell(withIdentifier: "patientTableViewCell", for: indexPath)
let text = "Bob Marley"
cell.textLabel?.text = text
return cell
}
When I do not put an image view in the object inspector but like below, it solves the problem. I am not sure why though
cell.imageView?.image = UIImage(named: "patients")

detailTextLabel on cell created in storyboard with dynamic prototype appears only after cell is selected?

I am adding cell like below. Before selecting cell, only textLabel is able to be seen, after selection detailTextLabel getting appear too. Why detailTextLabel not on screen from beginning?
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ooo", forIndexPath: indexPath) as UITableViewCell
cell.textLabel!.textColor = UIColor.whiteColor()
cell.textLabel!.text = "asdf1"
cell.detailTextLabel!.textColor = UIColor.whiteColor()
cell.detailTextLabel!.text = "asdf2"
return cell
}

Resources