Cannot Change Text of Label in TableView - swift

I have a tableView which displays the address and phone number of a business. I want to use a label next to the address and phone number. For some reason, all of my labels say "Address" even though I have the phone label set as "Phone" in the storyboard and in the code. Why is it doing this?
How it looks in the storyboard
How it looks when I run it
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! ProfileCell
cell.addressLabel.text = "Address:"
cell.phoneLabel.text = "Phone:"
cell.addressInfoLabel.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]
cell.phoneInfoLabel.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]
return cell
}

Remove following lines
cell.addressLabel.text = "Address:"
cell.phoneLabel.text = "Phone:"
My guess is you have set outlet reference to addressLabel, what should have been referred to phoneLabel. Check that.
I checked your code. The problem is with height of the labels. Address label is overlapping phone label. Here are the things you can do
Change the background colors of the address label and phone label. You will realise that address label is completely hiding phone label.
Remove aspect ratios for the labels and give some constant height.e.g. 30
Implement heightForRow for tableView and give some height e.g. 150.

Related

Setting the height: TableViewCells with custom Nib that are different sizes

I have a TableViewController that has a custom TableViewCell under the identifier "customCell". Heres an image of the configuration of the cell along with the IBOulets connected to it:
The cell takes information from my backend and presents it. The description text view (just noticed that I accidentally named it descriptionLabel) doesn't allow scrolling so it expands based off of the content that it's holding. The database is being processed correctly from the database, and it's displaying on the app. The only problem is that the cell is not its correct height. On the TableViewControl that's registering this cell through its identifier, I automatically set the height of the cell using UITableViewAutomaticDimension in heightForRow:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
but that's not working either. *The only thing that works is when I set the height of each cell to a standard value such as 200. That doesn't work for me because each cell will be a different height because of the length of the textView.
How do I set the height of a custom nib (tableViewCell) so that it adjusts based off of the content within it instead of setting the height to a specific value?
1-Remove both textViews and replace them with labels
2- Title lbl with theses constraints
top,leading,trailing , .lines = 0
3- Description lbl with theses constraints
bottom ,leading,trailing to contentView,top to bottom of title lbl , .lines = 0
Off course you can leave the 2 textviews , but you have to give each one an initial height and do this in
override func layoutSubviews() {
super.layoutSubviews()
self.titleTvHeight.constant = self.titleTv.contentSize.height
self.desTVheight.constant = self.desTv.contentSize.height
}
//
Don't forget to set this in viewDidLoad of the VC
tableView.estimatedRowHeight = 200
tableView.rowHeight = UITableViewAutomaticDimension
Anyway you can remove heightForRowAt

iOS Cell TextField Text Size Not Resizing When Scrolling Swift 3

I am having an issue when scrolling in a collection view cell. The text isn't resizing to fit based on the populated value and space available. In the image that isn't working as expected, I have scrolled down a "page" or 2. In the set-up below, extraDetail1 seems to scales based on the first scale size of the first cell created. It appears that when the cell is re-used, the text doesn't resize. I want "extraDetail1" to reduce font size when needed. I am even okay if extraDetail1 truncates if that is my only choice. I don't want the text to overlap. I prefer to have "extraDetail2" = "$..." not reduce in size, but I am okay if it also reduces similar to extraDetail1 if needed.
// cell
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let actionFigure = headerArray[indexPath.section].sectionActionFigureArray[indexPath.row]
if isShowPhotoView {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kPhotoCollectionCellIdentifier, for: indexPath) as! ImageCollectionCell
cell.configureForPhotoCollectionCell(forActionFigure: actionFigure)
return cell
}
else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kDetailCollectionCellIdentifier, for: indexPath) as! DetailCollectionCell
cell.configureForDetailCollectionCell(forActionFigure: actionFigure, forFigureCondition: ebaySearchConditionSelected)
return cell
}
}
view layout
cell setup
configuration
what shows up, green is correct, red isn't. I have the same issue with the other text boxes. See that the PDT-8 title line runs off too when its too long.

