Custom cell clear background? - ios

I have a tableview in a view controller that is dynamically populated with data from a database. Now I have set the tableview to be clear and it working correctly, but I have tried to set the cells to be clear to no avail ?
cell.backgroundColor = UIColor.clearColor()
That line has been placed in the cellForRowAtIndexPath function.

This little extension should help you.
The idea is to set the backgorundView to clear too and not just the backgroundColor:
extension UITableViewCell {
func setTransparent() {
let bgView: UIView = UIView()
bgView.backgroundColor = .clearColor()
self.backgroundView = bgView
self.backgroundColor = .clearColor()
}
}
Usage:
In the cellForRowAtIndexPath add the following line:
cell.setTransparent()

Related

How to prevent UITableViewCell from being visually deselected right at tap?

I use the following to set the selected backgroud color of my UITableViewCells:
let backgroundColorView = UIView()
backgroundColorView.backgroundColor = .red
UITableViewCell.appearance().selectedBackgroundView = backgroundColorView
When I tap to select them, the cell briefly flashes red and then return to the backgroundColor I've set.
When I add:
if cell.isSelected
{
cell.backgroundColor = .red
}
to cellForRowAt indexPath, the cell becomes red when I scroll it out of sight and back again.
How can I make sure that the cell remains red immediately after I tap it?
EDIT:
If I add the following code to cellForRowAt, things work as expected:
let backgroundColorView = UIView()
backgroundColorView.backgroundColor = .red
cell.selectedBackgroundView = backgroundColorView
Could be an iOS bug?

Change Press Down Color of UITableView

When I tap or hold one of the cells in the UITableView the color of the cell becomes gray by default. I want it to be some other color. I looked everywhere in the storyboard and on stack overflow but could't find it. How do we do it.
Try to change it like this, which will be red for example:
let bgColorView = UIView()
bgColorView.backgroundColor = UIColor.redColor()
cell.selectedBackgroundView = bgColorView
This is an extension based to #evgeny-karkan answer
extension UITableViewCell {
func ChangePressDownColor(to Color: UIColor){
let bgColorView = UIView()
bgColorView.backgroundColor = Color
self.selectedBackgroundView = bgColorView
}
}
Usage:
cell.ChangePressDownColor(to: .red)
check the selection after selecting the cell, where it currently says default.
however, this is very limited number of selection.
if you want custom, you should do it programatically.
i.e./ cell.selectedBackgroundView = customView with color

UITableView background color and separator style change not working

I tried to change the background and separator style in a UITableView, but nothing happens. In the snippet below, everything besides those changes works.
This tableView is located in a #IBDesignable UIView. Best part is, that everything shows as it should in the interface builder. The colors change, the separators dissapear.
func getTableViewToDisplayRecords() -> UITableView {
let tableView:UITableView = UITableView(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: self.frame.size.width, height: (self.frame.size.height * 3) / 4)), style: UITableViewStyle.Plain)
tableView.dataSource = self
tableView.delegate = self
tableView.tableFooterView = UIView(frame:CGRectZero)
tableView.backgroundColor = UIColor.clearColor()
tableView.separatorStyle = .None
return tableView
}
What could be causing this behavior?
----- EDIT -----
I have managed to do a workaround:
UITableView.appearance().backgroundColor = UIColor.clearColor()
UITableView.appearance().separatorStyle = .None
Now everything works fine, although this is not really a solution of the problem. Any new ideas anyone?
Try the code snippet below to change the background colour of UITableview along with no separator between the table cell's.
{
tableView.backgroundColor = UIColor.clearColor()
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
}
Also, you need to change the background color of UITableViewCells
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
// change the background color of cell, which is by-default white
cell.backgroundColor = UIColor.clearColor()
return cell
}
Here is the screenshot with the table background color being red (for demonstration purpose) and no seperator between cell.
And here is another image with table background being red and single line seperator between cell.
Hope this helps!!
I think this is a Bug in UITableView.
When your tableView move to superview, sometimes the 'separatorStyle' property will reset to UITableViewCellSeparatorStyleSingleLine(the default style).
So you must set this property again when you finish frame change. Like this:
[self.view addSubview:_searchContentView];
_searchContentView.separatorStyle = UITableViewCellSeparatorStyleNone;

Images in UIImageView not showing in UITableView when circular mask is applied to the ImageView unless scroll

