How to hide label in tableView with custom cells? - swift

I want to be able to hide label(s) in a tableView if they are empty.
For example:
The label is populated with a variable var name = String() And if the namevariable is empty, I want to hide the label. In a normal ViewControllerI would do:
override func viewDidLoad() {
if name.isEmpty == true {
nameLabel.isHidden = true
}
How is this done in a tableView?
EDIT*
I'm sorry for the unclear and badly formatted question. I´ll try again:
I have a nameArray, and a numberArray
The nameArray can contain blank("") spaces, and the numberArray can contain 0´s
When for example the numberLabelcontains 0, I want it to be hidden. This can vary from row to row.
The nearest I have gotten is this:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "InfoCell") as! InfoCell
cell.nameLabel.text = nameArray[indexPath.row]
cell.numberLabel.text = numberArray[indexPath.row]
if numberArray.contains(0) {
cell.numberLabel.isHidden = true
}
return cell
}
But this also hides all labels with 0 in it.
How can I remove thelabels only containing one 0?

Change your
if numberArray.contains(0) {
cell.numberLabel.isHidden = true
}
into:
if numberArray[indexPath.row] == 0 {
cell.numberLabel.isHidden = true
}
The same check can be done for nameArray as well.

Related

How to display a label on the last cell only?

I have a chat message table view with two cells to display, depending on whom sent the message.
I want the last cell to display the time, and only the last one. When I use tableView(:willDisplay cell:forRowAt indexPath:), the last cell doesn't show anything...
How can I display the time on that last cell?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if chatBubbles[indexPath.row].user == UserDefaultsService.shared.userID {
let cell = tableView.dequeueReusableCell(withIdentifier: CustomCell.senderCellIdentifier.rawValue, for: indexPath) as! SenderTVC
populateSenderChatBubble(into: cell, at: indexPath)
return cell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: CustomCell.conversationCellIdentifier.rawValue, for: indexPath) as! ConversationTVC
populateConversationChatBubble(into: cell, at: indexPath)
return cell
}
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == chatBubbles.count - 1 {
// What to do in here to display the last cell time?
}
}
Here is the method that display the cell content:
func populateSenderChatBubble(into cell: SenderTVC, at indexPath: IndexPath) {
let bubble = chatBubbles[indexPath.row]
let isoDateString = bubble.date
let trimmedIsoString = isoDateString.replacingOccurrences(of: StaticLabel.dateOccurence.rawValue, with: StaticLabel.emptyString.rawValue, options: .regularExpression)
let dateAndTime = ISO8601DateFormatter().date(from: trimmedIsoString)
date = dateAndTime!.asString(style: .short)
time = dateAndTime!.asString()
if dateAndTime!.isGreaterThanDate(dateToCompare: Date()) {
dateToShow = "\(date!) \(time!)"
}
else {
dateToShow = "\(time!)"
}
cell.senderDateLabel.text = dateToShow
cell.senderConversationLabel.text = bubble.content
}
The cell doesn't know it's last unless you tell it, but the tableView does know who's last. With that in mind, I would add a boolean in your cell like this:
var isLastCell: Bool = false {
didSet {
// do stuff if it's the last cell
if isLastCell {
// configure for isLastCell
} else {
// configure it for !isLastCell
}
}
}
When your custom UITableViewCell class initializes, it'll be with isLastCell = false, so assume that in your configuration. Whenever the boolean is updated to true, the cell will update via the didSet.
Then, in your cellForRow method, test the indexPath to see if it's the last indexPath of the datasource, if so, cell.isLastCell = true and the didSet in the cell will trigger to do whatever adjustments you need to do.
Another thing you'll need to do with this implementation is use cellForRow to update isLastCell for not just the last cell, but the cells that aren't last, since cells are created and destroyed all the time and the last cell at one moment might not be the last cell in another.

How to fix UITableViewCell shrinking on certain devices?