Swift TableView cell doesn't fit into 1 row

I'm working on a swift project. I have a tableview that loads the data correctly. However, I have many cells for each row and it doesn't fit into the iphone screen, and got cut off the screen. I would like to have some cells go on to the next line instead of not showing on the screen. How to do that? Thanks.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ResultTableViewCell
//cell.textLabel?.numberOfLines = 0
//cell.textLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
let category: Category = categories[indexPath.row]
cell.lblName.text = category.name
cell.lblQty.text = category.qty
cell.lblSub.text = category.sub
cell.lblSub2.text = category.sub2
cell.lblCountry.text = category.country
cell.lblState.text = category.state
cell.lblCity.text = category.city
return cell
}
On my screen, it can only fit 5 columns, and the rest gets cut off. I want to show each row in 2 lines. The first line will display the name, qty, sub, sub2 and the next line will display country, state and city.
It doesn't tell us much without seeing the layout of your cells and some example screens presenting the issue. But it's possible that you would want to look at autoresizing cells and UITableViewAutomaticDimension.

Use tags for tableView inside UIViewController to update Labels

I added a table view to my UIViewController. then i added a label to my custom cell inside the table view. i tried to update labels inside the tableview by using tags. seems it does not working.
this is my code.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("LineItemCell", forIndexPath: indexPath)
let object = lineItemsObject[indexPath.row]
//cell.textLabel?.text = object["quantity"]
print(object["description"]!)
if let descriptionLabel = cell.viewWithTag(500) as? UILabel {
descriptionLabel.text = object["description"]
}
else
{
print("fail")
}
return cell
}
When i call the function always it does not read the tag and prints "fail". i have assigned correct tag value to my label also.
here i have attached a image of my label details in attribute inspector
Please help me the fix the issue.
As #TheAppMentor said code looks fine. Make sure you entered correct cell identifier. Then make sure you connected your view controller to correct class (it's my common mistake). Also make sure that something else in this cell hasn't the same tag. You can also print all subviews to check what's inside.
Your cell is not knowing about your label inside (the one with 500 tag). You are not providing a custom cell, because of this line:
let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("LineItemCell", forIndexPath: indexPath)
This is a predefined cell. As of documentation you can have predefined cells or custom ones (obtained by subclassing UITableViewCell):
"When creating cells, you can customize them yourself or use one of several predefined styles. The predefined cell styles are the simplest option. With the predefined styles, the cell provides label and image subviews whose positions and styling are fixed. ... To set the text and images of the cell, use the textLabel, detailTextLabel, and imageView properties."
If you want to go predefined:
If you want to just put the text onto cell use (I see this one is commented on first place).
cell.textLabel?.text = object["quantity"]
Go custom
Extend UITableViewCell on a separate swift file. Do your bindings here and work with your storyboard in parallel. Assign the custom class to your cell. Also, change dequeue:
let cell: MyCustomTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("LineItemCell", forIndexPath: indexPath) as! MyCustomTableViewCell

iOS8 Swift - Problems dynamically resizing tableview cells

Here's what my application looks like currently.
You'll see that when the subtitle text label is only one line is resizes correctly, but when there's multiple lines it gets all messed up. I think this has to do with the constraints possibly? Right now I'm using the auto-layout constraints. You can see them in the screenshot. Here's the code that creates my cells.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> DealCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Deal Cell", forIndexPath: indexPath) as DealCell
let currentBar = bars[indexPath.row] as BarAnnotation
cell.barName.text = currentBar.name
cell.deal.text = currentBar.deal
cell.distanceToBar.text = String(format: "%.3f mi", currentBar.distance)
// Set the height of the table view cells
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 44.0;
return cell
}
Any ideas?
Use tableView:heightForRowAtIndexPath: function in the UITableViewDelegate.
Using this you can individually set the height of each row separately.
For more information : https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath:
Or you can use auto layout settings like in this article.
http://useyourloaf.com/blog/2014/02/14/table-view-cells-with-varying-row-heights.html