Swift TableView cell doesn't fit into 1 row - swift

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.

Related

Cannot Change Text of Label in TableView

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.

Checking If every row in table view have image hidden or not

Is there any way to check inside a function : func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) If rest of the rows ( instead of the first one ) have image hidden or not.
If images of rows which are greater then indexPath.row - 0 are all hidden or not.
So basically I would like to get the first row ( image ) of the table view hidden when the rest one are hidden as well. The simple check boxes.
This how I'm hiding them :
let row = indexPath.row
if row > 0 {
UIView.animate(withDuration: 0.5, animations: {
let currentCell = self.engineStatusTableView.cellForRow(at: indexPath) as! DropDownViewCell
if !currentCell.checkMark.isHidden {
currentCell.checkMark.isHidden = true
} else {
currentCell.checkMark.isHidden = false
}
})
}
Thanks in advance!
It's generally not a good idea to keep your state inside of cells. Cells are reused and somewhat expensive to create, so you want to let UITableView control the creation and manage their reuse.
But, hopefully, your datasource knows if the checkmark is supposed to be hidden or not and you can ask it without having to create a cell to do that.

Swift - TableView, change font of even rows to bold

I have a tableview and i want to change the font of even rows, Here is my code:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "ProductListTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! ProductListTableViewCell
let product = productList[indexPath.row]
cell.productName.text = product.name
cell.productPrice.text = "\(product.price) manat"
if(indexPath.row % 2 == 0) {
cell.productName.font = UIFont.boldSystemFontOfSize(13.0)
cell.productPrice.font = UIFont.boldSystemFontOfSize(13.0)
}
return cell
}
when i run my code, at the beginning everything works correctly, when i scroll my table view, all new rows appeared on screen becomes bold, both even and old rows. What am i doing wrong?
Remember that a table view reuses cells. That's why you get them from a method named dequeueReusableCellWithIdentifier(_:forIndexPath:).
You set the font to bold when it's an even-numbered row, but you don't set it back to normal when it's an odd-numbered row. If the cell was previously used for an even-numbered row, and is now being used for an odd-numbered row, it still has a bold font.
let weight = (indexPath.row % 2 == 0) ? UIFontWeightBold : UIFontWeightRegular
let font = UIFont.systemFontOfSize(13, weight: weight)
cell.productName.font = font
cell.productPrice.font = font
Your reusable cells are all being set to bold. Add an else to the if row % 2 == 0 to set the cell back to normal font when it is used in an odd row.

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