I'm working on an app that will show notifications, allow the user to post comments and edit the notification and see comments on the notification.
To do this I have 3 Prototype cells called "Active", "Control" and "Comment" cells. My first two Active and Control look and work fine with their auto-sizing on all devices. The comment cell, however, shrinks on Plus-sized iOS Devices, not including iPads to an unreadable size.
The comment cell simply has 3 labels of slightly different sizes and colors inside of a Stack View. This is on Swift 4.
The list of devices this happens on:
6+, 6s+, 7+, 8+, X, Xs, Xs Max
https://imgur.com/a/QHPTlSW
The above shows the cell as expected on an iPhone XR and as unexpected on an Xs Max
I've tried editing the compression and hugging rules, tried to use a constraint to force the height (which technically worked however it ruined the look and threw a warning).
func numberOfSections(in tableView: UITableView) -> Int {
if activeEvents.count == 0 {
self.tableView.isHidden = true
self.activityIndicator.isHidden = false
self.activityLabel.isHidden = false
self.activityIndicator.startAnimating()
} else {
self.tableView.isHidden = false
self.activityIndicator.isHidden = true
self.activityLabel.isHidden = true
self.activityIndicator.stopAnimating()
}
return activeEvents.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if activeEvents[section].expanded {
return activeEvents[section].comments.count + 2
} else {
return 1
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let activeEvent = activeEvents[indexPath.section]
let cell = tableView.dequeueReusableCell(withIdentifier: "activeCell") as! ActiveCell
cell.selectionStyle = .none
cell.setCell(active: activeEvent)
return cell
} else if indexPath.row == 1 {
let activeEvent = activeEvents[indexPath.section]
let cell = tableView.dequeueReusableCell(withIdentifier: "controlCell") as! ControlCell
cell.setNoti(active: activeEvent)
cell.selectionStyle = .none
cell.delegate = self
cell.vc = self
return cell
} else {
let activeEvent = activeEvents[indexPath.section]
let activeComment = activeEvent.comments[indexPath.row - 2]
let cell = tableView.dequeueReusableCell(withIdentifier: "commentCell") as! CommentCell
cell.setCell(comment: activeComment)
return cell
}
}
One thing you can do is add a stackView in your cell, then add your text label inside said stackView, just pin everything to the edges and DON'T set the height constraint in neither of the elements, that should auto resize the cell accordingly, also I don't see it in your code, but I assume you are implementing the tableView.rowHeight = UITableViewAutomaticDimension in your viewdidload()
You just need to check your constraints, as I said for automatic dimension to work you need to NOT set height constraints on the cells, here is a link that you'll find helpful raywenderlich.com/8549-self-sizing-table-view-cells

tableview with custom table view cells disappear on scroll

