Background of View inside UITableView which is inside UITableViewCell - objective-c

I have a UITableView inside UITableViewCell. The inner tableview is of size 5 with each cell contentview of different background color and is editable.I'm able to reorder the tableviewcells of inner tableview.
I have a custom backgroundview for outer UITableViewCell.
UIView *selectedBackgroundView = [[UIView alloc] init];
selectedBackgroundView.layer.borderColor = [[UIColor grayColor] CGColor];
selectedBackgroundView.layer.borderWidth = 1;
selectedBackgroundView.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = selectedBackgroundView;
When I select the tableviewcell, the bg color of tableviewcells of inner tableview changes to white.
//CellForRow code of Parent TableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
TableViewCellCustom *cell = (TableViewCellCustom *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"TableViewCellCustom" owner:self options:nil];
cell = _tableViewCellCustom;
UIView *selectedBackgroundView = [[UIView alloc] init];
selectedBackgroundView.layer.borderColor = [[UIColor grayColor] CGColor];
selectedBackgroundView.layer.borderWidth = 1;
selectedBackgroundView.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = selectedBackgroundView;
}
CustomPosition *customPosition = [_filteredArray objectAtIndex:indexPath.row];
[cell setProductsArray: customPosition.productsArray];//Sets the products and reloads child tableview
return cell;
}
//CellForRow of Child Tableview
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
TableViewCellProduct *cell = (TableViewCellProduct *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"TableViewCellProduct" owner:self options:nil];
cell = _tableViewCellProduct;
cell.viewProduct.transform = CGAffineTransformRotate(cell.viewProduct.transform, M_PI_2);
}
switch (indexPath.row) {
case 0:
cell.viewProduct.backgroundColor = [UIColor redColor];
break;
case 1:
cell.viewProduct.backgroundColor = [UIColor greenColor];
break;
case 2:
cell.viewProduct.backgroundColor = [UIColor blueColor];
break;
case 3:
cell.viewProduct.backgroundColor = [UIColor yellowColor];
break;
case 4:
cell.viewProduct.backgroundColor = [UIColor grayColor];
break;
default:
break;
}
Product *product = [_productsArray objectAtIndex:indexPath.row];
cell.lblProductName.text = product.name;
return cell;
}
Child tableview is rotated because i wanted horizontal tableview.
Anyone knows what is the solution for this?

When a table view cell is selected the framework goes through all subviews and changes thier background colour to UIColor clearColor. This is so that the background colour of the table view cell can be seen properly. Usually this is what you want.
If it isn't what you want, you need to subclass UITableViewCell so that, after selection, you can restore the background colour of all of the subviews. Or manage the result of the selection entirely yourself.

Related

UiTableView on iPhone showing only first row on iOS 10.0

I am experimenting a weird behavior with the tableView on iPhone only.
The problem is that the UITableView which updates automatically while loading data would show on iPhone only the first row and as I scroll down the other rows start appearing, This behavior started with iOS 10.0 and only on iPhone. On iPad it works correctly.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
// Return the number of rows in the section.
return [searchResultsArray count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[[NSBundle mainBundle] loadNibNamed:#"searchResultCell" owner:self options:nil];
}
else {
[[NSBundle mainBundle] loadNibNamed:#"searchResultCell~iphone" owner:self options:nil];
}
cell = referenceCell;
self.referenceCell = nil;
self.referenceCell.tag = indexPath.row;
}
NSMutableDictionary *referenceDict = [searchResultsArray objectAtIndex:indexPath.row];
UILabel *label = (UILabel *)[cell viewWithTag:3];
label.text = [referenceDict objectForKey:#"TEXT"];
UIView *cellView = (UIView*)[cell viewWithTag:5];
BOOL colorBack = [[referenceDict objectForKey:#"COLOR"]intValue];
if (colorBack) {
cellView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed: #"light gray.png"]];
}
else {
cellView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed: #"sepia.png"]];
}
cell.textLabel.highlightedTextColor = [UIColor blueColor];
return cell;
}
Test threads in which the code is running inside cellForRowAtIndexPath. I've had similar problems in this method in iOS10 while work with view layer

How to add uiview in uitableviewcell

Hope all of you'll be fine.
I have a little issue in my application. I have a tableview with custom-cell. In my custom-cell class i made a uiview
UIView* cellView = [[UIView alloc] initWithFrame:self.contentView.frame];
cellView.backgroundColor = [UIColor grayColor];
[self.contentView addSubview:cellView];
Problem is uiview is not fitting in my cell frame properly. Cellforrow is as :
- (SummaryViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cell";
SummaryViewCell *cell = (SummaryViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
{
cell = [[SummaryViewCell alloc] init];
}
cell.backgroundColor = [UIColor blueColor];
return cell;
}
I have tried initWithFrame:self.contentView.frame] but same problem happened (uiview appear in cell but cover only half of the cell), I also have tried cellView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.frame.size.width and same as for height) but failed. Need your valuable advise and ideas. Many Thanks.
Where did you creating subview?
Try to do it in -layoutSubviews method.
- (void)layoutSubviews {
[super layoutSubviews];
UIView* cellView = [[UIView alloc] initWithFrame:self.contentView.frame];
cellView.backgroundColor = [UIColor grayColor];
[self.contentView addSubview:cellView];
}
All frames for subviews are already calculated inside this method and you should receive correct width\height for your subview.
Can you try setting clip to bounds
[cellView setClipsToBounds:YES];

How to set separator of one section of tableView to clear

I have four sections in my tableView, and I want only the last section to have a clear separator. I know how to do this for the whole tableView, but cannot find any solution for my question. The only solution that I have thought of is to create the cell as an image in Photoshop and set that image as the cell background. Does anyone have an idea of how to do this without having to use Photoshop?
Try this one
In View didLoad
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
And
-(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];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 1)];
view.backgroundColor = [UIColor greenColor];
view.tag = 101;
[cell addSubview:view];
}
if (indexPath.section == 0)
{
[[cell viewWithTag:101] setHidden:YES];
}
else if(indexPath.section == 1)
{
[[cell viewWithTag:101] setHidden:NO];
}
return cell;
}

