When selecting, and holding, one of my custom cells for a UITableView plain-style table, it displays my custom selection color (redColor in this case), but then if I keep it held down and scroll, the custom selection color and the custom background disappear (showing through to the UIImageView behind the table.
The UITableView is created in IB and has a background set to clearColor to allow an image to show through.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:kCellIdentifier]
autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
/* Background */
cell.backgroundView = [[[UIImageView alloc] init] autorelease];
cell.selectedBackgroundView = [[[UIImageView alloc] init] autorelease];
}
cell.backgroundView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"customBackground"]];
cell.selectedBackgroundView.backgroundColor = [UIColor redColor];
return cell;
}
Can anyone help? Thanks!
Ok I solved this. Basically you cannot use initWithPatternImage when customizing a UITableViewCell. After it is selected, it's background view must either go transparent, or be removed from the view hierarchy.
Use the following:
((UIImageView*)cell.backgroundView).image = [UIImage imageNamed:#"customBackground"];
((UIImageView*)cell.selectedBackgroundView).image = [UIImage imageNamed:#"selectedBackground"];
Related
I am loading custom separator image in uitableview cell.
Here is my code :
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellID=#"Cell"
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SwitchCellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIImageview *aSwitch = [[UIImageview alloc] initWithImage:[UIImage imageNamed:#"divider.png"]];
separator.frame = CGRectMake(0,50,320,1);
[cell.contentView addSubview:seperator];
}
if(cell.height == 22)
{
/// here i am setting frame for uiimageview
}
but i am getting seperator image disappears for only one row out of 20 while scrolling.
Can you please help why it is loading like this.
If you've put your separator UIImage out of the bounds of the cell, and set cell.clipsToBounds = NO; to have the separator image displayed, the separator image might get hidden by drawing the next cell.
You can't control the z-index of the cells as they're being drawn on screen, it depends from where you're scrolling (bottom to top, or top to bottom).
If that's indeed you're issue, you can either put the divider inside the cell's frame, or if your separator is thin enough use:
[TableView setSeparatorColor:[UIColor colorWithPatternImage:[UIImage imageNamed:...]]];
self.tblView=[[UITableView alloc] initWithFrame:CGRectMake(0,0,320,370) //set as u need
style:UITableViewStylePlain];
self.tblView.delegate=self;
self.tblView.dataSource=self;
[self.view addSubview:self.tblView];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];//set as u ne
v.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"divider.png"]];
[self.tblView setTableHeaderView:v];
[self.tblView setTableFooterView:v];
[v release];
I am using a grouped table view for my iPhone application, for which I need to change the selected background color to red. I can set it, but the problem is that the first table cell the views are outside the table cell.
try to this....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.backgroundColor = [UIColor clearColor]; // clear the cell background color
// Configure the cell.
return cell;
}
You can use following code
1)cell.selectionStyle = UITableViewCellSelectionStyleRed;
OR
2)cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = [UIColor colorWithRed:(245/255.0) green:(245/255.0) blue:(245/255.0) alpha:1];
cell.selectedBackgroundView = selectionColor;
use any of above code will solve your problem if still your problem is not solved put your code i'll try to solve
I am using UITableView in my application and I have created custom cell in the file DealsCC.xib and when cell is tapped the color of the cell should be changed to blue but it does not happen in my code. I write the code as follows:
static NSString *MyIdentifier = #"dealsCC";
dealsCC *cell = (dealsCC *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
[cell selectionStyle:UITableViewCellSelectionStyleBlue];
I want to mention that in the line
[cell selectionStyle:UITableViewCellSelectionStyleBlue];
warning msg exists and it is "dealsCC may not respond to -selectionStyle"
plz help me to solve this problem
thanx in advance.
please see the following stackoverflow links,please check whether you have followed correclty them
1)link1
2)link2
3)enter link description here
Try this in your custom class for the table view cell
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
self.selectionStyle = UITableViewCellSelectionStyleBlue;
[super setSelected:selected animated:animated];
}
The warning is because the method should be
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
and not [cell selectionStyle:UITableViewCellSelectionStyleBlue];
Use cell.selectionStyle = UITableViewCellSelectionStyleGray; for changing the style of the cell selection. But here the selectionStyle only takes input of UITableViewCellSelectionStyle type.
For changing Color you need to use Image. Use selectedBackgroundView.image property and Follow the tutorial Link
Or you can also try
cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
cell.selectedBackgroundView.backgroundColor = [UIColor redColor];
EDIT:
OK I am updating code in case you have any other controls on the cell, then you can try below code.
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"myCellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIView *v = [[[UIView alloc] init] autorelease];
v.backgroundColor = [UIColor redColor]; // any color of your choice.
cell.selectedBackgroundView = v;
// After this line add all other controlls you are adding...
}
// Set up the cell...
cell.textLabel.text = #"foo";
return cell;
}
In my application ,I am using Grouped style for TableView. In that I want to customize the cell selection Style.I want that selection Style to be red.
I am using the below code for that:
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor redColor];
[cell setSelectedBackgroundView:bgColorView];
[bgColorView release];
By using the above code. I have a problem.Since I have taken grouped Style table , in the
selection of first and last rows, the selection is appear with sharp edged rectangle instead of
appeaing round Corners.
Can Anyone Help me in this.
Thanks in Advance.
Try this ,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"SelectedCellBackground.png"]] autorelease];
}
// Cell configuration takes place here
}
add QuartzCore framework. and import QuartzCore/QuartzCore.h framework in .m file. and after that add following code in cellForRowAtIndexPath Method.
UIImageView *imv = [[UIImageView alloc]init];
imv.backgroundColor=[UIColor redColor];
[cell setSelectedBackgroundView:imv];
CALayer *layer = imv.layer;
[layer setMasksToBounds:YES];
[layer setCornerRadius:10.0];
[imv release];
Override -(void)setSelected:animated: in your subclass.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
if (selected)
[self setBackgroundColor:[UIColor redColor]];
else
[self setBackgroundColor:[UIColor whiteColor]];
}
You can add fancy animation here to blend in and fade out the background upon selection and deselection if animated is YES.
In general UITableViewCell can be a bit awkward to subclass, be patient here.
UIImageView *imageVieww=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell2.bounds.size.width, cell2.bounds.size.height)];
imageVieww.image=[UIImage imageNamed:#"mavilik.png"];
[cell2 setSelectedBackgroundView:imageVieww];
[imageVieww release];
When I set repeating background image to a UITableView by using the following code is ok:
tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bg.png"]];
but when I use it to set repeating-background image to a UITableViewCell by following code, nothing happen, I don't know why, I also cannot set background color to this cell too.
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bg.png"]];
Is there anyone know the way to so this? please help me!
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cell.png"]];
which would go inside this code block:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[RecipeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cell.png"]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cell-on.png"]];
}
// Configure cell
return cell;
}
You can also see how to set the selected image in inside there too: selectedBackgroundView.
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cell-pressed.png"]];
I'm unsure about repeating, but I'm pretty sure backgroundView can have contentMode set on it.
Oh, it is more easier than I think. I just need to set background image by following code:
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bg.png"]];
and the whole cell background view is automatically filled with this background image. :)