I have a table view with custom tableview cells.
Each may have different heights.
Totally there are 14 rows but other rows are not visible and when i scroll up and down the rows are disappearing.
Please suggest where i am doing wrong. This is bugging me from 2 days. I am not able to find any solution.
Please find my code below for the tableview.
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if(fetchedElements[indexPath.row].elementType=="textarea")
{
return 100 ;
}
else if (fetchedElements[indexPath.row].elementType=="picklist")
{
return 60;
}
else if (fetchedElements[indexPath.row].elementType=="divider")
{
return 30;
}
else if (fetchedElements[indexPath.row].elementType=="scanner")
{
return 50;
}
else if (fetchedElements[indexPath.row].elementType=="multiselect")
{
return 60;
}
else if(fetchedElements[indexPath.row].elementType=="text")
{
var length = fetchedElements[indexPath.row].elementText?.characters.count
if length! < 30
{
return 80;
}else if length! < 60 && length! > 30 {
return 110;
}else if(length! < 100 && length! > 60)
{
return 130;
}else if(length! < 150 && length! > 100 )
{
return 170;
}
else{
return 140;
}
}else
{
return 200;
}
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("No of elements Fetched===\(fetchedElements.count)")
return fetchedElements.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var variableType = fetchedElements[indexPath.row].elementType
if(variableType==nil)
{
variableType = "";
}
var elementType = variableType!
print("=============Element Type=\(elementType)==============")
let frame = UIScreen.main.bounds
switch elementType {
case "picklist":
let dpcell = Bundle.main.loadNibNamed("Textbox", owner: self, options: nil)?.first as! Textbox
dpcell.backgroundColor = UIColor.lightGray
dpcell.txtValue.layer.cornerRadius = 3
dpcell.LabelName.text = "Country"//fetchedElements[indexPath.row].elementText
dpcell.txtValue.tag = Int(fetchedElements[indexPath.row].elementId)
dpcell.txtValue.addTarget(self, action: #selector(GoToDropdown), for: UIControlEvents.touchDown)
dpcell.txtValue.backgroundColor = UIColor.white
dpcell.txtValue.titleLabel?.numberOfLines = 0
dpcell.txtValue.titleLabel?.adjustsFontSizeToFitWidth = true;
dpcell.LabelName.lineBreakMode = .byWordWrapping
dpcell.LabelName.numberOfLines = 0
dpcell.selectionStyle = .none
print("============picklist==========\(indexPath.row)")
return dpcell
case "multiselect":
let dpcell = Bundle.main.loadNibNamed("Textbox", owner: self, options: nil)?.first as! Textbox
dpcell.backgroundColor = UIColor.lightGray
dpcell.txtValue.layer.cornerRadius = 3
dpcell.LabelName.text = fetchedElements[indexPath.row].elementText
dpcell.txtValue.tag = Int(fetchedElements[indexPath.row].elementId)
dpcell.txtValue.addTarget(self, action: #selector(GoToMultiSelect), for: UIControlEvents.touchDown)
dpcell.txtValue.backgroundColor = UIColor.white
dpcell.txtValue.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left
dpcell.txtValue.titleLabel?.numberOfLines = 0
dpcell.txtValue.titleLabel?.adjustsFontSizeToFitWidth = true;
dpcell.selectionStyle = .none
print("===========multiselect===========\(indexPath.row)")
return dpcell
default:
let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! UITableViewCell
cell.textLabel?.text = "default"
print("==========dummy cell============\(indexPath.row)")
return cell
}
This happens after scroll down and up
Note : I can't comment because i don't have enough reputation, so i could not ask you regarding certain queries i have.. But by reading your codes, this is what i can suggest.
I can see that your data has quite a number of element types,for example, Picklist, divider, scanner and ect, in your heightForRow Delegate method..
However, in your cellForRow function, you have only two cases, PickList and MultiSelect... Thus, all the data types that have different element type, will return you a cell with a big "default" on it.
The only peculiar thing that i do not understand is that the tableview seems to have loaded perfectly in the first try. Thus, i was wondering on how you set up the tableview.
If you set it up in storyboard manually.. Then the first time, when the application loads, it will present whatever you have designed in your UITableView, but the moment you scroll up and down, the CellForItem method will kick in, and re-write the cell, returning you the big "default" cell you see on your list.
So if i am guessing it right..
Then what you have to do is simply adding all the type cases in your cellForRow methods's switch statement.
You might want to look into dispatch async & tableView.reloadData
Since I cannot see all of your code, I would suggest that you create a function that will be called inside viewDidLoad. Inside this function, of course include the lines below, as well as whatever you want inside of your tableView cells. I'm assuming you're using an array for your tableViews data
DispatchQueue.main.async {
tableView.reloadData
}

Swift two table view cell get the same value

I have used a tableview with 16 cells on a view controller. Each cell has a textfield and a picker view as a inputview for textfield. The odd thing is that When I choose the value for the first cell, it's fine. When I scrolled down to the last cell, the value is same as the first one. But I have never touched the last cell. Why would this happened?
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
// selected value in Uipickerview in Swift
answerText.text = pickerDataSource[row]
answerText.tag = row
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = myTable.dequeueReusableCellWithIdentifier("addFollowCell", forIndexPath: indexPath) as! AddFollowTableViewCell
cell.questionView.text = listQuestion1[indexPath.row]
cell.pickerDataSource = dictPicker[indexPath.row]!
cell.answerText.addTarget(self, action: #selector(AddFollowUpViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingDidEnd)
return cell
}
func textFieldDidChange(sender: UITextField){
let rowIndex: Int!
let selectValue = sender.tag
if let txtf = sender as? UITextField {
if let superview = txtf.superview {
if let cell = superview.superview as? AddFollowTableViewCell {
rowIndex = myTable.indexPathForCell(cell)?.row
dictAnswer[rowIndex] = selectValue - 1
}
}
}
}
After two days, it solved by thousands of trials:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell = myTable.dequeueReusableCellWithIdentifier("addFollowCell") as! AddFollowTableViewCell
if(cell.identifier == true){
cell.answerText.text = selectedAnswerForRow[indexPath.row]
}
cell.questionView.text = listQuestion1[indexPath.row]
cell.pickerDataSource = dictPicker[indexPath.row]!
dictAnswer[indexPath.row] = cell.pickerValue
cell.answerText.addTarget(self, action: #selector(AddFollowUpViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingDidEnd)
cell.identifier = true
return cell
}
func textFieldDidChange(sender: UITextField){
let rowIndex: Int!
let cell = sender.superview?.superview as! AddFollowTableViewCell
rowIndex = myTable.indexPathForCell(cell)?.row
selectedAnswerForRow[rowIndex] = cell.answerValue
print(selectedAnswerForRow[rowIndex])
cell.answerText.text = sender.text
cell.identifier = true
}
It might have some performance issue need to be optimised , but it shows exactly what i want. LOL
You're basically recycling your views and not clearing them. That's the whole point of -dequeueReusableCellWithIdentifier:indexPath:.
Allocating and deallocating memory is very power consuming, so the system recycles every cell that goes out of viewport bounds.
You don't set the text inside answerText (I assume it's the text field that causes trouble) so its content will be kept when recycled.
Assuming you'll store user selection inside a dictionary var selectedAnswerForRow: [IndexPath:String]:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = myTable.dequeueReusableCellWithIdentifier("addFollowCell", forIndexPath: indexPath) as! AddFollowTableViewCell
cell.questionView.text = listQuestion1[indexPath.row]
cell.pickerDataSource = dictPicker[indexPath.row]!
cell.answerText.addTarget(self, action: "textFieldDidChange:"), forControlEvents: UIControlEvents.EditingDidEnd)
cell.answerText.text = self.selectedAnswerForRow[indexPath] ?? "" // add this
return cell
}
self.selectedAnswerForRow[indexPath] ?? "" returns the result or an empty string if it's not present in the dictionary.
Also, you're adding several times the action for edition control event. You have to check first if it isn't already bound.
Because the cell is reused. So you have to implement prepareForReuse() in your custom cell class and reset all the changing variables
UPDATE
See :
class MyCell : UITableViewCell {
#IBOutlet weak var myTextField : UITextField!
//Add the following
override func prepareForReuse() {
myTextField.text = nil
myTextField.inputView = myPickerView
super.prepareForReuse()
}
}

