UITableView selected row center like UIPickerView in Swift - ios

What I want to do: Make UITableView selected row animate like a UIPickerView on selection.
I have a UITableViewController in which I am displaying UIImages in rows from Parse backend. The cell size is almost half of the users screen. I have used tapGestureRecognizer "Single Tap" to increase/decrease the cell height to show selected cell images and "Double Tap" to segue data to next view. If anyone is interested to know, here is the Link to it.
Now the problem is, when I tap on any specific cell image the cell increases in height as needed but if the selected cell is not centered when tapped (i.e: I have bottom half of the cell Row-1 and top half of cell Row-2 on users screen) the cell which has its height increased is partially visible.
So I was wondering if there is way with which we can have the selected cell automatically centered on tap? Similar to UIPickerView?

Here is a solution. Use the didSelectRowAtIndexPath delegate method and then scroll to the provided indexpath.row of the tableview accordingly.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
yourTableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top , animated: true)
}

Related

Expandable view within tableview

I currently have a table view controller which consist of not only the cells but also a UIView. Now, within that UIView there's a label which might have more than 1 line of text with a See More button. When I pressed that button, the button itself will disappear and the text.numberOfLines is set to 0 so the View should expand to show all text. Which doesn't seems to work in my case, The button does disappear though and the text just continues to the edge of the screen, truncated instead of extending down.
But when this whole UIView is outside the table view controller the functions above work just as expected, but not after I've moved them to within the table View right above the prototype cells. Any Ideas?
When you are setting up the tableviewcell, you have to specify a fixed height for the row with the delegate function
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) {
if expandedArray[indexPath.row] {
return 60
} else {
return UITableViewAutomaticDimension
}
}
Then keep an array of bools, to specify if the cell is expanded or not. Primarily set the bools to false indicating "not expanded".
Then when the use presses the "see more" button, make the bool in the array with the right index true, indicating that the cell is expanded. and call the below code updating the cell.
tableView.beginUpdates()
tableView.reloadRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
tableView.endUpdates()
That should do the trick.
P.S: Don't forget to properly add constraints inside tableview cell.
Use UITableViewAutomaticDimension and reload your cell on click of see more button.

Change color of the drag/move Knob in reorderable tableView

I have a tableView in editing-Mode for reordering Cells. I want to customize the color of the Knob for dragging the Cell. Actually, it is light grey. To understand what the picture is showing: I have a Cell with a height of 70, and a uiview with a height of 60 in it because I want more Space between the Cells. That's why you see the grey parts of the knob (yellow arrows). These I want to have in the same Background color as the tableView itself.
Maybe someone knows how I achieve this?
So far, I've no hint to how set its background color. However there is others options you want to explore to have this space between cells :
Add extra "empty" cells between cells
Add sections for each cells.
I'd go with the second option if I could afford it.
PS : Maybe you can a view under your tableview with the desired color.
Ok... Got it. Using the Code from here Change Reorder Control's color in table view cell i modified it in this way:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
for subViewA in cell.subviews {
if (subViewA.classForCoder.description() == "UITableViewCellReorderControl") {
subViewA.backgroundColor = UIColor(red:0.89, green:0.00, blue:0.45, alpha:1.0)
break;
}
}
}
Now it looks like i want:
Nice looking

tableviewcell repeating uibuttons

