How to transform Delete option in Tableview cell - ios

I've transformed the tableview and tableview cell to fill the cell from bottom to top using How to populate UITableView from the bottom upwards? post.
But I want to transform the swipe to Delete option as well.
Please refer the image for more detail.
You can see the Delete option is not transform as the tableview.
Any helpful answer will be very appreciated.

First reverse UITableView in viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
tableView.transform = CGAffineTransform(scaleX: 1, y: -1)
}
Then reverse the cell in cellForRowAt.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableViewCell", for: indexPath) as? MyTableViewCell else { fatalError() }
cell.transform = CGAffineTransform(scaleX: 1, y: -1)
cell.contentView.transform = CGAffineTransform(scaleX: 1, y: -1)
return cell
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .default, title: nil) { (action, indexPath) in
// delete item at indexPath
}
let label = UILabel(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 75, height: tableView.rectForRow(at: indexPath).height)))
label.numberOfLines = 0
label.textAlignment = .center
label.textColor = UIColor.white
label.backgroundColor = UIColor.red
label.text = "Delete"
UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0.0)
label.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
if let cgImage = image?.cgImage {
let rotada3 = UIImage(cgImage: cgImage, scale: image!.scale, orientation: .downMirrored)
delete.backgroundColor = UIColor(patternImage: rotada3)
}
return [delete]
}
Here on editActionsForRowAt I did a work around to print image with mirrored text

Transform tableview
tableView.transform = CGAffineTransform(rotationAngle: -(.pi))
Then within cell
cell.transform = CGAffineTransform(rotationAngle: .pi)

Related

getting the height of the custom cell in UITableViewCell

I'm creating a bubble chat like application. I created custom functions for the receiving and sending bubble chats.
After creating them, I've added them into the cells that I've created using the UITableView.
However, I am not able to set the height of the cell, depending on the height of the custom cell itself.
For example, some text passed in could be longer than the one before.
Thus I can't seem to set each cell according to the custom bubble chat height.
Please help!
This are my tableview functions
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrMsg.count
}
// second
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
// lastly created
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for:indexPath)
if arrMsg[indexPath.row]["type"] as! String == "outgoing" {
cell.contentView.addSubview(createOutGoingBubble(msg: arrMsg[indexPath.row]["message"] as! String))
}
if arrMsg[indexPath.row]["type"] as! String == "incoming" {
cell.contentView.addSubview(createIncomingBubble(mymsg: arrMsg[indexPath.row]["message"] as! String))
}
return cell
}
This is how it looks currently
How do I set the height of the cell according to the custom bubble created inside the heightforrowat function?
Probably using the line to find the height and return it:
arrMsg[indexPath.row]["message"] as! String == "outgoing"
**This is my function to create the custom bubble chats to be added into the tableviewcell
func createOutGoingBubble(msg:String) -> UIView {
let subV = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))
let label = UILabel()
label.numberOfLines = 0
label.font = UIFont.systemFont(ofSize: 18)
label.textColor = .white
label.text = msg
let constraintRect = CGSize(width: 0.66 * view.frame.width,
height: .greatestFiniteMagnitude)
let boundingBox = msg.boundingRect(with: constraintRect,
options: .usesLineFragmentOrigin,
attributes: [.font: label.font],
context: nil)
label.frame.size = CGSize(width: ceil(boundingBox.width),
height: ceil(boundingBox.height))
let bubbleSize = CGSize(width: label.frame.width + 28,
height: label.frame.height + 20)
let outgoingMessageView = UIImageView(frame:
CGRect(x: view.frame.width - bubbleSize.width - 20,
y: bubbleSize.height - 25,
width: bubbleSize.width,
height: bubbleSize.height))
let bubbleImage = UIImage(named: "outgoing")?
.resizableImage(withCapInsets: UIEdgeInsets(top: 17, left: 21, bottom: 17, right: 21),
resizingMode: .stretch)
.withRenderingMode(UIImage.RenderingMode.alwaysTemplate)
outgoingMessageView.image = bubbleImage
outgoingMessageView.tintColor = UIColor(red: 0.40 , green: 0.71, blue: 1.00, alpha: 1)
subV.addSubview(outgoingMessageView)
label.center = outgoingMessageView.center
subV.addSubview(label)
return subV
}
Thanks in advance!
You can use Autolayout for the same and get free from all the coding part. Just set the constraint of the inner view in association with the cell and then right the following code.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:ChatSenderTableViewCell = (tableView.dequeueReusableCell(withIdentifier: "ChatSenderTableViewCell", for: indexPath) as? ChatSenderTableViewCell)!
cell.LabelChatMessage.text = "Your Message"
cell.LabelChatMessage.numberOfLines = 0
cell.LabelChatMessage.sizeToFit()
return cell
}
I am using a custom cell name ChatSenderTableViewCell to create a bubble like effect containing a label for messages send or receive.
Use label inside of view and use margin from top and bottom according to your desire. Also, you need to make 0 number of lines in UIlabel. you can do it by using label.numberoflines = 0.
Also, you need to add the following code to change the height of the row according to the text inside it.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
You can try this:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
guard let cell = tableView.cellForRow(at: indexPath) else { return 0 }
return cell.intrinsicContentSize.height
}
The intrinsicContentSize is the natural size for the receiving view, considering only properties of the view itself.