Swift TableView with multiple prototype cells are not displaying all Rows

I have a UITableView created with 2 prototype cells, each of which have separate identifiers and subclasses.
My problem is when I display the cells the second prototype's first row gets absorbed under the first prototype cell.
For example, I have the first prototype cell displaying only 1 item. The second prototype cell should display 4 items. But, the first item from the second prototype cell is not displaying and, instead, there are only 3 of the four items visible.
Here is the code I have implemented:
override func viewDidLoad() {
super.viewDidLoad()
self.staticObjects.addObject("Please...")
self.objects.addObject("Help")
self.objects.addObject("Me")
self.objects.addObject("Thank")
self.objects.addObject("You")
self.tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.objects.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if(indexPath.row==0){
let staticCell = self.tableView.dequeueReusableCellWithIdentifier("staticCell", forIndexPath: indexPath) as! StaticTableViewCell
staticCell.staticTitleLabel.text = self.staticObjects.objectAtIndex(indexPath.row) as? String
return staticCell
}
else{
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! PillarTableViewCell
cell.titleLabel.text = self.objects.objectAtIndex(indexPath.row) as? String
return cell
}
Thanks for all the help.
You have logic issues with how you are counting the number of rows in your table for both tableView:numberOfRowsInSection and tableView:cellForRowAtIndexPath.
Your code is producing a display output as shown below where:
The blue cells represent your staticCell prototype table cell view; these are the values from the staticsObjects array.
The yellow cells represent your cell prototype table cell view; these are the values from the objects array.
1. Look at tableView:cellForRowAtIndexPath:
In the cellForRowAtIndexPath method, you are only returning the count of the objects array.
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.objects.count
}
That means that the number of rows you will have in your table will be 4 instead of 5. Instead, you want to return the sum of the two arrays you are using in your table: objects.count + staticObjects.count. For example:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objects.count + staticObjects.count
}
2. Look at tableView:cellForRowAtIndexPath:
Here's your original code with my comments..
// You are assuming that `staticObjects` will always have
// exactly one row. It's better practice to make this
// calculation more dynamic in case the array size changes.
if (indexPath.row==0){
let staticCell = self.tableView.dequeueReusableCellWithIdentifier("staticCell", forIndexPath: indexPath) as! StaticTableViewCell
staticCell.staticTitleLabel.text = self.staticObjects.objectAtIndex(indexPath.row) as? String
return staticCell
} else {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! PillarTableViewCell
// Here's your problem! You need to calculate the row
// because you want to put the objects from your other
// array first. As a result, you need to account for them.
cell.titleLabel.text = self.objects.objectAtIndex(indexPath.row) as? String
return cell
}
Now, here's one way to fix your errors stated in the above discussion:
// If the indexPath.row is within the size range of staticObjects
// then display the cell as a "staticCell".
// Notice the use of "staticObjects.count" in the calculation.
if indexPath.row < staticObjects.count {
let staticCell = self.tableView.dequeueReusableCellWithIdentifier("staticCell", forIndexPath: indexPath) as! StaticTableViewCell
staticCell.staticTitleLabel.text = self.staticObjects.objectAtIndex(indexPath.row) as? String
return staticCell
} else {
// If you get here then you know that your indexPath.row is
// greater than the size of staticObjects. Now you want to
// display your objects values.
// You must calculate your row value. You CANNOT use the
// indexPath.row value because it does not directly translate
// to the objects array since you put the staticObjects ahead
// of them. As a result, subtract the size of the staticObjects
// from the indexPath.row.
let row = indexPath.row - staticObjects.count
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! PillarTableViewCell
cell.titleLabel.text = self.objects.objectAtIndex(row) as? String
return cell
}
Now you should see this: