TableView separator disappear - ios

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.

Related

How can I add a label to a UITableViewCell on touch?

I have a UITableView and I'm displaying UITableViewCells.
When I click on a cell, I want to add a label to that cell, on a new row within the cell.
I'm updating my data structure correctly, I just don't know how to add the label to the subview.
This is where I add the row to my data structure:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
itemdata[indexPath.section].items[indexPath.row - 1].rows.append(row(size: 0))
// need to call below method somehow to update views
}
This is where I think I need to add the label to the cell:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "itemcell") as! itemcell
cell.lblItemName?.text = itemdata[indexPath.section].items[indexPath.row - 1].name
if(itemdata[indexPath.section].items[indexPath.row - 1].rows.count > 0){
// dynamically add new rows to the cell in a loop
// each row is a label on a new line
}
return cell
}
Here is what the cell looks like before click:
After touching, it should look like this:
After touching again, it should look something like this:
Dynamically adding new rows is what I don't know how to do.
If possible, I'd like to be able to visually design a row (with a label, buttons, etc) in a similar way that cells can be prototyped, and then add such a row to the cell. Perhaps by having an empty array of such rows within the cell which can then by increased on touch.
I don't want to do it by putting another UITableView inside the cell with more UITableViewCells inside that because I don't need the view to be scrollable and I don't want the lines between cells.

How to create tableview cells without using prototype cells?

In my tableview, every cell will be different and determined by a JSON response from server. And there will be infinite possibilities. So defining a prototype for each type of cell is not possible.
For example, one cell will have labels and buttons, another cell have images and buttons in different orders.
How to achieve this dynamic structure in tableview cells?
Currently what I am doing is: adding views as subview in cellForRowAtIndexPath but scrolling is very laggy this way.
How to achieve this without affecting performance this much
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! MyCell
for myview in data[indexPath.row].myviews{
cell.addSubview(myview)
}
return cell
}
If you're using a table view then your content is going to scroll vertically, right?
There is a physical limit to the amount of UI that you can put horizontally. Limited by the screen size.
So I'm guessing your UI parts are being laid out vertically in the cell?
So instead of laying out a button, label, image, another button, and a text field vertically in a cell...
Create a cell type called ButtonCell, LabelCell, ImageCell, MultiLineLabelCell, TextFieldCell, etc...
So now, instead of creating one cell with all these elements added. You instead create multiple cells each containing one type of UI. Now you can dequeue your cells in any particular order (driven by your JSON) and won't lose the performance.
The only solution I see is to have empty cell and add/remove subviews as needed. But you should add new subviews to a cell only if you did not add them before.
For example:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! MyCell
if cell.contentView.viewWithTag(1) == nil {
let label = UILabel()
label.tag = 1
cell.contentView.addSubview(label)
}
let label = cell.contentView.viewWithTag(1)
// label config there
return cell
}
Also don't forget to add subviews to cell's contentView not to cell itself.

show Custom Table Separator UIView in tableView

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. .

Remove extra cells/rows whitespace without data appearing on UITableView

I m populating UITableView with data and works fine too.
But my issue is , when the UITableView has less number of rows of data it shows rows with data properly but shows a whitespace .
For example: if the tableview has 1 row to display it shows that row then under that there are whitespaces like empty rows.
I tried
tableView.tableFooterView = UIView()
and
tableView.tableFooterView = UIView(frame: CGRectZero)
Screenshot:
But both just removed the Cell borders not the extra whitespace appearing.
Please suggest. Thanks in advance.
Edit :
Select Button is not Part of the Cell. Just trying to Click on Select Button and then the UITableView Displays under it. ( Like a DropDown ).
Also , using default Tableview Cell not a Custom TableView Cell
Code :
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cityList.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(cityCellIdentifier, forIndexPath: indexPath) as UITableViewCell
let row = indexPath.row
cell.textLabel?.text = self.cityList[row].cityEn
print("CityName" + self.cityList[row].cityEn )
return cell
}
StoryBoard ScreenShot
If you don't want the empty cells set the TableView style from Plain to Grouped.
Adding to #choli answer You can do 2 things,
adjust the height of the table view based on the rows,
Resize UITableView's height to be as high as it needs to fit only total content size
Simpler solution would be to clear table background color
tableView.tableFooterView = UIView(frame: CGRectZero)
tableView.backgroundColor = UIColor.clearColor()
Sorry, I would comment, but not enough reputation:
This is the tableView size, whenever the full tableview is not filled with rows, it shows some extra rows that are empty. If you don't want this, then you have different possibilities:
you could make the size of the tableView smaller - e.g. the number of cells * cellHeight
you can change the appearance of the tableView, so it will be clearColor instead of white, so the placeholder cells will be invisible.

Swift UITableView didSelectRowAtIndexPath bug

I have a UITableView with the subtitles hidden but set up where when someone selects a cell it shows that cell's subtitle. This works fine except that after tapping any cell to reveal its subtitle if you scroll down you will find that every 12 cells have their subtitle unhidden (as well as the one it was supposed to reveal). Here is the code I'm using in didSelectRowAtIndexPath:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
for cell in tableView.visibleCells() {
cell.detailTextLabel??.hidden = true
}
var cell = tableView.cellForRowAtIndexPath(indexPath)
cell?.detailTextLabel?.hidden = false
}
I'm sure this is related to ".visibleCells()" since every 12 cells is about the height of my visible table on my iPhone 6 Plus. When I run it on a 4s in the simulator it's about every 8 cells. But I'm not sure how else to do it besides 'visibleCells'? But it's strange because it's the whole table - all the way down, every 12 cells is showing its subtitle...
thanks for any help
UITableView reuses its cells. So the cell for row a row you clicked on (unhidden the subtitle) may be used for row another row.
The solution is to define prepareForReuse() method in the UITableViewCell subclass (or make the subclass if you do not have one) and hide the subtitle again there.
Add that dataSource's method to your controller. Should work fine.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) {
var identifier = "cellIdentifier"
var cell = tableView. dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath)
cell.detailTextLabel?.hidden = true
return cell
}

Resources