Assign an upper bound for the height of a tableview?

I have a tableview whose height is dynamically adjusted. The height is adjusted so that the tableview only includes a certain number of rows, and no empty rows. So, if there are only 2 rows then the height is smaller, and if there are 4 rows then the height is bigger. However, I do not want its height to go past a certain point. How can I allow the height to be dynamically adjusted, while not letting the height ever reach past a certain point?
This is my code:
#IBOutlet var tableView: UITableView!
#IBOutlet var tableViewHeightConstraint: NSLayoutConstraint!
var items: [String] = ["Swift", "Is", "So", "Amazing"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "Cell") as! UITableViewCell
// Make sure the table view cell separator spans the whole width
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
override func viewWillAppear(_ animated: Bool) {
// Adjust the height of the tableview
tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.frame.size.width, height: tableView.contentSize.height)
// Add a border to the tableView
tableView.layer.borderWidth = 1
tableView.layer.borderColor = UIColor.black.cgColor
print("We ran ViewWillAppear")
}
// This function is used for adjusting the height of the tableview
override func viewDidLayoutSubviews(){
tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.frame.size.width, height: tableView.contentSize.height)
tableView.reloadData()
}
// Allow cell deletion in tableview
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
// delete item at indexPath
self.items.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
self.tableViewHeightConstraint.constant = self.tableView.contentSize.height - 44
print(self.items)
print("Number of rows: \(tableView.numberOfRows(inSection: 0))")
}
delete.backgroundColor = UIColor.blue
return [delete]
}
In viewWillAppear , you can try
let limit:CGFloat = 900.0
if tableView.contentSize.height < limit {
tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.frame.size.width, height: tableView.contentSize.height)
}
else {
tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.frame.size.width, height: limit )
}
Or set value to
tableViewHeightConstraint.constant = ( tableView.contentSize.height < limit ) ? tableView.contentSize.height : limit
self.view.layoutIfNeeded()

Unexpected behavior with nested collection views with constraints (Swift 4)

I have a cell in a tableview that contains a horizontal paging collection view.
Inside each page of this collection view there is a vertical collection view.
In order to avoid a "scroll-in-scroll" problem, I disabled vertical scrolling in the vertical collection views.
The vertical collection view's cell count is not static and can be any number.
So this creates a problem: the collection view (you'll have to ask me for clarification about which one) won't fit properly into the table view's cell anymore.
To solve this, I changed the table view's cell height by changing the constraints in the storyboard... but the problem remains.
Here is an image to help illustrate the problem:
and here is my code:
class myTBViewController: UITableViewController , UICollectionViewDelegate , UICollectionViewDataSource , UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView.tag == 10 {
return 2
} else {
return 30
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView.tag == 10 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mainPages", for: indexPath) as! mainPages
cell.backgroundColor = UIColor.yellow
cell.listCV.clipsToBounds = true
return cell
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "showAdvCell", for: indexPath) as! showAdvCell
cell.backgroundColor = UIColor.red
return cell
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
if collectionView.tag == 10 {
let collectionViewHeight = collectionView.frame.height
let itemsHeight = collectionView.contentSize.height
let topInset = ( collectionViewHeight - itemsHeight ) / 4
return UIEdgeInsetsMake(topInset ,0, 0 , 0)
} else {
let collectionViewHeight = collectionView.frame.height
let itemsHeight = CGFloat(80)
let topInset = ( collectionViewHeight - itemsHeight ) / 4
return UIEdgeInsetsMake(topInset ,0, 0 , 0)
}
}
var headerSegment = UISegmentedControl()
override func viewDidLoad() {
super.viewDidLoad()
headerSegment.frame = CGRect(x: 0, y: 200, width: UIScreen.main.bounds.width, height: 30)
let items = ["فروشگاه ها", "دیوار"]
headerSegment = UISegmentedControl(items: items)
headerSegment.selectedSegmentIndex = 0
headerSegment.addTarget(self, action: #selector(selectPage), for: UIControlEvents.valueChanged)
let frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 200)
let headerImageView = UIImageView(frame: frame)
let image: UIImage = UIImage(named: "1")!
headerImageView.image = image
let frame3 = CGRect(x: 0, y: 0, width: 80, height: 80)
let headerImageView2 = UIImageView(frame: frame3)
let image2: UIImage = UIImage(named: "1")!
headerImageView2.image = image2
headerSegment.addSubview(headerImageView2)
headerSegment.bringSubview(toFront: headerImageView2)
headerImageView2.center = CGPoint(x: UIScreen.main.bounds.width / 2, y: 200)
headerSegment.frame = CGRect(x: 0, y: 200, width: UIScreen.main.bounds.width + 10 , height: 40)
headerSegment.center = CGPoint(x: UIScreen.main.bounds.width / 2, y: 200)
let mainView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 230))
let headerSegmentView = UIView(frame : CGRect(x: 0, y: 200, width: UIScreen.main.bounds.width, height: 40))
headerSegmentView.center = headerSegment.center
headerSegmentView.backgroundColor = UIColor.white
mainView.addSubview(headerImageView)
mainView.bringSubview(toFront: headerImageView)
mainView.addSubview(headerSegmentView)
mainView.bringSubview(toFront: headerSegmentView)
mainView.addSubview(headerSegment)
mainView.bringSubview(toFront: headerSegment)
mainView.addSubview(headerImageView2)
mainView.bringSubview(toFront: headerImageView2)
self.tableView.tableHeaderView = mainView
headerImageView2.layer.borderWidth = 2
headerImageView2.layer.masksToBounds = false
headerImageView2.layer.borderColor = UIColor.init(white: 255/255, alpha: 0.3).cgColor
headerImageView2.layer.cornerRadius = headerImageView2.frame.height/2
headerImageView2.clipsToBounds = true
}
#objc func selectPage() {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "myTBViewCell") as! myTBViewCell
switch headerSegment.selectedSegmentIndex {
case 0:
print("0")
cell.myTBCollectionView.scrollToItem(at:IndexPath(item: 0, section: 0), at: .top, animated: false)
case 1:
print("1")
cell.myTBCollectionView.scrollToItem(at:IndexPath(item: 1, section: 0), at: .top, animated: false)
default:
print("0")
cell.myTBCollectionView.scrollToItem(at:IndexPath(item: 0, section: 0), at: .top, animated: false)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 1
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
tableView.estimatedRowHeight = 200
tableView.rowHeight = UITableViewAutomaticDimension
return 600
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "myTBViewCell", for: indexPath) as! myTBViewCell
return cell
}
}
I have a similar Problem , finally, I have solved it by creating a height Constraint of CollectionView.
than setting CollectionView height Constraint based on CollectionView ContentSize.
self.collectionViewHeight.constant = self.recentCollectionView.collectionViewLayout.collectionViewContentSize.height
the alternative approach is to override systemLayoutSizeFitting method of tableViewCell
ex:
override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
super.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: horizontalFittingPriority, verticalFittingPriority: verticalFittingPriority)
self.tagCollectionView.layoutIfNeeded()
return self.tagCollectionView.collectionViewLayout.collectionViewContentSize
}
in addition you don't need to return fixed 600 height in your table view delegate heightForRowAtIndexPath

