I was trying to use a background image for my TableView so I don't get the leftover tableview cells when there isn't enough data to populate the whole table on screen, so I tried to follow this URL, but much simpler: http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
I do this in the viewcontroller that has an outlet to my UITableView:
UIImage *backgroundTableImage = [UIImage imageNamed:#"form_background.png"]; // basically a gray->white gradient
UIImageView *tableBackgroundImageView = [[UIImageView alloc] initWithImage:backgroundTableImage];
tableBackgroundImageView.frame = self.TableView.bounds;
self.TableView.backgroundView = tableBackgroundImageView;
[tableBackgroundImageView release];
self.backgroundColor = [UIColor clearColor];
Then I have a custom subclass of UITableViewCell that I create in IB. In IB, it's background is set to White-default. My cells are all transparent, and I only see my background. How do I get my cells to stay white.
At first take a view in your .h file UIView *newView; declare its property and synhesize it at your .m file
Now in cellForRowAtIndexPath
if(cell==nil){
tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"imagename.png"]];
................
.........
newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
[cell.contentView addSubview:newView];
................
...............
}
if(indexPath.row == 0){
newView.backgroundColor = [UIColor blueColor];
}
if(indexPath.row == 1){
newView.backgroundColor = [UIColor redColor];
}
if(indexPath.row == 2){
newView.backgroundColor = [UIColor greenColor];
}
If any further question please knock me.
Related
I am trying to customize my tableView in my IOS app. When my tableView(or rather array) is empty, I want to display a customized label instead of the items in the tableView. The label I am referring to is "label0". But something is terribly wrong, my [label0 setHidden:YES]; or [label0 setHidden:NO]; only works in the first block of the if "method"? In the second block (if else) nothing happens no matter what I try to set the label as (hidden or shown).
What have I missed? I cannot see my own fault?
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
UILabel *label0 = [[[UILabel alloc] initWithFrame:CGRectMake(0, 25, tableView.bounds.size.width - 0, 100)] autorelease];
if ([self.searchResults count] == 0){
headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"lista2.png"]];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(5, 3, tableView.bounds.size.width - 5, 18)] autorelease];
label.text = #"Information";
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
[headerView addSubview:label];
label0.text = #"Test test test";
label0.textColor = [UIColor blackColor];
label0.backgroundColor = [UIColor whiteColor];
[tableView addSubview:label0];
[label0 setHidden:NO];
}
else {
headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"lista2.png"]];
UILabel *label2 = [[[UILabel alloc] initWithFrame:CGRectMake(5, 3, tableView.bounds.size.width - 5, 18)] autorelease];
label2.text = #"Search results";
label2.textColor = [UIColor whiteColor];
label2.backgroundColor = [UIColor clearColor];
[headerView addSubview:label2];
[label0 setHidden:YES];
}
return headerView;
}
EDIT
I have moved the code to viewDidLoad and set the property for the UILabel. This have unfortunately not solved my problem....
UILabel *label0 = [[[UILabel alloc] initWithFrame:CGRectMake(0, 25, tableView.bounds.size.width - 0, 100)] autorelease];
[tableView addSubview:label0];
if ([self.searchResults count] == 0){
label0.text = #"Test test test";
label0.textColor = [UIColor blackColor];
label0.backgroundColor = [UIColor whiteColor];
[label0 setHidden:NO];
}
else {
[label0 setHidden:YES];
}
This is because your label0 is created every time this method is called so in "else" you are referring to totally different object (not the one that you added to tableView when array was empty).
You shouldn't be adding subviews to tableView from this method. Consider using viewDidLoad. That way you will be adding label0 only once. To achieve that add label0 as property of your viewController.
You forgot to add label0 as subview please put this line in the else statement
[tableView addSubview:label0];
also I cannot see any benefit from doing so. I believe you can just hide the table view and show another view that has the label. but nesting views that way is not good when you come back to debug this code after 1 month you will struggle to understand it.
You say in your edit that you set the property for the UILabel (I assume you mean that you have a property called label0?). If this is so, then when you alloc init your label, it should be self.label0 = ..... not UILabel *label0 = .....
I am trying to add a background image to UITableViewCell using the following code:
UIImage *bgImage=[UIImage imageNamed:#"green-cell-row.png"];
UIImageView *bgForFirst=[[UIImageView alloc] initWithImage:bgImage];
cell.backgroundView=bgForFirst;
[cell.contentView addSubview:venueDisplayImage];
[cell.contentView addSubview:venueDisplayName1];
[cell.contentView addSubview:venueDisplayName2];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
However, I get venueDisplayName on white space around it. What would be the best way to fix this and correctly add my background image?
Try this way:
1) TabelView set the background like:
tblView.backgroundView.alpha = 0.0;
tblView.backgroundColor=[UIColor clearColor];
2) Add the image view in cellForRowAtIndexPath og delegate method of tableview:
UIImageView *bckView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, 300,80)];
cell.backgroundView.alpha = 0.0;
cell.backgroundColor=[UIColor clearColor];
bckView.image = [UIImage imageNamed:#"cell.png"];
bckView.contentMode = UIViewContentModeScaleToFill;
[cell.contentView addSubview:bckView];
Did you try to change backgroundColor or venueDisplayName to [UIColor clearColor] or to [UIColor greenColor]?
For performance reasons try to avoid using non-opaque views in UITableViewCell.
You have to use the property of cell
there are 2 property you can use
1) cell.backgroundColor
2) cell.backgroundView
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageName:#""]];
In second case you have to set any view as a background view
In my UITableViewCell subclass, I'm overriding the setHighlighted and setSelected methods to change the look of the cell when it's selected, but whenever I set the accessoryView property in either of the methods, all my other code that changes the font and shadow colors is ignored entirely.
For example, using this code will change the text color of the text and detail labels
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
if (selected) {
self.textLabel.textColor = [UIColor whiteColor];
self.textLabel.shadowColor = [UIColor clearColor];
self.detailTextLabel.textColor = [UIColor whiteColor];
self.detailTextLabel.shadowColor = [UIColor clearColor];
}
}
But the moment I add the custom accessoryView to the mix, all the other code is ignored, however the accessoryView image does appear.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
if (selected) {
self.textLabel.textColor = [UIColor whiteColor];
self.textLabel.shadowColor = [UIColor clearColor];
self.detailTextLabel.textColor = [UIColor whiteColor];
self.detailTextLabel.shadowColor = [UIColor clearColor];
self.accessoryView =
[[UIImageView alloc] initWithImage:styleImage(#"/icons/disclosure_selected.png")];
}
}
Is there something I'm doing wrong? How can I properly customize the accessoryView and the rest of the cell's code during the selected and highlighted states?
I ended up just creating my own UIImageView and hiding the built in one.
I think accessoryView is a UIButton not a UIImageView...
Try this :
accessory = [UIButton buttonWithType:UIButtonTypeCustom];
[accessory setImage:[UIImage imageNamed:#"/icons/disclosure_selected.png"] forState:UIControlStateNormal];
accessory.frame = CGRectMake(0, 0, 26, 26); //You can change it
accessory.userInteractionEnabled = YES; //You can change it too
self.accessoryView = accessory;
Hope it'll help
I have Created custom cell.And i have table view with Grouped style.Now i want to put Backgroung image to each cell of different section.How to set the background image of custom cell.
Here is the code for creating the custom cell.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 305, 90)];
// view.backgroundColor=[UIColor darkGrayColor];
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(11, 9, 61, 55)];
// NSLog(#"imageArray%#",[play.imageArray count]);
NSString *img=[play.imageArray objectAtIndex:indexPath.row];
NSLog(#"img=%#",img);
imageV.image = [UIImage imageNamed:img];
[view addSubview:imageV];
[imageV release];
UILabel *lblAchievementName = [[UILabel alloc] initWithFrame:CGRectMake(90, 10, 168, 21)];
lblAchievementName.text = [play.listData objectAtIndex:indexPath.row];
lblAchievementName.backgroundColor=[UIColor clearColor];
lblAchievementName.textColor=[UIColor orangeColor];
[lblAchievementName setFont:[UIFont fontWithName:#"Arial Black" size:16.5]];
[view addSubview:lblAchievementName];
[lblAchievementName release]
[cell.contentView addSubview:view];
return cell;
Now how to set the backgroung image for this custom view?
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:#"categorytab1.png"];
cell.backgroundView = av;
set thru setBackgroundView method.Form the ImageView with ur need Image aand set that as,
[cell setBackgroundView:urBgImgView];
You can use following code for different image in different section.
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(11, 9, 61, 55)];
// NSLog(#"imageArray%#",[play.imageArray count]);
NSString *img=[play.imageArray objectAtIndex:indexPath.row];
NSLog(#"img=%#",img);
if (indexPath.section == 0) {
imageV.image = [UIImage imageNamed:img0];
}
else if (indexPath.section == 1) {
imageV.image = [UIImage imageNamed:img1];
}
else if (indexPath.section == 2) {
imageV.image = [UIImage imageNamed:img2];
}
[view addSubview:imageV];
[imageV release];
i'd rather to add a CALayer or UIImageView to be the backgroud view. Because i used to set the BackgroundColor, but it never workd for me. so i changed my mind by this way.
In my application, i am presenting an UITableViewController class (Grouped style) to the user for the particular scenario. I want to show the table view's cells only. The background should be transparent so that the user can see the previous page.
I tried with the following codes in viewDidLoad method of the table view controller. But they are not working..
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.opaque = NO;
self.tableView.backgroundView = nil;
Is it possible to show the cells with a dimmed background?
Thanks in Advance
I use: (in viewDidLoad)
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.opaque = NO;
Then in cellForRowAtIndexPath:
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.backgroundColor = [UIColor colorWithWhite:1 alpha:.55];
Not sure if this is what you wanted, but it gives you the option to make everything clear. (obviously the alpha would change for your case, but that measures theo pacity of the cells)
Just altering the tableView doesn't help. You have to mess with the cells also. Inside the cellForRowAtIndexPath:(NSIndexPath *)indexPath method, put this before the return statement:
UIView * bgView =[[UIView alloc] initWithFrame:CGRectZero] autorelease];
bgView.backgroundColor = [UIColor clearColor];
cell.backgroundView = bgView;