UITableView background color won't change - objective-c

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

Related

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?

iOS - viewForFooterInSection sticking to bottom of UITableView

I have a UITableView with 3 sections. Each of them have a footer that I've added using viewForFooterInSection. The problem I'm having is that when I scroll the tableview down, the footer sticks to the bottom of the screen, and doesn't scroll like the rest of the cells. Does anyone know how to make it so the footer almost acts like a cell, and scrolls along with the rest of the table? Thanks!
I actually figured it out. Probably isn't the smartest way to do it, but I changed my UITableView style to grouped, and that fixed it. I had to tweak the TableView a bit so the cells would look the same as they did non-grouped (clear background, no separators), but it worked fine. The footer no longer sticks to the bottom of the TableView.
Footers are supposed to stick. The trick is to add an extra cell to each section and render the footer there. If you need more help, add a comment, but it should be pretty straightforward.
EDITS:
Q: Alright. I'm using Core Data and NSFetchedResultsController, to populate the TableView. Would that make it more tricky to accomplish this?
A: Not at all. Override
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
To add an extra cell in each section, and in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
test if the indexPath.row > than your fetchedResultsController's rows for that section. If true, add in your cell that shows the footer information.
One way around this is if you set the footer as one of the cells as the last cell in the scroll view (could be done but setting it as the last item in the array that you set the uitable from)
Adding an extra cell, making it invisible, and rendering your view there is not an advisable way of adding a footer. Doing it properly is pretty straight-forward:
- (UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section {
NSString* sectionFooter = [self tableView:tableView titleForFooterInSection:section];
UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, yourWidth, yourHeight)]; //create a view- the width should usually be the width of the screen
UILabel* label = [[UILabel alloc] initWithFrame:someFrame];
label.backgroundColor = [UIColor blueColor];
label.textColor = [UIColor whiteColor];
label.text = sectionFooter;
[view addSubview:label];
return view;
}
You will also have to implement tableView: titleForFooterInSection: if you want to add text like I have here.
I did had similar problem when I found out that my code did not work with Landscape mode, and only worked in Portrait mode. In my old code, when in Landscape the tableview can not scroll lower than visible view and it bounced back to top row when scroll (not letting me see the lower rows). All I have to change is to make sure that the height set to 44 as default cell height. So my footer is basically another cell with clearColor. Note my app uses 'AutoLayout'
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
CGRect screenBounds = [UIScreen mainScreen].bounds;
CGFloat width = screenBounds.size.width;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 44)];
view.backgroundColor = [UIColor clearColor];
UILabel *label = [[UILabel alloc] initWithFrame:view.frame];
label.text = #"Your Text";
[view addSubview:label];
return view;
}

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.

Maintain UITableViewCell background color when going into edit mode

I have set background colors for all my UITableViewCells. However, when I click my UIBarButtonItem "edit", the delete and the draggable icons distort the background color, making white background behind them. Is there any way around this? I can show code if necessary, but this seems like a pretty straightforward question.
The background color of a table cell is usually set like this:
cell.backgroundView.backgroundColor = [UIColor redColor];
This works fine. However, when an object of class UITableViewCell is created, by default it does not have a backgroundView, it is nil. The developer needs to create the backgroundView when needed. So depending on the particular situation you need to do something like:
if (cell.backgroundView == nil) {
cell.backgroundView = [[[UIView alloc] init] autorelease];
}
cell.backgroundView.backgroundColor = [UIColor redColor];
This is the default behavior of the uitableviewcells in the editing mode. Try setting the background color for your tableViewCells again in
- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
if(editing)
{
// set background color
}
else {
}
If needed, try setting your background color as your property so that you can set it up here.

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.