I've read and tried a few answers I have found on StackOverflow. I've also read and tried a few things from blogs, but nothing seems to accomplish what I am looking for.
I create a UIView and set it's background color to my desired UITableViewCell selection color (instead of the standard blue or gray selection colors). I add this UIView to my cell's selectedBackgroundView and this works fine, my cell changes to the desired color on user selection.
This method works great on Plain UITableViews; not so well on Grouped. On a grouped UITableView, the 1st and last cell do not conform to clip / mask bounds as demonstrated in the below screenshots.
I know there is no way to round just the top-left and top-right corners only.
I want to do this strictly by code, without images.
Question
Does anyone know of a nice little work around to change the selectedBackgroundView color of a UITableViewCell using only the UIView and not images AND to make the 1st and last cell conform to the rounded corner boundaries?
Example
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = #"Cell";
WCSBadgedCell * cell = [[WCSBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle andBadgeStyle:0 reuseIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[WCSBadgedCell alloc] initWithStyle:UITableViewCellStyleDefault andBadgeStyle:0 reuseIdentifier:CellIdentifier];
}
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:DARKBROWN];
[bgColorView setClipsToBounds: YES];
[cell.layer setMasksToBounds:YES];
[cell setSelectedBackgroundView:bgColorView];
[cell.textLabel setText: #"Testing a Cell"];
return cell;
}
Screenshots
Solution
I Accepted CodaFis answer because he added a comment which pointed to a pretty nice (yet lengthy) solution. I had to do quite a bit of revamping, but in the end, I now have the selectedBackgroundView's I needed which round the corners on the 1st and last cells, thanks again!
Here is a n example of how I achieved this.
I assume that you are using a UITableViewCell subclass because of the complexity of your cell. This is how I've been doing it:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]))
{
self.clipsToBounds = YES;
UIView* bgView = [[UIView alloc] init];
bgView.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.25f];
self.selectedBackgroundView = bgView;
//other code
}
return self;
}
This produces a sort of dark grey overlay on the cell, not images required!
In your case, the exact color of your selected cell (thanks to the handy dandy Digital Color Meter) would be
[UIColor colorWithRed:106.0f/255.0f green:51.0f/255.0f blue:6.0f/255.0f alpha:1.0f];
and the white text would be
- (void)setSelected:(BOOL)sel animated:(BOOL)animated
{
[super setSelected:sel animated:animated];
if (sel)
{
self.textLabel.textColor = [UIColor whiteColor];
}
else
{
self.textLabel.textColor = [UIColor colorWithRed:(105.f/255.f) green:(50.f/255.f) blue:(6.f/255.f) alpha:1.f];
}
}
I had the same problem, and fixed it by overriding -(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated in my UITableViewCell subclass and setting no selection style when creating the cell
//set no selection style in the cell
...
[self setSelectionStyle:UITableViewCellSelectionStyleNone];
...
//override setHighlighted to manually set the regular/highlighted background colors
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
if(highlighted){
self.backgroundColor = [UIColor greenColor];
}
else {
self.backgroundColor = [UIColor redColor];
}
}
Related
Following the advice in this answer Custom UITableViewCell selection style? about how to make a custom selection style for a UITableViewCell I did the following.
First, in my custom view controller, which has a UITableView as a property and which serves as its datasource and delegate I have the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//UITableViewCell *cell;
NavTableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Then this is my entire custom UITableViewCell:
#import "NavTableViewCell.h"
#implementation NavTableViewCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
if(highlighted){
self.contentView.alpha = .5;
}else{
self.contentView.alpha = 1;
}
}
#end
I have also tried putting the code in setSelected. In both cases I don't see any change in my selected/highlighted table view cell. I have tried changing its color and changing the colors of the labels within it, but there is simply no change. I have also tried going through all the kinds of selection styles for the cell, all to no avail as far as seeing my customization.
What am I doing wrong?
From the documentation:
You can use selectedBackgroundView to give the cell a custom appearance when it is
selected. When the cell is selected, this view is layered above the
backgroundView and behind the contentView.
So whatever you want to do to your cell while selecting it, you have to do it for the selectedBackgroundView .
Try this:
UIView *highlightView = [[UIView alloc] init];
highlightView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:1.0 alpha:.5];
cell.selectedBackgroundView = highlightView;
I have an cell class that is inherited from UITableViewCell. It has next grey lines:
However if I don't set cell texts I don't see these lines:
So what can be the cause for this lines?
UPDATE
Here is code for my custom cell:
#implementation MDItemTableViewCell
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
NSString *fontName = selected ? self.selectedFontName : self.fontName;
self.textLabel.font = [UIFont fontWithName:fontName size:self.fontSize];
}
#end
UPDATE 2
Here is code for cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *source = [(MDChoiceField *)self.field source];
MDChoiceItem *item = source[(NSUInteger)indexPath.row];
MDItemTableViewCell *cell = [(MDItemTableView *)self.fieldView cellForRowAtIndexPath:indexPath];
cell.textLabel.numberOfLines = 0;
NSString *string = NSLocalizedString(item.title, nil);
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]
initWithString:string];
[attributedString addAttribute:NSKernAttributeName
value:#(1.2)
range:NSMakeRange(0, [string length])];
cell.textLabel.attributedText = attributedString;
return cell;
}
And view one:
- (MDItemTableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MDItemTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.font = [UIFont fontWithName:self.fontName size:self.fontSize];
cell.fontSize = self.fontSize;
cell.fontName = self.fontName;
cell.selectedFontName = #"HelveticaNeue-Light";
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.textColor = [UIColor colorWithHexString:BLUE_COLOR];
if ([cell respondsToSelector:#selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:#selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.bounds];
cell.selectedBackgroundView.backgroundColor = [UIColor colorWithHexString:LIGHT_BLUE_COLOR];
return cell;
}
Sorry for so many lines.
Note that the added lines are shorter than the table cell itself. If you turn on "Color Blended Layers" in the simulator Debug menu you can see that they seem to be the same length as the label. This suggests it's the label, not the cell, which is producing the oddity.
This question seems related: How can I remove UILabel's gray border on the right side?
And that suggests the following fix, added to cellForRowAtIndexPath::
[cell.textLabel setBackgroundColor:[UIColor clearColor]];
A few settings will help you,
As Richa's answer, self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; It'll hide seperator (default one) from the table.
As user3781721 answer, tableName.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; will remove any empty cell. So there'll be a plain look of table.
In tableview's cellForRow datasource, cell.clipsToBound = YES; before return the cell.
If still not solve then increase your cell height little, may be by 5 and check.
Put this code in ViewDidLoad
tableName.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
Using a 'Grouped' style UITableView will keep the table from showing these 'blank' cells if they are in fact empty. You can either declare this when you init the UITableView like this,
UITableView *myTable = [[UITableView alloc] initWithStyle:UITableViewStyleGrouped];
Or if you're using interface builder there's a drop down to change the style from 'Plain' to 'Grouped'
Those lines are the default separator lines of tableView. Just set the separator style as NONE. write or use this line wherever you have declared your UItableView. Either in Xib file or code.
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
Update:1
- (void)viewDidLoad { [super viewDidLoad];
// This will remove extra separators from tableview
self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
}
Use this, and let me know if it works or not.
I'm setting UITableView separator color this way:
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.separatorColor = [UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.1f];
}
And SOMETIMES on iOS7 my table view separators look different (cells with data and empty cells separators have different alpha I guess), look at attached image:
How can I solve this issue?
If you want to remove all cell's separator which has empty content then use following line of code.
self.tableView.tableFooterView = [[UIView alloc] init];
in viewDidLoad method.
Other wise set self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; and set your custom UIImagefor separator.
EX:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"cell"; //[NSString stringWithFormat:#"S%1dR%1d", indexPath.section, indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
.
. // your other code.
.
UIImageView *imgLine = [[UIImageView alloc] init];
imgLine.tag = 101;
[cell.contentView addSubview: imgLine];
}
.
. // your other code.
.
UIImageView *imgLine = (UIImageView *) [cell.contentView viewWithTag:101];
imgLine.frame = CGRectMake(5, 69, 310, 1);
imgLine.image = [UIImage imageNamed:#"queTblLine.png"];
return cell;
}
And don't forget to set self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
EDITE :
Not sure but might be helpful in your case:
set self.tableView.separatorInset = UIEdgeInsetsZero;
Get From question/answer.
Swap the order of the two statements. Set the color first, then the inset:
self.tableView.separatorColor = [UIColor redColor];
self.tableView.separatorInset = UIEdgeInsetsZero;
You can achieve this visually in storyboard or nib/xib
Put UIImageView at bottom edge of cell and you can set image as per your requirement
STEP 1: Set divider as per image
STEP 2: Set Separator style to none
You can set it programatically:
- (void)viewDidLoad {
[super viewDidLoad];
[[UITableView appearance] setSeparatorColor:[UIColor redColor]];
}
Xcode 5.0, iOS 7 and updating an existing app. UITableView selected row is now gray, not blue.
From what I've read they changed the default selectionStyle to gray. But "Blue" is still an option in IB and UITableViewCellSelectionStyleBlue still exists. Checking out the new HIG, it doesn't look like they removed the blue and the "Settings" app still using blue cell selection.
I've tried setting the value in IB and in code, but no luck. Any ideas on what I need to do to get the blue selection style back?
There is only one selectionStyle in iOS7, to change you need to do this manually like below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
....
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor colorWithRed:(76.0/255.0) green:(161.0/255.0) blue:(255.0/255.0) alpha:1.0]; // perfect color suggested by #mohamadHafez
bgColorView.layer.masksToBounds = YES;
cell.selectedBackgroundView = bgColorView;
....
return cell;
}
I know this has already been answered but the last thing I wanted to do was touch all of my cellForRowAtIndexPath methods. So, I used an appearance proxy in my App Delegate. I took #null's code above to set the selected background view and in the applicationDidFinishLaunching:withOptions: method I placed this code.
UIView *bgColorView = [[UIView alloc] init];
//the rest of null's code to make the view
[[UITableViewCell appearance] setSelectedBackgroundView:bgColorView];
Then to make the white text hi light:
[[UILabel appearanceWhenContainedIn:[UITableViewCell class], nil] setHighlightedTextColor:[UIColor whiteColor]];
This made a global change in my app. The appearance proxy was introduced in iOS5 and Mattt has a great article on how to use it at his NSHipster blog.
Probably it could help you. I have my custom cell and to make it selected with needed color I have overwrite setHighlighted and setSelected now it's look like that
#define IOS_7 (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1 ? YES : NO)
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
[self changeSelectionColorForSelectedORHiglightedState:selected];
// Configure the view for the selected state
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
[super setHighlighted:highlighted animated:animated];
[self changeSelectionColorForSelectedORHiglightedState:highlighted];
}
- (void)changeSelectionColorForSelectedORHiglightedState:(BOOL)state
{
if (IOS_7) {
if (state) {
self.contentView.backgroundColor = [UIColor blueColor];
}
}
}
I know I'm really late to the party, but I'll offer my IOS10 work around as well. Don't touch any of your other code, but add:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor blueColor];
cell.textLabel.textColor = [UIColor whiteColor];
... whatever else you do ...
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
cell.textLabel.textColor = [UIColor blackColor];
}
I want this below type of layout in iPhone (using UItableview, Uitableviewcell, Uilabel and Uiimageview).
(assume light blue dots == uiimageview)
In each UItableviewcell I have 1 UIimageview and 1 Uilabel.
both control's starting alightment are same TOP LEFT.
My uilabel's content are dynamic and image is only one static image.
Image is very small compare to uilabel's content.
So, my image goes center vertical and horizontally. but I want only on TOP Left corner.
Edited :
I have already do this in Uitableviewcell
But if Uilabel have number of line is 2 or more then imageview's position is changed.
It goes in center vertically and horizontally. but I want imageview only on top left position whether uilabel's number of line 1,2,3....n
my code is here:
In CommonCustomeCell.m
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier];
if (self) {
dotImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"dotPoint.png"]];
dotImage.opaque = YES;
[self.contentView addSubview:dotImage];
titleName3 = [[UILabel alloc] init];
titleName3.textAlignment = UITextAlignmentLeft;
titleName3.backgroundColor = [UIColor clearColor];
titleName3.adjustsFontSizeToFitWidth = FALSE;
titleName3.font = [UIFont systemFontOfSize:14];
titleName3.textColor = [UIColor colorWithRed:17.0/255.0 green:72.0/255.0 blue:147.0/255.0 alpha:1];
titleName3.lineBreakMode = NSLineBreakByWordWrapping;
[titleName3 sizeToFit];
[self.contentView addSubview:titleName3];
}
-(void)layoutSubviews
{
frame = CGRectMake(20,2,self.bounds.size.width - 45 , self.bounds.size.height);
[titleName3 setFrame:frame];
frame = CGRectMake(4,10,dotImage.frame.size.width,dotImage.frame.size.height);
self.dotImage.frame = frame;
}
}
In RootViewController.m file
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CommonCustomeCell *cell = (CommonCustomeCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CommonCustomeCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
[cell.dotImage5 setImage:[UIImage imageNamed:#"myLogo.png"]];
cell.titleName3.text = [[myArr objectAtIndex:indexPath.row] objectForKey:#"title"];
}
}
help me, Thanks in advance.
Create UITableView with Section and grouped style.
Your loarem Ipsm (text) set as a Section Title.
and Create UITableViewCell custom cell with UIImageView and UILabel to display text
"loarem Ipsm is simple dummy"
Have you set autosizing for the CustomCell View same as given in the image below? Only top and left should be enabled.