cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
in this method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
but I can only see it when I select that cell otherwise it's not visible.and it work perfectly when background is white.
I am sure that I need to set a property, but I don't know which property I need to change to make this thing work.
thanks in advance.
cheers.
I ran into this same issue, and just create a UIImageView out of a UIView’s imageWithName #"AccDisclosure.png" using the following hastily mocked-up graphic which you're free to copy: http://thinkingman.com/db/downloads/AccDisclosure.png (if you just click that link, you'll probably see nothing, as it's a white image with a transparent background, but if you save it and view against a dark background, you'll see the alpha).
It would appear that the disclosure indicator is a gray, high-alpha image, so overlaying that over a black background makes it invisible. If you want to do this, you'll need to add your own UIImageView to the cell's contentView.
The following code allows me to set the background color of the arrow tip in a table row:
#property (nonatomic,retain) UILabel *backgroundLabel;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor orangeColor];
self.backgroundLabel = label;
[self.contentView addSubview:label];
[label release];
CGRect labelRect = CGRectOffset(contentRect,0, 0);
labelRect.size.height = contentRect.size.height - 1; // show white line
labelRect.size.width = contentRect.size.width + 50; // cover arrow tip background
backgroundLabel.frame = labelRect;
backgroundLabel.highlightedTextColor = [UIColor whiteColor];
I made a solution where I added an ImageView with addSubView in the normal fashion, with an image that was not black (in my case, a grey filled circle) at the position where the accessory appears.
That allows the arrow to be seen and still have a dark/black table cell background color.
Maybe not the most kosher solution, but it makes the arrow visible, and I get notified of accessory clicks without subclassing or writing lots of code.
Related
I'm trying to display a table of choices in a popover on the iPad. I'm adding a UITextField to the default UITableViewCell. The table is configured as a sectioned table.
It does not draw correctly and is not displayed.
If the row is selected, the UITextField becomes visible. If you click another row, it disappears.
I've played with subview order to no avail. What has worked has been to set UITableViewCell.backgroundColor = [UIColor clearColor].
If I do that, I think need to set the color somewhere else such as the content view, UITableViewCell.contentView.backgroundColor = [UIColor whiteColor];
However, it still does not display correctly. It looks fine when its not selected, or selected. But if I programmatically set an accessory checkmark, half of the background of the accessory checkmark shows up clear with the darker color of the table background.
If the row is selected, it looks ok, but if another row is selected, as the row with the UITextField is being de-selected, for a fraction of a second, you see half of the accessory background clear (transparent).
Any ideas or suggestions of what might be wrong or how to work around this? The Apple docs clearly state that adding a view to the ContentView is an appropriate way to implement it, yet it does not appear to work in this case.
Here's the code I currently have within tableView:cellForRowAtIndexPath:
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(130, 10, 60, 26)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.text = #"foo";
[cell.contentView addSubview:textField];
[textField release];
cell.contentView.backgroundColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor clearColor];
In general the place to set a UITableViewCell's backgroundColor is not tableView:cellForRowAtIndexPath:. You must do it in +tableView:willDisplayCell:forRowAtIndexPath:+.
You should not need to change the contentView's backgroundColor.
Whenever I add an accessoryView to my UITableViewCell, it doesn't carry the background color across? I'm setting a UISwitch as my accessoryView, and the color I have set in the cell.backgroundColor property only effects the contentView and not the accessoryView.
I have tried everything to set them to the same value. I tried to set the cell.backgroundView.backgroundColor and the cell.accessoryView.backgroundColor properties to the color I want but nothing is working. I also tried creating a subview inside contentView, which solved the backgroundColor problem (by avoiding it), but it creates the problem, where the switch sits on top of the cell.textLabel when the text is too long.
Is there are way I can modify the background color of the accessoryView without creating a subview in contentView, or to alter the length of the cell.textLabel without subclassing UITableViewCell?
Upon reading the documentation (a novel idea), I found the article, "A Closer Look at Table-View Cells". It helped me understand the composition of the cells, and I found my answer...
cells look like this...
Since the cell.accessoryView is a sister view to cell.contentView I had to ask the cell.contentView for its superview, and then I was able to change the background color for both views at once. Here's what the code looks like...
// Cell Formatting
cell.contentView.superview.backgroundColor = [UIColor greenColor];
I know it's really simple, but I'm a newbie and it took me ages to slow down and read the doc. Hopefully, this helps some other folks out there!
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor yellowColor];
}
UIView *myView = [[UIView alloc] initWithFrame:cell.frame];
myView.backgroundColor = [UIColor colorWithRed:214.00/255.00 green:233.00/255.00 blue:247.00/255.00 alpha:1.0];
cell.backgroundView = myView;
[myView release];
If you want to blend the accessory view background color with the background color of the cell, in iOS8 and Swift this worked like a charm in the tableView(_, cellForRowAtIndexPath:) method:
let color = cell.contentView.backgroundColor
cell.backgroundColor = color
#Zak, first of all thanks for bringing to my attention the details of the cell layout. Very helpful!
I would just like to point out that the following code:
self.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:#"cell_background.png"]];
worked for me. The result was a background image stretching over whole cell. AccessoryTypeView didn't cover it! Important part is to put this code into layoutSubviews method of your custom cell class and not into cellForRowAtIndexPath found in TableViewController class. In my case I defined a class CustomCell and inside of it I have defined labels, image views etc.
NOTE:The following code wasn't working:
cell.contentView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:#"cell_background.png"]];
when put inside tableView:(UITableView *)tableView cellForRowAtIndexPat method. It was giving me the background covering the cell but AccessoryTypeView was above it...
I have a UITableView with a custom background image set like this:
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"mybg.png"]];
The background appears fine, but my UITableViewCells (default cells, not custom) have some sort of weird tint to them, and the UILabel containing the "New Project" text also seems to have some sort of background behind it. How can I remove this? I've already tried:
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
Thanks
alt text http://cl.ly/Cg5/content
I believe that this is a nasty side-effect of simply adding an image straight into your table view's backgroundColor.
Try adding the image to the view's background color:
[[self view] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"mybg.png"]]];
and then set the table view's backgroundColor to be clear:
[[self tableView] setBackgroundColor:[UIColor clearColor]];
I hope this helps!
Some times when your working with setting images for an app, and testing on the simulator, they get frozen to the app for the few run. Not sure, this is the case even if you delete the image files; they still keep popping up.
I would make that you have rest the simulator, and restart Xcode. Then force a rebuild of the app back on the simulator. This should clear out any images- even background images if they are still being referenced.
If this is not a solution that works...try making sure that you don't have conflicting commands going to the same UiTablView object-(1 from IB and 1 from Xcode programmically). Sometimes you can overlook that you have set something in IB, and it conflicts with what your telling it to do programically.
If that doesn't solve the issue...check the connections in IB and make sure your reffrencing the correct IBOutlet UITableView *tableview. And you have the delegat and data protocols in the header.
If you want to have each cell set with background and want to remove text's background, maybe you can try this...
- (void)viewDidLoad {
...
self.tableView.backgroundColor = [UIColor clearColor];
...
}
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"mybg.png"]];
cell.textLabel.backgroundColor = [UIColor clearColor];
...
}
How can I set the background of UITableView (the tableview style is "Grouped") to use an image?
In newer versions of the SDK, you'll need to set tableView.backgroundView if you want it to be transparent, try something like this:
tableView.backgroundColor = [UIColor clearColor];
tableView.opaque = NO;
tableView.backgroundView = nil;
We need to do something about that plain background. We're going to use a PNG image and display it behind the UITableView.
Prepare a PNG image. It should be either 320x460 (if you have the status bar visible in your app) or 320x480 (if you hide it).
Drag it into XCode into the Resources folder and add to your project
Load the NIB file containing your UITableView into Interface Builder
Open the library (Tools> Library), switch to the Media tab, and drag the image to the View, create a new UIImageView.
Use the inspector to move and resize the image so it's at X=0, Y=0, Width=320, Height=480
Put the UIImageView behind the UITableView (Layout > Send to Back)
Save, Build and Go!
Disappointingly, you won't be able to see your background. The UITableView's background is blocking us from seeing the UIImageView. There are three changes you need to make:
In the Attributes Inspector, make sure the UITableView's "opaque" checkbox is unchecked!
Set the UITableView's background color to transparent:
tableView.backgroundColor = [UIColor clearColor];
I hope this helps and solves your problem. It has worked for me and I have yet to find a more elegant way to display a background image for a UITableView.
The advantage of my solution, in comparison with setting a background image directly on the UITableView, is that you can indent the table's content. I often wanted to do this to just show two or three table cells at the bottom of the screen.
[tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"whatever.png"]]];
tableView.backgroundView = nil;
is enough. No need to set background color as Clear Color.
One way would be to make the table view transparent (set the view's background to 0% opacity) and place a UIImageView behind the UITableView. Remember that transparent tables and table cells will not perform as well as opaque ones.
In UI Builder the Background color has an "Other" choice.
This brings up a color picker.
The color picker has an opacity setting.
If you set the Opacity of the COLOR to 0 it works, can't speak to performance.
What I've found is that you have to use a "plain" styled table with a transparent background and then recreate the look of the rounded-corner cells by setting each cell's backgroundView to a UIImageView with a image that simulates the rounded look. This means that the top, bottom, and middle cells need different background images.
However, this does not address what happens when the user taps the cell and it goes "highlighted" - it will look squared off then. You can get around this by setting the highlighted image for your faked tablecell background image. You will also want to create your own disclosure accessory view (ImageView) with a white highlighted version. Then you can create a cell like this one I'm using (below). After I alloc one of these cells I then set the backgroundView and accessoryView to my UIImageViews.
#import "ClearBackRoundedTableCell.h"
#implementation ClearBackRoundedTableCell
- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]) {
}
return self;
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
if( [[self.accessoryView class] isSubclassOfClass:[UIImageView class]] )
((UIImageView *)self.accessoryView).highlighted = highlighted;
if( [[self.backgroundView class] isSubclassOfClass:[UIImageView class]] )
((UIImageView *)self.backgroundView).highlighted = highlighted;
self.textLabel.highlighted = highlighted;
}
#end
One note if you go this route: the cells in a grouped table are typically 300 px wide (in portrait mode) but your plain table here would need to be 302 wide to allow for the grey line on each side of the table, which is normally outside of the "content" of the table cell.
After spending a while with color picker, I found out that you need to specify opaque background not for the table view cell xib, but for the Table View where the cells will be located, which is another xib. From what I have seen, table view cell background attributes have no visual effect.
try this one
UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;
It worked for me in grouped tableview.
Make UITableview background as clear color.
Programmatically you can do it like this if your image is added into your resources:
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.opaque = NO;
UIImage *backroundImage = [UIImage imageNamed:#"my_backround"];
UIImageView *backroundImageView = [[UIImageView alloc] initWithImage:backroundImage];
Else you can do it in Interface Builder with this style :
You may need to configure the header files interface from UITableViewController to UIViewController and add <UITableViewDataSource,UITableViewDelegate> ,also don't forget to set the attributes of the tableview to not be opaque and reconnect the tableviews datasource and delegate outlets to the viewcontroller.
I have a UITableView with reorderable rows and I'm using the standard UITableViewCell.text property to display text. When I tap Edit, move a row, tap Done, then tap the row, the built-in UILabel turns completely white (text and background) and opaque, and the blue shade to the cell doesn't show behind it. What gives? Is there something I should be doing that I'm not? I have a hacky fix, but I want the real McCoy.
Here is how to reproduce it:
Starting with the standard "Navigation-Based Application" template in the iPhone OS 2.2.1 SDK:
Open RootViewController.m
Uncomment viewDidLoad, and enable the Edit button:
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
Specify that the table has a few cells:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}
In tableView:cellForRowAtIndexPath:, add a line to set the text property of a cell, and therefore to use the built-in UILabel subview:
// Set up the cell...
cell.text = #"Test";
To enable reordering, uncomment tableView:moveRowAtIndexPath:toIndexPath:. The default implementation is blank, which is fine in this case since the template doesn't include a data model.
Configure the project for the Simulator, OS 2.2.1, Build and Go. When the app comes up, tap Edit, then slide any row to a new position, tap Done, and then tap each row one at a time. Usually a tap will select a row, turn it blue, and turn its text white. But a tap on the row that you just moved does that and leaves the UILabel's background color as white. The result is a confusing white open space with blue strips on the edges. Oddly enough, after the first bogus tap, another tap appears to correct the problem.
So far I have found a hack that fixes it, but I'm not happy with it. It works by ensuring that the built-in UILabel is non-opaque and that it has no background color, immediately upon selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// hacky bugfix: when a row is reordered and then selected, the UILabel displays all crappy
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
for (UIView *view in cell.contentView.subviews) {
if ([[view class] isSubclassOfClass:[UILabel class]]) {
((UILabel *) view).backgroundColor = nil;
view.opaque = NO;
}
}
// regular stuff: only flash the selection, don't leave it blue forever
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
This appears to work, but I don't expect it to be a good idea forever. What is the Right Way to fix this?
This looks like a bug in UITableView's rendering, and you should file a Radar bug report on it. It's like the cells don't get refreshed properly after the move.
One way to work around this for now is to not use the built-in label, but roll your own in the cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
CGRect frame = cell.contentView.bounds;
frame.origin.x = frame.origin.x + 10.0f;
UILabel *textLabel = [[UILabel alloc] initWithFrame:frame];
[textLabel setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
textLabel.tag = 1;
textLabel.textAlignment = UITextAlignmentLeft;
textLabel.backgroundColor = [UIColor clearColor];
textLabel.textColor = [UIColor blackColor];
textLabel.font = [UIFont boldSystemFontOfSize:20.0];
textLabel.numberOfLines = 1;
textLabel.highlightedTextColor = [UIColor whiteColor];
[cell.contentView addSubview:textLabel];
[textLabel release];
}
UILabel *textLabel = (UILabel *)[cell viewWithTag:1];
textLabel.text = #"Test";
return cell;
}
I tried this, and it doesn't exhibit the same sort of white blank rectangle you see with the built-in label. However, adding another non-opaque view to the table cell might not be the best for overall rendering performance.
I don't know how major of a glitch this is, because Apple doesn't want you to persist a selection highlight on a table row (they've been enforcing this lately during the review process). You're supposed to place a checkmark or move on to the next level in the navigation hierarchy with a selection, at which point this white box would only be on the screen for a fraction of a second.
The trick in the solution from Brad appears to be:
textLabel.backgroundColor = [UIColor clearColor];
If you leave the background as the default you still get the problem even when you roll your own cells UITableViewCells.
The reason I left it as the default is because the documentation says it is less computationally costly to use opaque backgrounds. Ideally I wouldn't want to use [UIColor clearColor] to fix this bug.
Maybe a completely custom painted cell would somehow fix it. I haven't tried those before though.
Does anyone else have a solution for this?
Thanks for the info, I was searching how to erase the background color from a UILabel.
I used the following line:
textLabel.backgroundColor = [UIColor clearColor];
and worked perfectly!!!
thanks
Alejandra :)
Selections aren't meant to be shown for extended periods! (We got knocked on this for several of our apps)
??? That means Apple would not approve their own Calendar app on iPhone! When you go to edit the start and end times of the event, the start time is selected indefinitely, it only changes once the user taps to the next field.