Thanks in advance for the help.
I have a UITableView within a main view contoller. Within the prototype cell, I have a UIImageview. In the code below everything works until I add the 5 lines to apply a circular mask and border. Once I do that, the images will not load unless I scroll the cells. The mask and border do get applied perfectly however. Will be great when it works... but until then.
Certainly this has been seen before. I'm a swift/objective-C newbie.
Working in swift for this one.
Code below;
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("mixerCell", forIndexPath: indexPath) as! MixerTableViewCell
// set label background color to clear
cell.textLabel?.backgroundColor = UIColor.clearColor()
// set highlight selection to none
cell.selectionStyle = .None
// set image for cell
let imageView = cell.viewWithTag(1) as! UIImageView
// put circular mask and border. This is the problem code that causes initial load of the tableview images to show up blank.
imageView.layer.cornerRadius = imageView.frame.size.width / 2;
imageView.clipsToBounds = true;
let color1 = UIColor(white: 1.0, alpha: 0.5).CGColor as CGColorRef
imageView.layer.borderWidth = 2;
imageView.layer.borderColor = color1
// assign image
imageView.image = UIImage(named: mixerSounds[indexPath.row])
return cell
}
initial view load
after scroll
your code is perfectly working for me. Here i am using Xcode-7. i think you are using Xcode-6.3 or less version. just upgrade it to Xcode- 7. and if you are using the same then just check your heightforRowAtIndexpath or other delegates there should be some issue.
thanks
Try changing the below lines,
// replace this
let imageView = cell.viewWithTag(1) as! UIImageView
// to
let imageView = cell.yourImageViewName
/* yourImageViewName is the outlet
reference name you have given in the
MixerTableViewCell custom class.
*/
Edit 2: Just for debugging purposes,
hardcode the image name and check if the image appears on the all the cells.
imageView.image = UIImage(named: "first1.png")
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
let cellIdentifier = "cell"
var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as UITableViewCell
cell.image_View.image = UIImage(named: mixerSounds[indexPath.row])
println("The loaded image: \(image)")
cell.image_View.layer.masksToBounds = false
cell.image_View.layer.borderColor = UIColor.blackColor().CGColor
cell.image_View.layer.cornerRadius = image.frame.height/2
cell.image_View.clipsToBounds = true
return cell
}
Give imageview outlet to cell and not give imageview name because by default name is imageview so take diffrent name
It looks like the problem is using clipToBounds = true I am facing the same issue while making circular UIImageView inside UITableViewCell
I didn't find the exact solution but for now I found a way to do this
if (indexPath.row == indexPath.length && !isTableReloaded)
{
let dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.000000001 * Double(NSEC_PER_SEC)))
dispatch_after(dispatchTime, dispatch_get_main_queue(), {
self.reloadTableView()
})
}
func reloadTableView()
{
isTableReloaded = true
self.tableViewContacts.reloadData()
}
Here isTableReloaded is a Bool type var which is initialized to false in viewDidLoad()
and the if condition is to be placed at the last of cellForRowAtIndexPath but before return statement
This will resolve our problem but do not rely on this as this is not the best approach.
Please post solution for this if any one found the better approach.
Here is a perfect and state away solution for circular image in UITableview Cell.
Simply modify your UITableviewCell (custom cell) class with below code.
override func awakeFromNib() {
super.awakeFromNib()
imgEvent.layer.frame = (imgEvent.layer.frame).insetBy(dx: 0, dy: 0)
imgEvent.layer.borderColor = UIColor.gray.cgColor
imgEvent.layer.cornerRadius = (imgEvent.frame.height)/2
imgEvent.layer.masksToBounds = false
imgEvent.clipsToBounds = true
imgEvent.layer.borderWidth = 0.5
imgEvent.contentMode = UIViewContentMode.scaleAspectFill
}
It will also helps to solve the problem of image circular only after scrolling table..(if any!)
let width = cell.frame.size.width
cell.img.layer.cornerRadius = width * 0.72 / 2
0.72 is the ratio of the cell width to image width, for eg. cellWidth = 125 and imageWidth = 90, so 125/90 would give 0.72. Do similar calculation for your image.
First: Images doesn't load until you scroll, because when cellForRowAtIndexPath methods called the constraints doesn't set for image until now, so when scrolling the constraints was added and the image appears, so if you set proportional width and height for imageView (width==height) in cell then
do that
let w = tableview.frame.width*(proportional value like 0.2)
imageView.layer.cornerRadius = w / 2
imageView.clipsToBounds = true;

How to load content view inside a tableview cell?

I have created a table view as
var tblView : UITableView = UITableView()
tblView.frame = CGRectMake(0, 168, 320-50 , 450)
tblView.separatorColor = UIColor.clearColor()
tblView.scrollEnabled = false
tblView.rowHeight = 39
self.addSubview(tblView)
tblView.delegate = self
tblView.dataSource = self
tblView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "myCell")
Now i am trying to add custom view to the tableviewcell as
//MARK: table view data source methods
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("myCell") as UITableViewCell
var cellImgView:UIImageView = UIImageView()
cellImgView.frame = CGRectMake(cell.contentView.frame.origin.x+10, cell.contentView.frame.size.height, 20, 20)
let cellImage = UIImage(named: self.cellImgs[indexPath.row])
cell.backgroundColor = UIColor(red: 0.000, green: 0.400, blue: 0.404, alpha: 1.00)
cellImgView = UIImageView(image: cellImage)
cell.contentView.addSubview(cellImgView)
return cell
}
Only first image is at correct position and other are overlapped.Why is this happening?
cellForRowAtIndexPath will called each time when cell in user's sight.
But you need add UIImageView only once. So, you need to check, is there is the first call.
Also, it is good practice to use custom UITableViewCell
Tableview is reusing created cells, so your approach is not the best. What you are doing now is creating UIImageView everytime a cell is being used and if its a cells that is going to be reused you will create new UIImageView on top of your previous one.
I would suggest to do all this in interface builder. If you dont want to, at least subclass UITableViewCell and create image view in init method of your cell, that will take care of overlapping UIImageviews over each other, but still consider using interface builder, its the easiest way to build tableviews.
EDIT
Here's some tutorial on tableviews in interface builder, this should help you get started: http://shrikar.com/blog/2015/01/17/uitableview-and-uitableviewcell-customization-in-swift/
Good luck!

Resources