is there a way to show Uitableviewcell selection on limited frame?

i have a uitableview cell added some labels something like this
- (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];
UIImage* myImage = [UIImage imageNamed:#"AccessoryForDealsMain.png"];
UIImageView *MyimageView = [[UIImageView alloc] initWithImage:myImage];
MyimageView.contentMode=UIViewContentModeScaleToFill;
cell.accessoryView=MyimageView;
[MyimageView release];
UILabel *BluePatti = [[UILabel alloc] init];
BluePatti.frame=CGRectMake(0,0,4,44);
BluePatti.backgroundColor = BLUEPATTI;//[UIColor blueColor];
UILabel *BlackLine = [[UILabel alloc] init];
BlackLine.frame=CGRectMake(3,0,1, 45);
BlackLine.backgroundColor = BLACK_LINE;
[BluePatti addSubview:BlackLine];
[BlackLine release];
[cell.contentView addSubview:BluePatti];
[BluePatti release];
UILabel *Seperator = [[UILabel alloc] initWithFrame:CGRectZero];
Seperator.backgroundColor = [UIColor colorWithRed:234/256.0 green:234/256.0 blue:234/256.0 alpha:1.0]; //[UIColor lightGrayColor];
Seperator.frame = CGRectMake(4,43,316,1);
[cell.contentView addSubview:Seperator];
[Seperator release];
}
if(indexPath.section==5)
{
cell.textLabel.text=[self.GenderCellData objectAtIndex:indexPath.row];
cell.textLabel.textColor=CELL_TEXT_COLOR;
cell.textLabel.font=CELL_FONT;
}
else
{
cell.textLabel.text=#"Cells";
cell.textLabel.font=CELL_FONT;
}
return cell;
}
now when i touch cell it shows blue selection above all subviews.How do i change selection frame and colour?
You can create a custom selectedBackgroundView in UITableViewCell that has your own frame and color. This will be shown when the cell is selected instead of the default blue background.
For example:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 20, 30, 40)];
view.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = view;
[view release];
I face the same problem today , and I solved via this code
First , Inside my custom cell init
self.customerbgImageview = [UIImageView new];
[self.customerbgImageview setFrame:CGRectMake(0 , 11, Width, 66)];
[self.customerbgImageview setBackgroundColor:UIColor_RGB(0, 255, 255, 0.5)];
[self addSubview:self.customerbgImageview];
Second set selectionStyle none
cell.selectionStyle = UITableViewCellSelectionStyleNone;
third in the table method
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
CustomOrderTableViewCell *cell = (CustomOrderTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
[cell.customerbgImageview setHidden:NO];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
CustomOrderTableViewCell *cell = (CustomOrderTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
[cell.customerbgImageview setHidden:YES];
}

Cell background colour only shows up to the text

I have gone as far as to use the following code to set the background colour of a TableView cell (in cellForRowAtIndexPath)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.backgroundColor = [UIColor yellowColor];
cell.contentView.backgroundColor = [UIColor yellowColor];
UIView* backgroundView = [ [ [ UIView alloc ] initWithFrame:CGRectZero ] autorelease ];
backgroundView.backgroundColor = [ UIColor yellowColor ];
cell.backgroundView = backgroundView;
for ( UIView* view in cell.contentView.subviews )
{
view.backgroundColor = [ UIColor yellowColor ];
}
}
// Set up the cell...
if ([[menuList objectAtIndex:indexPath.row] isKindOfClass:[Category class]])
{
cell.text = ((Category *)[menuList objectAtIndex:indexPath.row]).name;
}
else
{
[cell setText: [menuList objectAtIndex:indexPath.row]];
}
return cell;
}
However when it renders only part of the cell has the background applied kind of like this (where z is the bg fill), even the background of the Accessory is colour but not the text.
xxxxxxxxxxxxxxxxxxxx
zzzSearch
xxxxxxxxxxxxxxxxxxxx
The only way I see to be able to change the background of the text is by setting the background colour of the whole table view in Interface Builder
I bet your text is an opaque UILabel. I remember having to add myText.backgroundColor = [UIColor clearColor] to the content text UILabel to get this to work right.
I have managed to do it using a Custom TableViewCell object, but Im sure there must be a way to do it without