How to acces a view from tableViewCell to TableViewController

I have a UITableViewController and UITableViewCell. Now I am try to access a view from a UITableViewCell to UITableViewController by didSelectRowAt function. But I could not do it.
TableViewController
import Foundation
import UIKit
class FlipViewCon: UIViewController, UITableViewDelegate, UITableViewDataSource{
let flipCellId = "flipCellid"
let flipTableView: UITableView = {
let tableView = UITableView()
tableView.backgroundColor = .green
return tableView
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .gray
flipTableView.delegate = self
flipTableView.dataSource = self
flipTableView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
self.view.addSubview(flipTableView)
flipTableView.register(FlipTableViewCell.self, forCellReuseIdentifier: flipCellId)
}
let countryArray = ["bangladesh", "nepal", "china", "malaysia", "thai land", "japan", "England", "canada"]
let cityArray = ["Dhake","Kathmandu", "Beijing", "Kuala Lumpur", "Bangkok", "tokeyo", "London", "Torento"]
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return countryArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: flipCellId, for: indexPath) as! FlipTableViewCell
//cell.textLabel?.text = countryArray[indexPath.row]
cell.zeroLabel.text = countryArray[indexPath.row]
cell.oneLabel.text = cityArray[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
UIView.transition(with: FlipTableViewCell.zeroView, duration: 0.5, options: .transitionFlipFromLeft, animations: nil, completion: nil)
FlipTableViewCell.zeroView.isHidden = true
FlipTableViewCell.oneView.isHidden = false
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
UIView.transition(with: FlipTableViewCell.oneView, duration: 0.5, options: .transitionFlipFromLeft, animations: nil, completion: nil)
FlipTableViewCell.zeroView.isHidden = false
FlipTableViewCell.oneView.isHidden = true
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return self.flipTableView.frame.width / 4
}
}
TableViewCell
import UIKit
class FlipTableViewCell: UITableViewCell{
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
static let zeroView = flipView(myColor: .yellow)
static let oneView = flipView(myColor: .green)
static func flipView(myColor: UIColor) -> UIView {
let view = UIView()
view.backgroundColor = myColor
return view
}
let zeroLabel: UILabel = {
let lb = UILabel()
lb.text = "Zero 0"
return lb
}()
let oneLabel: UILabel = {
let lb = UILabel()
lb.text = "one 1"
return lb
}()
func setupView(){
FlipTableViewCell.zeroView.frame = CGRect(x: 0, y: 0, width: frame.width, height:frame.height)
FlipTableViewCell.oneView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
self.addSubview(FlipTableViewCell.zeroView)
self.addSubview(FlipTableViewCell.oneView)
zeroLabel.frame = CGRect(x: 20, y: 0, width: self.frame.width - 40, height: 50)
oneLabel.frame = CGRect(x: 20, y:0, width: self.frame.width - 40, height: 50)
addSubview(zeroLabel)
addSubview(oneLabel)
}
}
Try this:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) as? FlipTableViewCell {
UIView.transition(with: cell.zeroView, duration: 0.5, options: .transitionFlipFromLeft, animations: nil, completion: nil)
cell.zeroView.isHidden = true
cell.oneView.isHidden = false
}
}
Edit
I have update your FlipTableViewCell
it looks like this
class FlipTableViewCell: UITableViewCell{
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
var zeroView : UIView!
var oneView : UIView!
func flipView(myColor: UIColor) -> UIView {
let view = UIView()
view.backgroundColor = myColor
return view
}
let zeroLabel: UILabel = {
let lb = UILabel()
lb.text = "Zero 0"
return lb
}()
let oneLabel: UILabel = {
let lb = UILabel()
lb.text = "one 1"
return lb
}()
func setupView(){
zeroView = flipView(myColor: .yellow)
oneView = flipView(myColor: .green)
zeroView.frame = CGRect(x: 0, y: 0, width: frame.width, height:frame.height)
oneView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
self.addSubview(zeroView)
self.addSubview(oneView)
zeroLabel.frame = CGRect(x: 20, y: 0, width: self.frame.width - 40, height: 50)
oneLabel.frame = CGRect(x: 20, y:0, width: self.frame.width - 40, height: 50)
addSubview(zeroLabel)
addSubview(oneLabel)
}
}
And Change some code in FlipViewCon
class FlipViewCon: UIViewController, UITableViewDelegate, UITableViewDataSource{
let flipCellId = "flipCellid"
let flipTableView: UITableView = {
let tableView = UITableView()
tableView.backgroundColor = .green
return tableView
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .gray
flipTableView.delegate = self
flipTableView.dataSource = self
flipTableView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
self.view.addSubview(flipTableView)
flipTableView.register(FlipTableViewCell.self, forCellReuseIdentifier: flipCellId)
}
let countryArray = ["bangladesh", "nepal", "china", "malaysia", "thai land", "japan", "England", "canada"]
let cityArray = ["Dhake","Kathmandu", "Beijing", "Kuala Lumpur", "Bangkok", "tokeyo", "London", "Torento"]
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return countryArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: flipCellId, for: indexPath) as! FlipTableViewCell
//cell.textLabel?.text = countryArray[indexPath.row]
cell.zeroLabel.text = countryArray[indexPath.row]
cell.oneLabel.text = cityArray[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) as? FlipTableViewCell {
UIView.transition(with: cell.zeroView, duration: 0.5, options: .transitionFlipFromLeft, animations: nil, completion: nil)
cell.zeroView.isHidden = true
cell.oneView.isHidden = false
}
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) as? FlipTableViewCell {
UIView.transition(with: cell.zeroView, duration: 0.5, options: .transitionFlipFromLeft, animations: nil, completion: nil)
cell.zeroView.isHidden = false
cell.oneView.isHidden = true
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return self.flipTableView.frame.width / 4
}
}
Edit2
Replace this method in above solution
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) as? FlipTableViewCell {
UIView.transition(with: cell.oneView, duration: 0.5, options: .transitionFlipFromLeft, animations: nil, completion: nil)
cell.zeroView.isHidden = false
cell.oneView.isHidden = true
}
}
Hope it will work for you
Do like this
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! FlipTableViewCell
// TODO:
}
Just like all the other answers on this post, use the cellForRow(at: IndexPath) method on UITableView, to get a reference to the cells in the table view.
This method returns a UITableViewCell, if the cell is loaded (visible) and nil if the indexPath is not correct or the cell is not loaded.
The code should look like:
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
// Do something with cell.
}
TableViewController
import Foundation
import UIKit
class FlipViewCon: UIViewController, UITableViewDelegate, UITableViewDataSource{
let flipCellId = "flipCellid"
let flipTableView: UITableView = {
let tableView = UITableView()
tableView.backgroundColor = .green
tableView.allowsMultipleSelection = true
return tableView
}()
let countryArray = ["India","bangladesh", "nepal", "china", "malaysia", "thai land", "japan", "England", "canada"]
let cityArray = ["Delhi","Dhake","Kathmandu", "Beijing", "Kuala Lumpur", "Bangkok", "tokeyo", "London", "Torento"]
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .gray
flipTableView.delegate = self
flipTableView.dataSource = self
flipTableView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
self.view.addSubview(flipTableView)
self.resetFlag()
flipTableView.register(FlipTableViewCell.self, forCellReuseIdentifier: flipCellId)
}
var flagArray : [String] = []
func resetFlag() {
flagArray.removeAll(keepingCapacity: true)
for _ in 0 ..< self.countryArray.count {
self.flagArray.append("0")
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return countryArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: flipCellId, for: indexPath) as! FlipTableViewCell
cell.selectionStyle = .none
if flagArray[indexPath.row] == "0" {
cell.zeroView.isHidden = false
cell.oneView.isHidden = true
}else{
cell.zeroView.isHidden = true
cell.oneView.isHidden = false
}
cell.zeroLabel.text = countryArray[indexPath.row]
cell.oneLabel.text = cityArray[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) as? FlipTableViewCell {
self.flagArray.remove(at: indexPath.row)
self.flagArray.insert("1", at: indexPath.row)
UIView.transition(with: cell.zeroView, duration: 0.5, options: .transitionFlipFromLeft, animations: nil, completion: { (complete) in
cell.zeroView.isHidden = true
cell.oneView.isHidden = false
})
}
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) as? FlipTableViewCell {
self.flagArray.remove(at: indexPath.row)
self.flagArray.insert("0", at: indexPath.row)
UIView.transition(with: cell.oneView, duration: 0.5, options: .transitionFlipFromLeft, animations: nil, completion: { (complete) in
cell.zeroView.isHidden = false
cell.oneView.isHidden = true
})
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return self.flipTableView.frame.width / 4
}
}
TableViewCell
import UIKit
class FlipTableViewCell: UITableViewCell{
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let zeroView = flipView(myColor: .yellow)
let oneView = flipView(myColor: .green)
static func flipView(myColor: UIColor) -> UIView {
let view = UIView()
view.backgroundColor = myColor
return view
}
let zeroLabel: UILabel = {
let lb = UILabel()
lb.text = "Zero 0"
return lb
}()
let oneLabel: UILabel = {
let lb = UILabel()
lb.text = "one 1"
return lb
}()
func setupView(){
zeroView.frame = CGRect(x: 0, y: 0, width: frame.width, height:frame.width/4)
oneView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.width/4)
self.addSubview(oneView)
self.addSubview(zeroView)
zeroLabel.frame = CGRect(x: 20, y: (frame.width/4)/2 - 25, width: self.frame.width - 40, height: 50)
oneLabel.frame = CGRect(x: 20, y:(frame.width/4)/2 - 25, width: self.frame.width - 40, height: 50)
zeroView.addSubview(zeroLabel)
oneView.addSubview(oneLabel)
}
}

