iphone sdk how to change customcell colour on selecting the cell? - objective-c

i am using customcell with label. How to change the textcolour of label when the cell selected .By default it is black after selecting that, it needs to be white. How to do that in customcell point. Anyone help me.
Thanks in advance.

Try this code(I presume that you have an access to that label, right?):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
yourLabel.textColor = //Your color;
}

You have to take one integer. when you select the cell assign the row value to that integer and reload the table view. in cellForRowAtIndexPath method out the code like
if(selectedIndex == indexpath.row){
textLabel.textColor = [UIColor whiteColor];
}
selectedIndex is a integer value
try out I am not tested but it may be working

In the custom cell xib .I have connected the label to the customcell.i just changed the text colour and highlighted text colour in the xib .and it is working great.

Related

How to use Autolayout and self-sizing cells in iOS8

I have setup the auto-sizing cells with UILabels with no problem in iOS8 following this tutorial:
http://useyourloaf.com/blog/2014/08/07/self-sizing-table-view-cells.html
But I am having problems setting up a UIImageView using auto-sizing. I need the images to be different sizes in Landscape compared to portrait (so they retain the same aspect ratio). But each time I get a broken layout constraints error
"<NSLayoutConstraint:0x14554680 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x14564440(200)]>
As the actual code has quite a few different types of cell (that all work with auto-sizing cells) I have setup a simple example of the issue with a blue image view here:
https://github.com/AdrianMW/ios8autosizingCells
I have setup the constraints to the superview and am setting up the height like so:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kImageCell forIndexPath:indexPath];
cell.heightConstraint.constant = [self heightForOrientation:self.interfaceOrientation];
return cell;
}
Solutions Tried
I have tried turning off setTranslatesAutosizingMask: on the cell as per some of the other suggestions but when I do I just get a blank space instead of a image. I have also tried adding the following:
[cell setNeedsDisplay];
[cell layoutIfNeeded];
I have seen mention of overriding sizethatfits: to get it to work on this cell using the old way but that doesn't seem to be called on the cell.
Cheers
In your ImageTableViewCell.xib, try changing the constraint:
Image View.Bottom EQUAL Superview.Bottom
Change the priority from 1000 to 999.

UITableView background color won't change

I know this question has been asked before and i've been searching for hours now but I just can't find the answer. I want to change the color of the table that shows when u drag down the table on the absolute top as here:
Picture
The part between the searchbox and the segmented control.
I'm not sure which element it is where I have to change the color. I tried everything I could find over the interface builder (table, searchbar, cell) and it seems like nothing can help.
I read this post about changing the UITableView's background color but that also didn't do the job for me: Post about background colors
I know that when I usually change the color of the underlying view of the table that this background color changes, but not in this table so i thought something is might overlapping.
UPDATE
I figured that this only happens when I add a search bar with search display controller, but I still dont know where to change the background color there.
So maybe someone could tell me which element it is that I'm trying to change.
Thanks in advance :)
Set the background color of the UITableView in ViewDidLoad Method not in XIB.
(void)viewDidLoad {
[super viewDidLoad];
self.myTableView.backgroundColor = [UIColor blueColor];
}
And CellForROwAtIndexPathMethod,mention the Background color of cell and ContentView of Cell to Clear Color.
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
.....
cell.backgroundColor =[UIColor clearColor];
cell.contentView.backgroundColor = [UIColor clearColor];
....
}

How to change UITableViewCell appearance if cellForRowAtIndexPath is not calling

I have a table view in my app. The table view cells has background with pattern image. Table view content is changing and sometimes there are only two or three cells with content info. And table view automatically add other cells to bottom of screen. The problem is the background of these cells is clear but i want to make background same as other cells (with pattern image). Usually i change cells appearance in cellForRowAtIndexPath. But table view don'call this method for cells that created automatically. So, is there any solution?
You could do this in viewDidLoad()
self.searchDisplayController.searchResultsTableView.backgroundColor = [UIColor redColor]; // set your color for tableview here This should color the search results table cells to the color of your preference. Another delegate you could investigate using is: - (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor redColor]; // your color here
}Hope this helps.
Did you try to subclass your tableview cells and set their background in their initializer?

how to toggle the selection color of a UITableViewCell?

I want to modify a cell in a table view; making it highlight blue only when clicked. when released, the blue color should disappear.
the code:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
in the configuration of the cell, will disable the selection completely.
in all, this is mostly needed when i return from the push view controller. i then dont want to see any selected cells in the table.
Any ideas on how to do this?
thanks
When you touch a row in a tableview (if selection is allowed) the row is rendered in the selection style. If you want the "blue" selection colour to disappear, just deselect the row using:
NSIndexPath* rowPath = [NSIndexPath indexPathForRow:5 inSection:2];
[self.tableView deselectRowAtIndexPath:rowPath animated:NO];
Sounds like you're detecting the selection and doing something with the object backing the row and then returning to the same table, so just do the deselection in the same method as you do the action probably in:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
You can set a cell background color when the user clicks and set it back to white when the user releases.
That code should be useful to solve the 2nd problem
self.clearsSelectionOnViewWillAppear = NO;

UITableView row height not drawing taller with text

I'm trying to mimic the address cell in Address Book / Contacts App.
I want a cell with style UITableViewCellStyleValue2 to display an address such as:
// 123 Fake St.
// Suite 555
// Denver CO, 80000
But actual cell isn't drawing the way I want. The label is drawing outside the bounds of the tableview row and part of top of the label is being cut off by the row directly above it.
Here's what I have so far:
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.autoresizingMask = UIViewAutoresizingFlexibleHeight; //doesn't seem to do anything
self.tableView.rowHeight = 88.0f; // default is 44.0
cell.detailTextLabel.numberOfLines = 0; //unlimited lines
An NSLog statement confirmed the rowHeight is set to 88.0.
I realize a delegate method exists to change the row height. However, in my current app I only need this one cell to expand with the text, so I didn't feel it necessary to implement the delegate method.
Thank you for your help.
I don't think that setting row height this way (self.tableView.rowHeight = 88.0f;) for a single row will work. You should implement the delegate method which in your case is very simple, i.e.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return ([indexPath row] == MY_TALLER_CELL_ROW) ? 88.0f : 44.0f;
}
I think you'll probably have more luck not using the cell's default textLabel and detailTextLabel views; instead, create your own labels and add them to the cell's content view.