views:

575

answers:

5

hey all,

i've got an annoying problem in my tabelview..i was able to achive color-changing cells (blue/white/blue/...), but now i'm in trouble with my text which has a white background on the blue cells.

i've tried for testing to set a background color to red

    // try to set the backgroundcolor of the text ???
cell.textLabel.text.backgroundColor = [UIColor redColor];

which doesn't work..hmpf

please have a look at my code below, can anybody tell me what's wrong and how can i solve my problem giving the texts a transparent background?


// Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

    // Set up the cell
cell.textLabel.text=[RssFeedNodes objectAtIndex:indexPath.row];

    // try to set the backgroundcolor of the text ???
cell.textLabel.text.backgroundColor = [UIColor redColor];


// show image in cell
NSString *imageName=@"rss.png";
cell.imageView.image=[UIImage imageNamed:imageName];

// changing colors in cells
NSInteger row = [indexPath row];
if (row % 2) cell.contentView.backgroundColor = [UIColor whiteColor];
else cell.contentView.backgroundColor = [UIColor colorWithRed:0.90f green:0.95f blue:1.0f alpha:1.0f];

return cell;

}

A: 

text has no background, but textLabel has. so

[[cell textLabel] setBackground:[UIColor redColor]];
Morion
thanks, i've already tried this, similar to your solution:cell.textLabel.backgroundColor = [UIColor redColor];your suggestion:[[cell textLabel] setBackgroundColor:[UIColor redColor]]; doesn't work in my code, toono background-color for my textlabels yet :-(
michbeck
A: 

You'll need to add your own UILabel label onto the cell, and set the background colour of that to transparent. For some reason the label that a table cell has does not have a settable background colour.

Something like:

UILabel* label = [[UILabel alloc] init];
label.frame = CGRectMake( 20, 10, 200, 22 );
label.backgroundColor = [UIColor clearColor];
label.opaque = NO;        
label.text = @"your text here:";
[cell addSubview:label];
[label release];

In this example I've set the frame for the label fairly arbitrarily (well not, actually, this was modified from some of my own real code). You may need to be more dynamic with the sizing, in which case you'll probably need to subclass the cell and override setFrame to keep the label's frame in sync. But hardcoded values should get you going for now.

Phil Nash
thanx phil, could you show me how to do that in my example?i've got rather beginner difficulities in objective c
michbeck
And once you're using your own UITextField, you can probably get by just setting its background color to [UIColor clearColor] (ie, no color at all)
wkw
example added. I also realised that, in my haste earlier, I referred to UITextField, when I should have been talking about a UILabel
Phil Nash
hey phil, morion and wkw, thanx a lot for your help, now it worls with the Uilabel :-)
michbeck
michbeck - glad it helped. In which case it's only polite for your to upvote the helpful responses :-)
Phil Nash
A: 
#define LABEL_TAG 99
    // whatever your label rect size should be... change as appropriate
    UIlabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300.0, 30.0)]; 
    label.tag = LABEL_TAG;
    // set up alignment, font, autoresizemask, etc.
    label.backgroundColor = [UIColor clearColor];
    label.opaque = NO;
    [cell.contentView addSubview:label];
    [label release];

Something to watch out for is that you're not adding your own label to a reuseable table cell more than once. By setting a known view.tag property you can get at (or discover existence of) the UILabel view. [cell viewWithTag:LABEL_TAG]

Whenever you dequeue a reuseable cell, first get the reference to your label view, then do what you would normally do if you were using the UITableCell's textLabel.

wkw
A: 

Your answer is found (and described very well) here: http://undefinedvalue.com/2009/11/02/easy-gradient-backgrounds-uitextviewcells

My brief summary of the solution: Subclass the UITableViewCell, and then use your subclass when creating instances in cellForRowAtIndexPath.

You then need to override just one method (setSelected) in your subclass UITableViewCell:

  • (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state for (UIView *view in self.contentView.subviews) { view.backgroundColor = [UIColor clearColor]; } }

The reason appears to be that the built-in UITableViewCell class will set the label background to white (or selected color) when being displayed based on the selection state in the table in the setSelected method. Substitute your own, call the base class implementation, then set your subview backgrounds to clear in order to let your contentView background shine through.

Bill
A: 

Hi Guys, While I am showing a semi-transparent view and label inside it as transparent, the text becomes transparent too. view.alpha = 0.5 is it possible to set the text not transparent, i.e. something like label.text.alpha = 1.0?

here is my code:

updatingView = [[UIView alloc] initWithFrame: frame];

UIActivityIndicatorView *busySymbol = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
busySymbol.frame = CGRectMake(busySymbol.frame.origin.x + 10, (frame.size.height - busySymbol.frame.size.height)/2, busySymbol.frame.size.width, busySymbol.frame.size.height);
[busySymbol startAnimating];
[updatingView addSubview:busySymbol];

double labelHeight = 20;
UILabel *labelUpdating = [[UILabel alloc] initWithFrame:CGRectMake(50, (frame.size.height - labelHeight)/2, 100, labelHeight)];
labelUpdating.text = NSLocalizedString (@"Updating ... ", @"Updating text label");
labelUpdating.textColor = [UIColor whiteColor];
labelUpdating.backgroundColor = [UIColor clearColor];
[updatingView addSubview:labelUpdating];


[NSTimer scheduledTimerWithTimeInterval:1.0 target:self  selector:@selector(updateTimerFinished) userInfo:nil repeats:NO];
updatingView.backgroundColor = [UIColor blackColor];
updatingView.layer.cornerRadius = 8;
updatingView.alpha = 0.5;
[self.view addSubview:updatingView];
karim