UITableViewCell - selected background size based on content

I have this code to get table cell
func getCell(_ tableView: UITableView) -> UITableViewCell? {
var cell:UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: CELL_REUSE_ID)
if (cell == nil)
{
//init and configure the cell
cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: CELL_REUSE_ID)
let selectedView:UIView = UIView(frame: CGRect(x: 0, y: 0, width: cell!.frame.size.width, height: cell!.frame.size.height))
selectedView.backgroundColor = UIColor.black.withAlphaComponent(0.3)
let layer = selectedView.layer
layer.borderColor = UIColor.blue.cgColor
layer.borderWidth = getCellBorderWidth();
layer.cornerRadius = getCellCornerRadius();
layer.backgroundColor = UIColor.blue.cgColor
cell!.selectedBackgroundView = selectedView
}
return cell
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell:UITableViewCell? = getCell(tableView)
cell!.backgroundColor = UIColor.clear
cell!.textLabel!.textColor = UIColor.darkText
cell!.textLabel!.text = tableData[indexPath.row]
cell!.textLabel!.textAlignment = .right
cell!.textLabel!.numberOfLines = 1
cell!.textLabel!.adjustsFontSizeToFitWidth = true
return cell!
}
Cells and table are generated in code, no xibs. Now, If I select cell, the background is covering entire width of table. How can I do background for some cells, that is only half of the table width (or some other percentual ratio)?
I think U can add another view to selectedBackgroundView
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
// selectedBackgroundView
let backgroundView = UIView()
backgroundView.backgroundColor = UIColor(white: 0, alpha: 0)
// add another view to selectedBackgroundView, and set any frame for this view
let selectView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
selectView.backgroundColor = .red
backgroundView.addSubview(selectView)
cell?.selectedBackgroundView = backgroundView
}
return cell!
}

Resources