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];
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 have a UITableView with method cellForRowAtIndexPath shown below, the problem is that cellImage only appears after having scrolled completely up (so no rows are visible) and then releasing. The strange thing is that I have identical code in another class for a separate table view and the image appears fine. Any ideas?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
}
[cell.textLabel setText:[items objectAtIndex:indexPath.row]];
[cell.textLabel setFont:[UIFont fontWithName:#"Helvetica" size:15.0f]];
[cell.textLabel setTextColor:[UIColor whiteColor]];
[cell setBackgroundColor:[[UIColor alloc] initWithRed:117.0/255.0 green:118.0/255.0 blue:121.0/255.0 alpha:1.0]];
[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundView:[[UIView alloc] init]];
[self.view setBackgroundColor:[[UIColor alloc] initWithRed:167.0/255.0 green:169.0/255.0 blue:172.0/255.0 alpha:1.0]];
UIImageView *cellImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"next#2x.png"]];
[cell addSubview:cellImage];
[cellImage setFrame:CGRectMake(0, 0, 26, 24)];
[cellImage setCenter:CGPointMake(cell.frame.size.width - 30, cell.frame.size.height/2)];
return cell;
}
[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundView:[[UIView alloc] init]];
The lines dont belong to the method. You call the methods every time you scroll for every cell. Put them in your viewDidLoad method, after you have created your table view.
UIImageView *cellImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"next#2x.png"]];
[cell addSubview:cellImage];
These lines are really very BAD and may cause a memory leak.
Everytime you scroll the table view you add a imageview to the cell. Table view reuses the cells. It means when cell is reused, you add a new imageview to the cell. After you scroll more times you have x times imageview on every cell, unless you remove them somewhere in you code.
Other point is, if you want to add some subview to a cell (eg. in init method of the cell) then add it to contenview of the cell like this
[cell.contentView addSubview:someView];
Otherwise your subview may overlap accessory views. And you can have problems when your cell is resized.
I advise you read the guide from the following link:
http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW1
As captured in many of the comments, there are a number of issues:
Set your view and tableView background in your viewDidLoad method (or via your XIB), not in cellForRowAtIndexPath
Use the cell.imageView property to set a left icon, OR, alternatively, to build a custom cell you would use [cell.contentView addSubview:cellImage]. If you do the latter, you should also add your textLabel in the same manner as a UILabel.
To set the background of a tableview cell, use this delegate method:
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[[UIColor alloc] initWithRed:117.0/255.0 green:118.0/255.0 blue:121.0/255.0 alpha:1.0]];
}
though for efficiency you should instantiate this UIColor once in your viewcontroller and set it as [cell setBackgroundColor:self.cellBackground];
The problem you are facing is due to reuseIdentifier for cell.
Also you are creating Imageview everytime cellForRowAtIndexPath is executed.
i will suggest two solution here for you.
1st Option :- just replace this code :
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.accessoryType = UITableViewCellAccessoryNone;
}
this is not proper way when you are dealing with dynamic images, means you are downloading thumbnail from server and data content is more, if it's small and static data then it's fine for you.
2nd Option :- just use this formatted code this will not create imageview everytime just use object from current cell with tag.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
UIImageView *cellImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 26, 24)];
cellImage.tag = 88;
[cell addSubview:cellImage];
[cellImage setCenter:CGPointMake(cell.frame.size.width - 30, cell.frame.size.height/2)];
}
[cell.textLabel setText:[items objectAtIndex:indexPath.row]];
[cell.textLabel setFont:[UIFont fontWithName:#"Helvetica" size:15.0f]];
[cell.textLabel setTextColor:[UIColor whiteColor]];
[cell setBackgroundColor:[[UIColor alloc] initWithRed:117.0/255.0 green:118.0/255.0 blue:121.0/255.0 alpha:1.0]];
[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundView:[[UIView alloc] init]];
[self.view setBackgroundColor:[[UIColor alloc] initWithRed:167.0/255.0 green:169.0/255.0 blue:172.0/255.0 alpha:1.0]];
UIImageView *cellImage = (UIImageView *)[cell viewWithtag:88];
cellImage.image = [UIImage imageNamed:#"next#2x.png"];
}
Fixed using the following code:
UIImageView *cellImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"next#2x.png"]];
[cell setAccessoryView:cellImage];
I tried the following code, but the cell doesn't change at all:
- (void)viewDidLoad
{
[super viewDidLoad];
//Set background image of cells
UITableViewCell *cell;
cell = [CalculatorTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Spots.png"]];
}
Two things. First of all, cells should be created in the cellForRowAtIndexPath method. Secondly, you should make your background view and then add it to the cell content view.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell = [CalculatorTableView alloc] initWithStyle:UITableViewStyleDefault reuseIdentifier:nil]; //This wil obviously depend on your subclass
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Spots.png"]];
[cell.contentView addSubview:backgroundView];
}
cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"Spots.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
That might be a better way to do it.
OR, you can try this:
You can try this code that will help you.
Put this code in cellForRowAtIndexPath method
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 277, 58)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
av.image = [UIImage imageNamed:#"Spots.png"];
cell.backgroundView = av;
You put your cell initializing code in viewDidLoad? That is very wrong, it should be in:
cellForRowAtIndexPath:
You might want to check out this link:
TableView programming
Cells created using static table views don't exist until after viewWillAppear: has been called. Move your code above into that method (and make sure you call the super implementation first.
Greetings,
I'm trying to implement a tableview with asynchronously loading images (80px x 60px). I have the async bit working, however my text (cell.textlabel.text) isn't showing up correctly - it overlaps the image!
Here is a photo of my iPhone:
Any ideas about how I might fix this?
I've tried setting a frame with CGRectMake, but it's not working... All out of ideas here and thought I'd ask the stackoverflow community.
Many thanks in advance,
Here is the code I am using:
-(UITableViewCell ) tableView:(UITableView)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
static NSString *CellIdentifier = #"ImageCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]
autorelease];
} else {
AsyncImageView* oldImage = (AsyncImageView*)
[cell.contentView viewWithTag:999];
[oldImage removeFromSuperview];
}
CGRect frame;
frame.size.width=80; frame.size.height=60;
frame.origin.x=0; frame.origin.y=0;
AsyncImageView* asyncImage = [[[AsyncImageView alloc]
initWithFrame:frame] autorelease];
asyncImage.tag = 999;
NSString *urlStr = [[NSString alloc] initWithFormat:#"http://www.example.com/driverPhotos/%#/%#_80.png",[options objectAtIndex:[indexPath row]],[images objectAtIndex:[indexPath row]]];
NSURL* url=[NSURL URLWithString:urlStr];
[asyncImage loadImageFromURL:url];
[cell.contentView addSubview:asyncImage];
cell.textLabel.frame=CGRectMake(100, 0, 220, 60);
cell.textLabel.text=#"Hi";
return cell;
}
You can't change the frame's of any of the default subviews (detailTextLabel, textLabel, imageView, etc) If you really want to then you'll have to subclass UITableView and override Layout Subviews..
I'd suggest ignoring cell.textLabel and creating your own UILabel and adding it to cell.contentView in the same way you are adding your AsyncImageView (with tags, etc).
If you don't set anything on cell.textLabel then it won't appear and cover up your AsyncImageView. (however if it does then just bring you AsyncImageView to the front)
Btw, if you press the Home button and the lock button at the same time, the iPhone will take a screenshot for you and save it into the photoalbum. Where you can email it to yourself and get it on your PC.
Or you can have a custom cell and implement x, y in layoutSubviews:
-(void) layoutSubviews
{
[super layoutSubviews];
[[self textLabel] setFrame:CGRectMake(75,10,250,20)];
[[self detailTextLabel] setFrame:CGRectMake(75,30,250,20)];
}
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"];