show Custom Table Separator UIView in tableView - ios

I have a dynamic Prototype TableView. As I want to show a line in the cell so I drag a uiview and placed in the cell. The problem is when I compile the app in mobile the line doesn't show up in the cell. I am doing this in my code to fill the cell values
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell",
forIndexPath: indexPath) as! RequestTableViewCell
cell.userNameLabel.text = firstName.uppercaseString + " "
+ lastName.uppercaseString
cell.contentView.addSubview(cell.tableSeparatorUIView)
}
I think I am doing something wrong here
cell.contentView.addSubview(cell.tableSeparatorUIView)

I have done the sample example. Use auto layout. Go throght sample example Please go through the link. it will helps you.

Do this :-
Select your TableView do separator None in attribute inspector. Then add UIView at bottom with height 1px and leading, trailing,bottom to tableView. .

Related

Swift: Images placed in a tableviewcell are not visible

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "approve_cell");
let event = self.events[indexPath.row];
cell.textLabel?.text = event.name;
return cell;
}
I placed 2 images and a label in tableviewcell. When i run the app, images are not visible. What could be the reason?
Check that you are not using the basic style of the UITableViewCell because if you are then that style only includes labels.. no image views. Change it to subtitle or one of the details.
For reference, see: https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html
You probably forgot to set AutoLayoutConstraints of your image view. You could either add constraints to that image view through storyboard or through code. The other method is setting the frame of your image view in -(void)viewDidLayoutSubviews, because when you're using auto-layout, the layout of those views will be undefined if its constraints doesn't fulfill system's computing need. So you should either set right constraints or give a specific frame to those undefined views after all of the others has-constraints-views are arranged.
You creat cell in storyboard but you never use this cell created from storyboard. instead you manually creat it by UITableViewCell's designated initializer without override layout method of it.
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "approve_cell", for: indexPath)
let event = self.events[indexPath.row];
cell.textLabel?.text = event.name;
return cell;
}
You need to set either Autolayout or used Autoresizing. For autolayout just pinned the proper leading, trailing, top and bottom constraint.

TableView separator disappear

When I select rows from top to bottom, separator appears. However when I scroll the rows, the separator removed.
When I select rows from bottom to top, separator doesn't appear.
How can I keep the separator always existed?
I have searched for a while and seems there is no solution yet.
When you simply add a UITableView, it shows the separator like these. The separator remains there when you select, reload or perform any other function.
Other can be check if the separator is selected to default or none.
At one time you can select single row only. The selection color being same as the separator. It appears there is no separator. Plus when you have multi select. Its default functionality that it appears that there is no separator.
// create a cell for each table view row
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// create a new cell if needed or reuse an old one
let cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier(cellReuseIdentifier) as UITableViewCell!
// set the text from the data model
cell.textLabel?.text = mainArr[indexPath.row]
//Main item here is set selection style none.
cell.selectionStyle = UITableViewCellSelectionStyle.None
return cell
}
and when you select a row, add a new UIView or ImageView to the cell which is same width as cell view but height should be cell.height-1
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath)
{
let cell = self.tableView.cellForRowAtIndexPath(indexPath)
var customView = UIView(frame: CGRectMake(0, 0, (cell?.frame.width)!, 43))
customView.backgroundColor = .redColor()
customView.alpha=0.3
cell!.addSubview(customView)
}
In this manage an algorithm, that way you can come to know if the row has to be selected or deselected and that way add the view or remove the view from the cell.

Comment controller with table view section header and footer

How can I achieve this screen with UITableViewCell and UITableViewController. With table section and header. Some ideas to achieve this?? Thanks!
What have you tried so far?
Your question seems a little broad.
You will need a set of custom UITableViewCell Subclasses, which you design in nibs.
To make the cells seem apart from each other, resize the content size of the Cells, and make the cell background another color.
Create a Segmented Control and add it to the Tableviews HeaderView.
For the FooterView it seems like this is some kind of subclassed Tabbar.
Easiest way to customise it in such a way, would be to create a View, and add buttons to it. Add this View as Subview to your TableViewController.
Have 2 UITableViewCell's one for each type i.e. 1 for showing the image and text and another for showing just the text.
Then in the cellForRowAt delegate method determine which to type to use based off the object you are data binding it to.
Example:
public final func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let customObject = customObjects[indexPath.section]
switch customObject.type {
case .imageAndText:
if let cell = tableView.dequeueReusableCell(withIdentifier: ImageAndTextCell.identifier, for: indexPath) as? ImageAndTextCell {
cell.customObject = customObject
return cell
}
case .text:
if let cell = tableView.dequeueReusableCell(withIdentifier: TextCell.identifier, for: indexPath) as? TextCell {
cell.customObject = customObject
return cell
}
}
return UITableViewCell()
}

Change cell height by the content of the textView inside the cell

I have a Table View called todoTableView with cells that created by the user.
Each cell has Text View.
I want to change the height of the cell by the content of the Text View.
This is what I tried:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: TableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath:indexPath) as! TableViewCell
cell.bounds.size.height = cell.textView.bounds.size.height
return cell
}
Bound Your textview with cell from all sides using marginal constraints.(Leading, Trailing, Top and Bottom constraints)
Disable textView Scrolling
In viewDidLoad() add the following.
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
This will make your cell size according to your textview content size.
Have a look at result :
You don't need to write heightForRowAtIndexPath.
Irfan's answer didn't work for me.
It works after I go to Size Inspector and check "Automatic" for "Row Height".
You should also implement heightForRowAtIndexPath:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return // Calculate your custom row height here.
}

Align Button to the right in UITableViewCell

I want to have a button in my custom cell which is right aligned. Doing it with auto layout and setting a horizontal space from the right and centering it vertical does not show the Button in my cell because it is too far at the right.
In the picture you can see my cell. But it is not shown like this in the actual app.
Edit: I tried your answer but this does not seem to work. My TableView looks kind of strange in the Emulator:
Is it possible that there is a problem in my controller?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("eventCell", forIndexPath: indexPath) as EventTableViewCell
let event = items[indexPath.row]
cell.contentView.autoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth
return cell
}
Do the following for that Button:
Set Width and Height constraints
set Vertical center constraints to the cell view
Set horizontal/trailing(may be 10px) space to the right side of the cell view
Sry I am dumb.
Everything was fine with my Cell layout. I was so focused on the cell that I forgot to add constraints to the TableView. Adding these constraints fixed the problem.

Resources