Change Color On Selection - TableView/SplitView - iphone

I have a UITableView for my MasterView of SplitView app.
On selecting any row, the color of that row becomes blue. I want to change it to black or dark grey color. How can I do that?
Thanks.

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *tableCell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (tableCell == nil) {
tableCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease] ;
[tableCell setSelectionStyle:UITableViewCellSelectionStyleGray];
}
}
Or you can also try this on cellForRowAtIndexPath
if (indexPath.row == selectedIndex)
{
Cell.LblDynamicCell.textColor=[UIColor whiteColor];
Cell.LblBackView.backgroundColor=[UIColor Black];
}
//LblDynamicCell,LblBackView objects declare in cellViewController class
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedIndex = indexPath.row;
[self.ResultTbl reloadData];
}

Following enum is defined for selection style -
typedef enum {
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle;
So for your purpose you can use - UITableViewCellSelectionStyleGray

You can change the color of selected cell as follows:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
UIImageView *theImageView = [[UIImageView alloc] initWithImage:[UIImage imageName:#"table_selected_cell.png"]];
self.selectedBackgroundView = theImageView;
[theImageView release];
}
to change the color of your choice, where table_selected_cell.png is a 2px image of your color.

Related

How to change custom cell image and label font color when selected in uitableview?

I have a custom cell with imageview and label.When user selected a particular cell in the tableview I want change its image and the label font color.How can I do this ?I want to avoid that default way of cell selection(highlight cell in blue color) and use above method instead of that.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCell *selectedCell = (MyCustomCell *)[tableView cellForRowAtIndexPath:indexPath];
//selectedCell.textLabel.textColor = <#your color#>
//selectedCell.myImage = <#your image>
}
if you want changed selected cell backgroundColor, try to this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellId = #"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor redColor]];
[cell setSelectedBackgroundView:bgColorView];
}
}

UITableViewCell background view/image

Where do I set backgroundImage of tableview cell in cellForRowAt.. And how do I do that ? This is how I did it and it kinda works, but I want to know is that right method for setting backgroundimage (cell.backgroundView) and is it right place inside if(cell==nil) or outside that "if".?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"RootViewCellIdentifier";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image1.png"]];
}
return cell;
}
Used to do that, but nowadays I prefer willDisplayCell:
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor redColor];
}
Documentation says
A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out.
When using cellForRowAtIndexPath, there were some weird special cases, where cell customization just didn't work fully as I wanted. No such problems with willDisplayCell.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[cell.imageview setImage:[UIImage imageNamed:#"image1.png"]];
}
return cell;
}
try with this might be helpful...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"RootViewCellIdentifier";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
UIImageView *backimg = [[UIImageView alloc] init];
backimg.frame = CGRectMake(0, 0, 320, 44);
backimg.tag = 20;
[cell.contentView addSubview:backimg];
}
UIImageView *imag = (UIImageView *)[cell.contentView viewWithTag:20];
[imag setImage:[UIImage imageNamed:#"sky.png"]];
return cell;
}
The best place to set a background view for a cell is the tableView:willDisplayCell:forRowAtIndexPath: method.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image1.png"]];
}

UITableViewCell with semi transparent selected background

I need to hide the normal (unselected - cell.backgroundView) image of a cell when the cell is selected and show it when it is not selected.
The way the tableview works is that the normal view (cell.backgroundView) is always there and when the cell is selected it animates the selected image (cell.selectedBackgroundView) into view and places on top of the normal view.
The problem is when the selected cell is semitransparent and the normal cell is always visible underneath it.
I created, in 2 views for my (custom) UITableViewCell which I load in my view controller:
-(void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"XYCell"]];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"XYCellSelected"]];
}
I cleared colors from in the required places but I cannot get it to work as I want.
Since my selected images (cell.selectedBackgroundView ) is semitransparent, the cell.backgroundView can still be seen underneath it.
How can I make it go away?
In general, if you want a custom cell you should implement your own uitableviewcell.
In your case take a look at
- (void)setSelected:(BOOL)selected animated:(BOOL)animated;
Code example to help you :
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
//your own backgroundview when selected
self.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"selectedBck.png"]];
if (selected){
// edit the cell's view when it's selected
self.backgroundView = nil;
}
else {
// edit the cell's view when it isn't selected
}
}
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
self.highlightIndexPath = indexPath; // for iOS6
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundView.hidden = YES;
}
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (indexPath.row == NSNotFound) {
// for iOS6
cell = [tableView cellForRowAtIndexPath:self.highlightIndexPath];
}
cell.backgroundView.hidden = NO;
}

uitableviewcell alternating background cell colors even for cells without data

I know how to set alternating colors to a tableviewcell, but how can I set all the visible rows to have an alternating color? For instance, right now if I only have 2 cells with data, only 2 cells will have background colors - how do I fill the backgrounds of the empty cells too?
try doing some thing like this:
(a)Add only required number of dummy rows
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ([UIAppDelegate.getwaitlistArray count]>=numberOfVisibleRowsOnYourScreen) {
return [UIAppDelegate.getwaitlistArray count];
}
else{
return ([UIAppDelegate.getwaitlistArray count] + numberOfVisibleRowsOnYourScreen - [UIAppDelegate.getwaitlistArray count]);
}
}
(b)Alternare your cell:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
//alternating cell back ground color
if (indexPath.row%2 == 0) {
[cell setBackgroundColor:[UIColor colorWithRed:237.0/255.0f green:237.0/255.0f blue:237.0/255.0f alpha:1.0f]];
}else
{
[cell setBackgroundColor:[UIColor colorWithRed:247.0/255.0f green:247.0/255.0f blue:247.0/255.0f alpha:1.0f]];
}
}
(c)in cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MyIdentifier"] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
//Create your lables/buttons/text/image
nameLabel.test=#"Whatever"; ..... ......
......
if (indexPath.row < [YourArray count]) { Populate your data here in the cells } else { cell.userInteractionEnabled=FALSE; nameLabel.text=#""; } return cell; }
Add dummy cells to the end of your list.

table view containg image view

i ve got an imageview ..below the imageview is a table view..which loads the data from server.by default five fields in table view are visible ..works fine..if i m to scroll the table view to view the remaining fields..the scroller rebounces..wat i want is the table view to be fixed and not to rebounce when scrolled ..i d be so greatful if u guys cud help me out..below is the code..
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [items count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 28.0;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,
tableViewteam.bounds.size.width, 28)] autorelease];
headerView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"team"ofType:#"png"]]];
return headerView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.imageView.backgroundColor=[UIColor clearColor];
cell.textLabel.text=[[items objectAtIndex:indexPath.row]objectForKey:#"title"];
cell.textLabel.textColor=[UIColor whiteColor];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
I didn't fully understand your problem. If you want to stop bouncing the table view you can add
yourTableView.bounces = NO;
in ViewDidLoad()
If you need your tableView to be fixed you can disable scrolling.
tableView.scrollEnabled = NO;