How to change UITableViewCell appearance if cellForRowAtIndexPath is not calling - objective-c

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?

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];
....
}

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

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.

UITableViewCell Align all subviews

I have a custom UITableViewCell which has some subviews. i.e. some labels, some images. I want all these subviews to be right aligned in the UITable. How do i do that?
I have tried these approaches -
1.In InterfaceBuilder when I select the UITableViewCell, I can set the "indentaion" section. It's by default 0, I made it 100 but I see no change in the device.
2.I have tried this in the code too. Override the default method...
(NSInteger)tableView:(UITableView *)tableView
indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
The above code also does not work. How do I align all my subviews in my UITableViewCell to right?
Basically, I want to display one cell left aligned (which is default) & some cells right aligned. As shown in the picture.
There is automatic way everything will align to right. You will have to lay the view appropriately. For UITextField and UILabel objects you can set the textAlignment property to UITextAlignmentRight.
label.textAlignment = UITextAlignmentRight;
If necessary you must also adjust the autoresizingMask of the views to set it such that it has a UIViewAutoresizingFlexibleLeftMargin mask set so that they stick to the right on view resize.
use interface builder for alignment.

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;