custom selectionStyle color for uitableviewcell - iphone

I have been using the default colors Grey and Blue for cell selection, but I would like to try a bright orange, however I am not sure how to do custom UITableview selections.
this is the code I am using at the moment..
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
I have also tried
[cell setSelectionStyle:[UIColor orangeColor]];
but I get a warning saying setSelectionStyle is not an integer something.

You're getting that error because the program is expecting a UITableViewCellSelectionStyle where you have given it a UIColor. I don't know if there is way to do this programatically, but you could try and use the cell's selectedBackgroundView property to use your own image:
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourImageHere.png"]] autorelease];
This would go in your cellForRowAtIndexPath method.

Related

iPhone SDK - Highlight the selected cell in TableView

I have a customTableViewCell in a tableView. I am highlighting the cell when selected using,
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
But, the tableView appears like the following image.
It hides the labels and text in the customCell. I just want to highlight the cell simply without hiding the background image and labels. I do not know where I am making the mistake, whether in code or IB. .
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.Your_imageView.highlightedImage = [UIImage imageNamed:#"Your_image_Name.png"];
this will work fine
Hey i think you need to add labels and texts in the customCell to cell.contentview. For reference go to Apple developer document.
http://developer.apple.com/library/ios/#DOCUMENTATION/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7
[_tableView deselectRowAtIndexPath:selectedIndexPath animated:YES];
put this in to view Will Appear and its works for me ,
NSIndexPath *selectedIndexPath;
also put this to .h file too
Try this
UIView *viewSelected = [[[UIView alloc] init] autorelease];
viewSelected.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:#"cell_highlighted.PNG"]];
cell.selectedBackgroundView = viewSelected;

UIImageView simply does not appear in custom UITableViewCell

I have a table cell to which I'm adding subviews programmatically. All the textual subviews work fine, but I can't get an image subview working at all.
You'll notice that I set the background color to black. This is simply to indicate to me that the subview is indeed being initialized and positioned properly within the cell. When I remove the background color there is nothing there.
Also, the cell style is UITableViewCellStyleDefault but I don't think that's pertinent for custom subviews. I want the image positioned on the right, which is why I'm not using the standard imageView property that cells offer.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// ... add textual views ...
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"clock.png"]];
img.frame = CGRectMake(271.0f, 10.0f, 19.0f, 22.0f);
img.backgroundColor = [UIColor blackColor];
[cell addSubview:img];
}
// ... more code ...
return cell;
}
Your code looks fine to an extent. One issue I had when I was creating my own UITableViewCells in code was I was creating the cells with variables like you are but you shouldn't do this.
What you should do is create the cell and then set the variables. If cells are different you should use different reuse identifiers. Hope this makes sense if not let me know and I'll update.

UILabel subview in UITableViewCell isn't showing up. (iPhone Dev)

