iPhone SDK - Highlight the selected cell in TableView - iphone

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;

Related

iOS - UiTableviewCell - Background Image only showing on one cell at a time

I've used UIAppearance to set the background image of my table cells across my app.
[[UITableViewCell appearance] setBackgroundView:[ [UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"list_bg.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]];
However, when I view a list the background image is only set for one of the cells on screen. If I scroll the screen the background image is set on the cell that appears into view but it doesnt set this bg image on any other visible cell.
Anyone got any idea whats going on? I thought that by setting the UIAppearance of UITableviewCell that all instances of this type would automatically get the background image.
Thanks
Brian
Here is my cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"plantCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"PlantListCell" owner:self options:nil];
cell = _plantCell;
self.plantCell = nil;
}
Plant *plant = [self.fetchedResultsController objectAtIndexPath:indexPath];
UIImageView *thumbnail = (UIImageView *)[cell viewWithTag:1];
thumbnail.image = [UIImage imageNamed:[plant.name stringByAppendingString:#"_S"]];
UILabel *label = (UILabel *)[cell viewWithTag:2];
label.text = plant.name;
UIImageView *set = (UIImageView *)[cell viewWithTag:3];
set.image = [UIImage imageNamed:#"set"];
UIImageView *favourite = (UIImageView *)[cell viewWithTag:4];
favourite.image = [UIImage imageNamed:#"fav"];
return cell;
}
Yep. The issue with this is you're telling the UITableViewCell class to set the one UIImageView for each table view cell's background view. Once its set as one, its removed from its previous superview, and added to the new UITableViewCell. You're not getting duplicate UIImageViews set on each tableViewCell; you're getting the one view set on every table view cell.
So then its set and unset from each tableViewCell till the last one it was set on.
Using the appearance proxy for this method sets it on every object of the UITableViewCell.
I wouldn't use the appearance proxy to set the background view method, as whatever view you pass to it will be one view, which can only apply to one cell at a time.
My recommendation is to create a view for each cell independently, or creating a subclass which sets it on initialisation.
Thanks thebarcodeproject. It kind of makes sense alright, but I don't see why Apple bothered to allow us to set the UITableviewCell background image if it doesnt really work.
I went with a simple solution in the end by just setting the image in the cellForRowAtIndexPath method.
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"list_bg.png"]];
It works fine, but not great if you have lots of tables and want to set it in a generic way.
Cheers
Brian
I solved this by creating a base class called CustomizableUITableCell and setting up a ui appearance value for the background image. Then whenever i create a cell that i need with a custom background i simple subclass CustomizableUITableCell.
//Header
#import <UIKit/UIKit.h>
#interface CustomizableUITableCell : UITableViewCell <UIAppearance>
#property (nonatomic, weak) UIColor *backgroundCellColor UI_APPEARANCE_SELECTOR;
#end
//Implementation
#import "CustomizableUITableCell.h"
#implementation CustomizableUITableCell
#synthesize backgroundCellColor;
#pragma mark - Public Methods.
-(void)setBackgroundCellColor:(UIColor *)backgroundColor
{
[super setBackgroundColor:backgroundColor];
}
#end
You can then set it using the appearance proxy.
[[CustomizableUITableCell appearance] setBackgroundCellColor:[UIColor colorWithPatternImage:backgroundImage]];
cell.backgroundView = [[UIImageView alloc]
initWithImage:[ [UIImage imageNamed:#"1.jpg"]
stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
It works for me..

custom selectionStyle color for uitableviewcell

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.

UITableView Cell Label Duplicating

I have a UINavigationController full of UITableViews and I use custom cells in a specific one of these.
In tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath I add a UILabel with [cell addSubview:label];
This works great, however if I then go into the detail view, then return back, it duplicates the label. How can I stop this?
Thanks.
The problem is that you are adding a new label every time the cell gets reused and displayed again.
Since you're using a custom cell already, the easiest solution would be to give the cell a UILabel property, and use that instead of adding a new label each time. Specifically, you should only add a new label if you're creating a cell instead of getting a recycled one. Or, you could add a new UILabel only if the property is nil.
Or as an alternative to adding the label in the custom class, just make sure to add the label inside the conditional cell creation. e.g.
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:ident];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:ident] autorelease];
[cell.contentView addSubview:[[[UILabel alloc] init] autorelease]]; // <-------
}
Add a tag to the label, then check if it exists
UILabel *label;
if ([cell viewWithTag:2]){
label = [cell viewWithTag:2];}
else{
label = [[UILabel alloc]init];label.tag = 2;}

Adding the little arrow to the right side of a cell in an iPhone TableView Cell

This should be simple enough.
I have an iPhone app with a TableView. How do I add the little classic arrow to the righthand side of each cell?
Just set the respective accessoryType property of UITableViewCell.
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
In Swift 3,
cell.accessoryType = .disclosureIndicator
You can do this in Interface Builder. Simply click click on the Table View, go to Prototype Cells on the right side and make it 1. Then click that Prototype Cell and on the right look for Accessory. In the drop-down, click Disclosure Indicator.
You can set little classic arrow like two way as below
1) Using inbuilt accessory type method of UITableViewCell
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
2) Creating own accessory view with own image
(I) Drag & drop an arrow image(i.e. circle_arrow_right.png) in your project
(II) In cell design method for every row as below
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Write below code:
if (cell ==nil) {
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
//For creating custom accessory view with own image
UIImageView *accessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[accessoryView setImage:[UIImage imageNamed:#"circle_arrow_right.png"]];
[cell setAccessoryView:accessoryView];
[accessoryView release];
//Your other components on cells
.............
.............
}
[Note: Choose appropriate images for accessory view and add in your desired cells. Choose small images for accessory views for better performance.]
IN SWIFT 5
Use
cell.accessoryType = .detailButton
for a small Info-button on the right.
cell.accessoryType = .disclosureIndicator
for a small Info-arrow on the right.
cell.accessoryType = .detailDisclosureButton
for a small Info-button and Info-arrow on the right.
infront of your return cell
Swift 3:
cell.accessoryType = .disclosureIndicator
Use the accessoryType property of the cell to show a arrow on the right side. See this.
for simple arrow:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
and for detailed arrow:
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
The other way to do this is to specify UITableViewCellAccessoryDisclosureIndicator when you create the cell. To do this you'll likely alloc init your cell in the tableViewCellWithReuseIdentifier delegate method (then proceed to customize and configure your cell).
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellAccessoryDisclosureIndicator reuseIdentifier:identifier];
Best way is select Disclosure Indicator in Accessory section.

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