I am having a little trouble with buttons on a tableview.
I have a tableViewCell that I customised with 3 buttons. I set the buttons to hidden in interface builder and when the table loads the buttons are hidden as expected.
I then set the hidden property of the tableview to false when didSelectRow is called and hidden.true when didDeselectRow is called. This works fine as well. The problem is the buttons that are set to visible in the didSelectRow are also visible every seven cells down. they keep repeating themselves.
Below is the code that shows the buttons
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! ContactsViewCell
print("Table selected")
cell.insertEmailButton.hidden = false
cell.insertPhoneButton.hidden = false
cell.insertAllButton.hidden = false
cell.contactTextLabel.alpha = 0.2
cell.contactDetailTextLabel.alpha = 0.2
}
And this hides them when the tableViewCell is deselected
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! ContactsViewCell
cell.insertEmailButton.hidden = true
cell.insertPhoneButton.hidden = true
cell.insertAllButton.hidden = true
cell.contactTextLabel.alpha = 1.0
cell.contactDetailTextLabel.alpha = 1.0
}
I did some research and I learnt it might be the row with the buttons.hidden set to false are being reused by the tableview. But I understand from documentation that the cell being reused is from cellForRowAtIndexPath and not the cell at didSelectRow which is where I am setting the button.hidden to false.
I also tried using the cell.isSelected property in an if else statement in the cellForRowAtIndexPath to hide and show the buttons but this does not show the buttons at all.
Thanks in advance for your help
The tableview reuses the view of the cell when the table is scrolled, to save memory. So, for example, when you set the button to visible (inside didSelectRow) and then scroll down the table, the tableview will take the cells that go out of the visible screen at the top and will reuse them at the bottom, to save the overhead of creating new cells, improving performance.
That is why, your previous properties on the cells are repeating.
To get the desired hidden button on scrolled cells, I recommend setting button.hidden to true/false in
tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
This will set the button to hidden whenever a new row is scrolled into the visible view area.
Hope this helps.
I solved the recurring buttons by hiding them when I check if the cell is deselected in cellForRowAtIndexPath. This also means any cell I select will lose its selected status and buttons will disappear when it leaves the view.
I can live with that.
if cell.selected == false{
cell.emailButton.hidden = true
cell.phoneButton.hidden = true
cell.allButton.hidden = true
}
UITableView reuses its cell to improve performance. So, you can not do the way you are trying. What we have to do is, like other tableview cell info e.g. title, description, thumb image etc we also need to save the state for buttons in the array. When you want to hide a button for the cell take object at index from the array and change the button state for the button and reload that table view cell. Still if you face problem or feel difficult to understand, please feel free to ask.

Button in (static) cell disappears when clicking on the cell itself instead of the button

i'm having a weird issue with the code on my (static) cells. In multiple cells I have a UIButton. When I click on it, it's all fine, but when I click on the area next to the button (still in the cell itself) it turns grey (color when selected cell) + the button also disappears!
I tried to change the background of the cell to white when pressed with the following code:
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let myBackView=UIView(frame:cell.frame)
myBackView.backgroundColor = UIColor.clearColor()
cell.selectedBackgroundView = myBackView
}
but the issue is still there.
This is when the static cell isn't pushed
This is when I pressed the cell (NOT the button within the cell)
This is a common issue when selecting UITableViewCells, see Why do all backgrounds disappear on UITableViewCell select?
If you don't intend the cell to be selected, implement this delegate method:
- (NSIndexPath * _Nullable)tableView:(UITableView * _Nonnull)tableView willSelectRowAtIndexPath:(NSIndexPath * _Nonnull)indexPath
and return nil when indexPath is the one of your cell. This will disable selection for this cell.
If none of your cells is intended to be selected, it's even easier, just set your UITableView's allowsSelection to NO (this can also be done in Interface Builder).

iOS why is UIView displayed correctly only after reloading UITableView

I am trying to insert a lot of views into UITableViewCell.
I add view and calculate required constants. And I can't resolve one problem. Inner labels can be bigger than one line and when UITableView only initialized it didn't displays correctly: some cropped labels, wrong height and etc.
But when I reload tableview or scroll to bottom elements and scroll back to incorrect one cell all views displays correctly.
EDITED
Example: I have UITableViewCell. Every cell contains number of attributes(separate views). Each attribute view has title(gray text) and attribute value(single view again). Each attribute value view may have different height. And I assume height doesn't calculates right at the first time.
When I build UITableViewCell firstly I build all of attribute views and after that I add each view to Cell and calculate constants.
I have solve problem.
When I got UITableViewCell it frame has width 325 but tableView frame width is 375. Because of this inner views were build incorrect way. Now I just set contentView of Cell to tableView frame.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let identifier = "ListCell"
var cell = tableView.dequeueReusableCellWithIdentifier(identifier) as? ListViewCell
cell!.contentView.frame = tableView.frame
// additional setup
return cell!
}

Resources