custom UITableViewCell with image for highlighting - ios

I have a custom UITableViewCell (instantiated from XIB) with an image as background. Now I want another image to be the background when the cell is selected (similar to the blue flashing for standard cells). I already tried setting it using setSelectedBackgroundView in (void)setSelected:(BOOL)selected animated:(BOOL)animated or even in didSelectRowAtIndexPath but the background for highlighting wont show up. What am I doing wrong?

In your Custom Cell.xib
Next make this
And Finally do this

have you tried like below one
Call below Method From the TableView DataSource Method (cellForAtIndexPath) For Doing the same Task
- (void)setCellBGColorNormalAndSelectedForTableViewCell:(UITableViewCell*)cell cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UIImageView *normalCellBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
[normalCellBG setImage:[UIImage imageNamed:#"box_nonselected.png"]];//Set Image for Normal
[normalCellBG setBackgroundColor:[UIColor clearColor]];
[cell setBackgroundView:normalCellBG];
UIImageView *selecetedCellBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
[selecetedCellBG setImage:[UIImage imageNamed:#"box_selected.png"]];//Set Name for selected Cell
[selecetedCellBG setBackgroundColor:[UIColor clearColor]];
[cell setSelectedBackgroundView:selecetedCellBG ];
}

You can set selected background view using the XIB you created for CustomCell . You just need to take UIImageView on to the that XIB (imageview must have same height as that of CustomCell) . Now connect the outlet naming "selectedBackgroundView" for that CustomCell to the UIImageView you have taken as a selected background view . Also check whether the selectionStyle for that CustomCell is not none .

Related

Return custom UILabel from a subclass of UITableViewCell (programatically)

I'm new on iOS and so sorry for my bad english.
I want to change the appearance theme to my application through
the UIAppearance proxy. Then I have some different cells that are a subclass of UITableViewCell with a style for each one.
Some labels in some cells have a different color, so that the "textLabel" property of the cell may have white color in a cell and red color in another.
I have a UILabel subclass, for example, for red color of text.
I change the appearance color for this label as follows:
[[MyRedLabel appearance] setTextColor:[UIColor redColor]];
I want to use the same cell so what I did was Overwrite the method like this:
-(UILabel*) textLabel {
//(MyRedLabel is only put for test purposes, lastly I'll remove it by a property that will be what I want)
return [[MyRedLabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}
The problem is that when TableView draws the cells, text is not. Please can you help me?
Thanks.
[EDIT]
Sorry if I don't explained. I have a UILabel's subclass for each color that I want, so if I want blue label I do this:
[[MyBlueLabel appearance] setTextLabel:[UIColor blueColor]];
I want is assign the label that I want to a cell:
//Cell1
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Identifier"];
if (cell == nil) {
cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel = [[MyBlueLabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
//...
//Cell2
//....
cell.textLabel = [[MyRedLabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
Then when I do cell.textLabel.text = #"Hi world"; the text will be blue in cell1 and red in cell2.
I want to know if this is correct because I don't want to create 4 or more custom cells with his .xib
Not clear what you are asking. But you should not use [MyReadLabel appearance] to set the properties unless you want all you UILabel behaves in the same way. In your case like what you have said, i think you have misused the methods.

UITableViewCell imageView overlaps text in cell

I have a cell that has some text in it and was trying to add an image to the cell.
This is what Ive tried:
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(30, 30, 30, 30)];
imageView.backgroundColor = [UIColor clearColor];
[imageView.layer setCornerRadius:5.0f];
[imageView.layer setMasksToBounds:YES];
[imageView setImage:[UIImage imageNamed:#"test2.jpeg"]];
[cell.contentView addSubview:imageView];
cell.imageView.clipsToBounds = YES;
This is what it looks like right now:
Is there a way to avoid the image from overlapping the text in the cell? Thanks in advance!
tableviewcells have their own imageView that will appear to the left of the content or atleast to the left of the cells text.
You are creating an extra imageView that just happens to have the same name.
Just change the location
cell.imageView.center = CGPointMake(x,y);
Check out https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW11 which has a nice example of how to use the built-in UITableViewCell image view.
Every UITableViewCell already has an UIImageView property 'imageView', but it's only shown if you set the imageView properties image property (cell.imageView.image = /* image code */;)
The best thing you can do is create a custom UITableViewCell. Or perhaps add another UILabel to the current cell and set your text in that instead of using the default UILabel.

iOS: custom view in "viewForHeaderInSection:" delegate method hides for section = 0

I have some problem with UITableView, with it's Header. I need to set custom UIView in every section header, but the problem is in displaying header in section with number 0.
On the picture I tried to show my problem - on the left side its design what I need to do and on the right side it's what I did. This is the comparison of screens
I need fully displaying of badge with character in any section.
Thanks for your help!
You could set transparent tableHeaderView to your tableView with the height that is being hidden. Like this:
UIView *tView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, DESIRED_HEIGHT)];
self.tableView.tableHeaderView = tView;
Good Luck!
make a custom header like this
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *viewHeader = [UIView.alloc initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 28)];
UILabel *lblTitle = [UILabel.alloc initWithFrame:CGRectMake(6, 3, 136, 21)];
[lblTitle setFont:[UIFont fontWithName:#"HelveticaNeue" size:13]];
[lblTitle setTextColor:[UIColor blackColor]];
[lblTitle setTextAlignment:NSTextAlignmentLeft];
[lblTitle setBackgroundColor:[UIColor clearColor]];
[viewHeader addSubview:lblTitle];
return viewHeader;
}
Make a NSArray for Header Titles. and get this title in above method
Looks like it might be an iOS7 specific issue with the tableView hiding partially behind the navigation bar? Have you tried running it on iOS6? I would guess that you wouldn't have the same issue. Try adding the following code to your table view file and see if it helps:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// if this is ios7 this code will make the view appear properly below the navigation bar
if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0) {
self.edgesForExtendedLayout = UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars = YES;
}
}
Well its very simple solution. Just create your custom UITableviewCell class with manually nib file inside that drag and drop UITableviewCell and then set imageview, label etc. On UITableviewCell and then just load the same inside your viewForHeaderinsection. As we know this method returns view. So when load custom UITableviewCell class inside this method just return cell, as it is also inherited from UIView.

Accessory View image not highlighted when row selected

I have added an accessoryView UIView in my UITableCell when cellForRowAtIndexPath is called.
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"flechalinterna.png"]];
cell.accessoryView.backgroundColor=[UIColor clearColor];
But when the user selects row at index path, the accessory view is not highlighted. I tried to avoid it setting the background color to clearColor but it does not make any difference. Getting the following effect shown in this image:
Try to use this one
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"flechalinterna.png"] highlightedImage:[UIImage imageNamed:#"flechalinterna.png"]];

UIButton gets covered when UITableViewCell is selected

I'm using a UIButton inside my UITableViewCell, this button can be selected, gold star, and unselected, gray star, as shown bellow.
The problem is that when ever the cell is selected, no matter if the button is selected or not it will turn to a gray color (this gray color is different than the gray color of unselected mode of the star). I've been trying to figure out why this happens, but had no luck.
here is the implementation of the cells,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom]retain];
//set the background of the cells
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: #"cellBackground.png"]];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cellselected2.png"]];
// set the button content
[button setImage:[UIImage imageNamed:#"star.png" ] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"starSelected.png"] forState:UIControlStateSelected];
[button addTarget:self action:#selector(selectedButton:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(280, 10, 24, 24)];
[cell addSubview:button];
return cell;
}
also when I tap the button when the cell is selected the button will totally disappear!
Thanks,
I figured what the problem is. The problem is that when the cell is selected the UIButton in the cell will go to it's "highlighted state". If there is no image assigned to be used for the button highlighted state of the button it will look the same as it look in my case.
So I just fixed it by adding the image I'd like to be used on the UIButton highlighted state,
[button setImage:[UIImage imageNamed:#"starSelected.png"] forState:UIControlStateHighlighted];
Hope this helps other people who face the same issue :)
You are adding the button to the cell frame instead of it's contentView.
UITableViewCell adds the value of this property as a subview only when the cell is selected. It adds the selected background view as a subview directly above the background view (backgroundView) if it is not nil, or behind all other views.
Whilest a UITableViewCell adds the background view as a subview behind all other views and uses its current frame location.
replace
[cell addSubview:favButton];
with
[cell.contentView addSubview:favButton];
And that should solve it.
The content view of a UITableViewCell object is the default superview for content displayed by the cell. If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode.
It's all in the AppleDocs ^^
Ref: http://developer.apple.com/library/IOs/#documentation/UIKit/Reference/UITableViewCell_Class/Reference/Reference.html
You can certainly set the image for the highlighted state. Another option, if you want to use the same image for UIControlStateNormal and UIControlStateHighlighted you can also say
button.adjustsImageWhenHighlighted = NO, the default is YES.

Resources