I have a cell in a table view that has a UILabel as a subview, when I set the text of the label, it is never shown on the cell. I'm somewhat a noob in iPhone development and am not sure what I'm doing wrong...
I am creating and returning the following cell in my table view's cellForRowAtIndexPath delegate:
cell = [[[ActivityCell alloc] initWithFrame:
CGRectZero reuseIdentifier:ColumnedCell] autorelease];
CGRect frame = CGRectMake(180.0, 11.0, 130.0, 22);
UILabel *valueField = [[UILabel alloc] initWithFrame:frame];
[valueField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
valueField.tag = 111;
valueField.textAlignment = UITextAlignmentRight;
valueField.textColor = [UIColor colorFromHex:#"326799"];
valueField.highlightedTextColor = [UIColor whiteColor];
valueField.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
valueField.adjustsFontSizeToFitWidth = YES;
[cell.contentView addSubview:valueField];
[valueField release];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
I then want to set the UILabel (subview) later by doing this (I currently have this code in viewDidAppear):
ActivityCell *cell = (ActivityCell*)[formDetailsTableView cellForRowAtIndexPath:
[NSIndexPath indexPathForRow:0 inSection:1]];
UITextField *valueField = (UITextField *)[cell viewWithTag:111];
valueField.text = #"foo";
But the word "foo" never shows up in the UILabel. I believe it shows up when I compile with <= 2.2 SDK, but I want to get this working with 3.0+
Any ideas of why the text isn't showing up?
FOLLOW UP:
Please take a look at this question as a follow up to this: UITableViewCell's textLabel is overlapping a subview label, how can I fix it? (iPhone Dev)
Well first off, you're creating a UILabel, tagging it as 111, and then adding it as a subview to the cell's contentView. Later on, you are trying to retrieve that UILabel but you're casting it to a UITextField. Fundamentally something you're doing here is wrong.
Here's what I recommend doing:
Take a look at Apple's guide for using Interface Builder to create cells. It's going to lead to a lot less code and you'll be able to easily see what's going on, and won't have to add the Label/TextField programatically.
Instead of trying to access cells directly in viewDidAppear and changing their subviews, you should be doing all of that display logic inside of cellForRowAtIndexPath. In another method (for example, one called when the user touches a button), you'll modify some data structure and then send the tableView the reloadData message. At that time, cellForRowAtIndexPath is called again and the values set in your data structure determines how the cell is created.
It looks like you have a small bug:
change
UITextField *valueField = (UITextField *)[cell viewWithTag:111];
to
UITextField *valueField = (UITextField *)[cell.contentView viewWithTag:111];
My guess is that the cell coming back is nil and thus not updating your text. I would start buy making sure you are getting a cell back from this line:
ActivityCell *cell = (ActivityCell*)[formDetailsTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
cell = [[[ActivityCell alloc] initWithFrame:
CGRectZero reuseIdentifier:ColumnedCell] autorelease];
your cell has a frame of CGRectZero

Need to change the color of table view selection

I need to change the default blue color selection of table view to some custom color. Is there any way to do that. Help me
The best way to do this is like this:
// 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];
cell.selectedBackgroundView = v;
}
// Set up the cell...
cell.textLabel.text = #"foo";
return cell;
}
The relevant part for you is the cell.selectedBackgroundView = v; instruction.
You can substitute the very basic view 'v' here with any view you like.
I also came across this problem with trying to create custom selected cell views for the grouped cell. I solved this problem by creating 3 types of images for the cell top, middle and bottom assuming that there will be a middle cell.
NSString *imageFile;
if (row == 0) {
imageFile = #"highlighted_cell_top.png";
} else if (row == ([registeredDetailsKeys count] - 1)) {
imageFile = #"highlighted_cell_bottom.png";
} else {
imageFile = #"highlighted_cell_middle.png";
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageFile]];
cell.selectedBackgroundView = imageView;
There is possibly an easier way but I am happy with the result.
I do not think you can use a custom color. However, you can use the following property of UITableViewCell
#property(nonatomic) UITableViewCellSelectionStyle selectionStyle
The selection style is a backgroundView constant that determines the color of a cell when it is selected. The default value is UITableViewCellSelectionStyleBlue. Since
typedef enum {
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle;
you can switch from the default blue to gray, or no colored selection at all.
Make sure that after you declare in the header
#property(nonatomic) UITableViewCellSelectionStyle selectionStyle
Implement
cell.selectionStyle = UITableViewCellSelectionStyleGray
where UITableViewCellSelectionStyleGray can be UITableViewCellSelectionStyleNone, UITableViewCellSelectionStyleBlue, UITableViewCellSelectionStyleGray.
Another way to do it would be to move in a new view over your cell, with whatever color you'd like and 50% or so opacity. You move this view over to the cell when you get a -setSelected:animated: call. When I say move, you actually could always have a view on top of your cell, but just turn the hidden bit off and on as you need.

Cocoa-Touch: How to: custom UITableViewCell with UIColor clearColor background

I have a UITableView with custom cells.
I have MyTableViewCell : UITableViewCell and MyTableViewCellContentView : UIView classes.
What I'm doing is basically what is done in the AdvancedTableViewCells demo app from Apple with a slight change, on some rows I want to use a clearColor background to show the table's background behind the painted text.
So in MyTableView's cellForRowAtIndexPath I'm doing:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MyCell";
MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.someValue = indexPath.section;
[cell finishedSetup];
return cell;
}
And in my MyTableViewCell's finishedSetup:
cellContentView = [[MyTableViewCellContentView alloc] initWithFrame:CGRectMake(0, 0, 320, 80) cell:self];
cellContentView.autoresizingMask = UIViewAutoresizingNone;
cellContentView.contentMode = UIViewContentModeRedraw;
[self.contentView addSubview:cellContentView];
And in MyTableViewCellContentView I implement the drawRect method. And plan to not use any subviews but draw my custom content just as the Apple example does in the CompositeSubviewBasedApplication.
The problem is that for a few sections I want to use a clearColor backgroundColor. This works, until a cell with a non-clearColor backgroundColor is reused to draw a clearColor cell, at which time the background is not cleared and will still have the old color.
How can I make the background redraw?
Edit:
I forgot to mention, I'm setting the background color in MyTableViewCellContentView's init after calling super's init. Setting it via:
self.backgroundColor = [UIColor clearColor];
I've verified that this in fact does get called and is called as expected with clearColor or redColor.
I've also tried setting the table cell's background color, it didn't help.
Edit #2: Here's my drawRect method:
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
static int i = 0;
[[NSString stringWithFormat:#"Cell %d", ++i] drawAtPoint:CGPointMake(3, 3) withFont:[UIFont boldSystemFontOfSize:16]];
}
To make the background color setting take effect you need to do the setting in tableView:willDisplayCell:forRowAtIndexPath - the OS will not alter anything you set here. The reason is that some additional setup of the cell gets done by the OS after you return it from tableView:cellForRowAtIndexPath. If you can, get a look at session 101 from WWDC 09 presented by Jason Beaver.
I haven't found out why it was happening. But I've tried a different strategy. I'm no longer reusing table cells, since I don't have that many of them.
Instead I'm creating all cells at startup and actually get a boost in performance when showing them since this way there is no additional setup needed when scrolling.