Custom cell in UITableView not working properly in iOS 13 Dark Mode - objective-c

I have used, since 2011, Objective-C code for my medical application, which basically has 5 tabs with associated UITableViews, 3 of which use custom cells. Initially with using Dark Mode I found that the custom cells did not change to Dark Mode automatically. Instead, I needed to implement the code below to test for Dark Mode and make cell text and background changes accordingly.
The problem is that the Dark Mode changes do not occur for empty cells in the first tab, above or below the filled cells; they remain blank white. However, similar, empty cells in the UITableViews associated with the second 2 tabs DO behave as expected in Dark Mode.
The sample images below show the cells displayed by the first and third tabs. I'm wondering at this point if there is a bug in the OS, or whether I just am not implementing the proper change, to explain why Dark Mode does not work properly in the first tab only.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomCellIdentifier = #"CustomCellIdentifier ";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
for (id oneObject in nib)
if ([oneObject isKindOfClass:[CustomCell class]])
cell = (CustomCell *)oneObject;
}
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
cell.calculationLabel.text = [dictionary objectForKey:#"Title"];
[cell.calculationLabel setLineBreakMode:NSLineBreakByWordWrapping];
cell.calculationLabel.numberOfLines = 0;
//Make color changes in cells relevant to Dark mode, if present.
if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) { //Dark mode
cell.calculationLabel.textColor = [UIColor whiteColor];
cell.calculationLabel.backgroundColor = [UIColor blackColor];
cell.statusLabel.backgroundColor = [UIColor blackColor];
cell.favoriteStatus.backgroundColor = [UIColor blackColor];
cell.backgroundColor = [UIColor blackColor];
} else { //Light mode
cell.calculationLabel.textColor = [UIColor blackColor];
cell.calculationLabel.backgroundColor = [UIColor whiteColor];
cell.statusLabel.backgroundColor = [UIColor whiteColor];
cell.favoriteStatus.backgroundColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor whiteColor];
}
cell.statusLabel.textColor = [UIColor systemGreenColor];

You need to be using the proper colors, the ones that automatically adapt between light and dark mode.
Avoid colors like whiteColor and blackColor. Use labelColor and systemBackgroundColor. Use those colors for the cells and the table view. Then you don't need to have any code that checks the current traits or interface style.
Code such as:
if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) { //Dark mode
cell.calculationLabel.textColor = [UIColor whiteColor];
cell.calculationLabel.backgroundColor = [UIColor blackColor];
cell.statusLabel.backgroundColor = [UIColor blackColor];
cell.favoriteStatus.backgroundColor = [UIColor blackColor];
cell.backgroundColor = [UIColor blackColor];
} else { //Light mode
cell.calculationLabel.textColor = [UIColor blackColor];
cell.calculationLabel.backgroundColor = [UIColor whiteColor];
cell.statusLabel.backgroundColor = [UIColor whiteColor];
cell.favoriteStatus.backgroundColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor whiteColor];
}
becomes:
cell.calculationLabel.textColor = UIColor.labelColor;
cell.calculationLabel.backgroundColor = UIColor.systemBackgroundColor;
cell.statusLabel.backgroundColor = UIColor.systemBackgroundColor;
cell.favoriteStatus.backgroundColor = UIColor.systemBackgroundColor;
cell.backgroundColor = UIColor.systemBackgroundColor;

Related

Disable UITextField in UITableViewCell

I have a UITextField in a UITableViewCell.
Even though I set -
textField.userInteractionEnabled = NO;
textField.enabled = NO
But when I click on the table cell which contains the textField, the keyboard comes up for the textfield.
Why is this happening and how can I prevent it?
EDIT: Strangely this is happening when I first set some text in the textfield. When the textfield is empty, it is not editable.
EDIT: Code for cellForRowAtIndexPath -
cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
cell.accessoryType = UITableViewCellAccessoryNone;
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, cell.bounds.size.width - 20, cell.bounds.size.height - 20)];
textField.font = [UIFont systemFontOfSize:15];
textField.textColor = [UIColor blackColor];
UIColor *placeholderColor = [UIColor colorWithRed:146/255.0 green:146/255.0 blue:146/255.0 alpha:1];
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[self getPlaceHolderTextForIndexPath:indexPath] attributes:#{NSForegroundColorAttributeName : placeholderColor}];
textField.keyboardType = [self getKeyboardTyeForIndexPath:indexPath];
textField.returnKeyType = UIReturnKeyDone;
textField.backgroundColor = [UIColor clearColor];
textField.textAlignment = NSTextAlignmentLeft;
textField.autocapitalizationType = [self getAutocapitaliztionTypeForIndexPath:indexPath];
textField.tag = 1;
if (_editingNotAllowed) {
[textField setText:[self getTextForTextFieldWithIndexPath:indexPath]];
[textField setUserInteractionEnabled:NO];
textField.enabled = NO;
} else {
[textField setUserInteractionEnabled:YES];
}
[cell.contentView addSubview:textField];
You should create an UITableViewCell as shown in this repo :)
https://github.com/breeno/EditingUITableView
And use your custom UITableViewCell like this:
if(condition){ // Check the row must be TextField
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier: nil];
if(!cell){
cell = [[CustomCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: nil];
}
cell.label.text = #"Title Row"; //UITextLabel Title
UIColor *placeholderColor = [UIColor colorWithRed:146/255.0 green:146/255.0 blue:146/255.0 alpha:1];
cell.textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[self getPlaceHolderTextForIndexPath:indexPath] attributes:#{NSForegroundColorAttributeName : placeholderColor}];
cell.textField.returnKeyType = UIReturnKeyDone;
cell.textField.backgroundColor = [UIColor clearColor];
cell.textField.textAlignment = NSTextAlignmentLeft;
cell.textField.tag = 1;
} else { // Normal UITableViewCell
}

Edit UITableViewCell property on scrolling

I am making an app in which there are quotes from famous people in an array which are being read out by openears and a tableView which shows all the quotes. The currently reading quote is to be displayed in the tableview in a different color than the other quotes. I tried with the following function in cellForRowAtIndexPath but the color of the cell remains the same unless the user scrolls it out and then come back to the view. But I need to set the color automatically and at the same time all other cells should revert back to the initial(white) color. Is there any method which can achieve this? I don't want to reload the table each time to achieve this.
if ([[arrayOfQuoteIds objectAtIndex:indexPath.row]intValue] == currentQuoteId) {
cell.backgroundColor = [self colorWithHexString:#"D3FFFF"];
cell.contentView.backgroundColor = [self colorWithHexString:#"D3FFFF"];
cell.textLabel.backgroundColor = [self colorWithHexString:#"D3FFFF"];
cell.detailTextLabel.backgroundColor = [self colorWithHexString:#"D3FFFF"];
cell.accessoryView.backgroundColor = [self colorWithHexString:#"D3FFFF"];
}
else {
cell.backgroundColor = [UIColor whiteColor];
cell.contentView.backgroundColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor whiteColor];
cell.detailTextLabel.backgroundColor = [UIColor whiteColor];
cell.accessoryView.backgroundColor = [UIColor whiteColor];
}
I can be wrong but how You want to achieve change of cell appearence without redrawing cell?
If You don't want reload all table try to reload just changed rows
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

UITableViewCell and disappearing separator

I try to implement my own simple style of cells in my UITableView and I have a problem with separator. Works great with normal view, but when i select a cell it disappears. I try to add to my customSelect view separator, but then I can't see the separator anywhere. How can I add a separator to selected cell?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyCellIdentifier = #"MyCellIdentifier";
UITableViewCell *cell = [wallMenuTableView dequeueReusableCellWithIdentifier:MyCellIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyCellIdentifier];
MenuItemModel *mItem = [menu.menuPositions objectAtIndex:indexPath.row];
cell.textLabel.text = mItem.displayName;
cell.textLabel.textColor = [UIColor colorWithRed:0.70 green:0.70 blue:0.70 alpha:1.0];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.font = [UIFont fontWithName:#"ArialMT" size:16];
cell.textLabel.shadowColor = [UIColor blackColor];
cell.textLabel.shadowOffset = CGSizeMake(0.0, 1.0);
customSeparator = [[UIView alloc] initWithFrame:CGRectMake(0, (cell.frame.origin.y), 320, 2)];
customSeparator.backgroundColor=[UIColor blackColor];
[customSeparator.layer setShadowOffset:CGSizeMake(0.0, 0.8)];
[customSeparator.layer setShadowOpacity:0.8];
[customSeparator.layer setShadowRadius:0.8];
[customSeparator.layer setShadowColor:[UIColor grayColor].CGColor];
[cell.contentView addSubview:customSeparator];
customSelect = [[UIView alloc] initWithFrame:CGRectMake(0, (cell.frame.origin.y+2), cell.frame.size.width, cell.frame.size.height)];
//[customSelect addSubview:customSeparator];
customSelect.backgroundColor = [UIColor clearColor];
[cell setSelectedBackgroundView:customSelect];
}
return cell;
}
And current result:
Use a UIImageView instead of a simple UIView for your separator. Set the Image value (this is important!) instead of backgroundColor value and stretch the image with scale to fill.
Maybe your costumSelect is under the contentView of the Cell. I implemented such behaviour before, but I subclassed UITableViewCell. Try to override setSelected method on your custom cell.
use tableView.separatorColor (and tableView.separatorStyle) to set a contrasting separator color. If you're drawing your own separators within the cell, set separatorStyle=UITableViewCellSeparatorStyleNone. Furthermore, setting the selectionStyle to UITableViewCellSelectionStyleNone will likely help you

Make a cell's background transparent, without also making the text transparent

I have a UITableView that I want to have a transparent background, but I can't seem to get that without making the text in the cells transparent as well. Here is what I have.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSString *labelString = #"Hello";
[cell setBackgroundColor:[UIColor darkGrayColor]];
cell.alpha 0.2f;
cell.textLabel.textcolor = [UIColor whiteColor];
cell.textLabel.text = labelString;
I have tried several other things such as setting the contentView's background color and alpha, as well as the backgroundView's color and alpha, nothing seems to work.
Instead of setting the cell.alpha,
cell.backgroundColor = [UIColor clearColor];
You may also need to set the table view background.
Placing this code in the -tableView:willDisplayCell:forRowAtIndexPath: solved the issue.
UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;

Loading data issue in UITableview

I created an application using UITableView. I parsing the data from URL and listed in UITableView.
But in UITableView, the first the data only displayed again and again.
I don't know why this problem occurred.
My code is as below:
-(UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
CGRect frame = CGRectMake(10.0f, 5.0, 300.0, 55);
valueField = [[UILabel alloc] initWithFrame:frame];
[valueField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
valueField.tag = 111;
valueField.font=[UIFont fontWithName:#"Helvetica" size:16.0];
valueField.textAlignment = UITextAlignmentLeft;
valueField.lineBreakMode=UILineBreakModeWordWrap;
valueField.numberOfLines = 0;
valueField.textColor = [UIColor whiteColor];
valueField.highlightedTextColor = [UIColor whiteColor];
valueField.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
valueField.adjustsFontSizeToFitWidth = YES;
valueField.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
valueField.text=[title1 objectAtIndex:count1];
[cell.contentView addSubview:valueField];
UIImage *favOn = [UIImage imageNamed:#""];
UIImage *favOff = [UIImage imageNamed:#""];
titlebutton = [[UIButton alloc] initWithFrame:CGRectMake(10.0f, 15.0, 300.0, 55)];
[titlebutton setTag:indexPath.row];
//titlebutton.tag = 111;
[titlebutton setImage:favOff forState:UIControlStateNormal];
[titlebutton setImage:favOn forState:UIControlStateSelected];
if([self getFavState:#"111"])
{
[titlebutton setSelected:YES];
}
else
{
[titlebutton setSelected:NO];
}
[titlebutton addTarget:self action:#selector(favButtonSwitch:) forControlEvents:UIControlEventTouchUpInside];
[titlebutton setBackgroundColor:[UIColor clearColor]];
[titlebutton setTitle:#"" forState:UIControlStateNormal];
[cell.contentView addSubview:titlebutton];
CGSize labelSize = [valueField.text sizeWithFont:valueField.font constrainedToSize:valueField.frame.size lineBreakMode:UILineBreakModeWordWrap];
int labelHeight = labelSize.height;
NSLog(#"labelHeight = %d", labelHeight);
frame = CGRectMake(10.0f, 40.0, 300.0, 55);
pubField = [[UILabel alloc] initWithFrame:frame];
[pubField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
pubField.tag = 111;
pubField.font=[UIFont fontWithName:#"Helvetica" size:12.0];
pubField.textAlignment = UITextAlignmentLeft;
pubField.lineBreakMode=UILineBreakModeWordWrap;
pubField.numberOfLines = 0;
pubField.textColor = [UIColor whiteColor];
pubField.highlightedTextColor = [UIColor whiteColor];
pubField.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
pubField.adjustsFontSizeToFitWidth = YES;
pubField.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
pubField.text=[pubDate objectAtIndex:indexPath.row];
[cell.contentView addSubview:pubField];
frame = CGRectMake(5.0, 70.0, 300.0, 55);
desField = [[UILabel alloc] initWithFrame:frame];
[desField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
desField.tag = 111;
desField.font=[UIFont fontWithName:#"Helvetica" size:13.0];
desField.textAlignment = UITextAlignmentLeft;
desField.lineBreakMode=UILineBreakModeWordWrap;
desField.numberOfLines = 0;
desField.textColor = [UIColor whiteColor];
desField.highlightedTextColor = [UIColor whiteColor];
desField.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
desField.adjustsFontSizeToFitWidth = YES;
desField.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
desField.text=[description objectAtIndex:indexPath.row];
[cell.contentView addSubview:desField];
}
UIImage *patternImage = [UIImage imageNamed:#"bg.jpg"];
UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor colorWithPatternImage: patternImage];
cell.backgroundView = backView;
NSString *image1 =[images objectAtIndex:indexPath.row];
NSLog(#"Images ..... = %#",image1);
NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[images objectAtIndex:indexPath.row]]];
UIImage *myimage = [[UIImage alloc] initWithData:mydata];
UIImageView *imageView = [ [ UIImageView alloc ] initWithImage: myimage ];
imageView.frame = CGRectMake(10.0f, 110.0f, 120.0f, 120.0f);
[cell.contentView addSubview:imageView];
return cell;
}
do all the alloc/init and all things, that will be the same in any cell in the
if(cell==nil){
//<-- here
}
while setting text and any thing that changes by the specific data outside the block
if(cell==nil){
}
//<-- here
i.e:
if(cell==nil){
//....
valueField.tag = 111;
valueField.font = [UIFont fontWithName:# "Helvetica" size:16.0];
valueField.textAlignment = UITextAlignmentLeft;
valueField.lineBreakMode = UILineBreakModeWordWrap;
valueField.numberOfLines = 0;
valueField.textColor = [UIColor whiteColor];
valueField.highlightedTextColor = [UIColor whiteColor];
valueField.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
valueField.adjustsFontSizeToFitWidth = YES;
valueField.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
[cell.contentView addSubview : valueField];
//....
}
//....
valueField.text = [title1 objectAtIndex:count1];
//....
the first cell data is getting displayed in all the cells because when you initialize and create your reusable cell, you fill the data there, you need to initialize a reusable cell that doesnt have all its values set, and then set this information for each cell. That is, you need to have all the information you want for each particular cell outside the if(cell==nil) part of this code, and rely on the indexPath to know which cell you are in, and feed in different data based on this.
You seem to be constructing the subviews of the cell correctly when there is no reusable cell available. The question is, what does your code do when you do dequeue a reusable cell? It appears that you do nothing. You should move any row-specific initialization (setting label text values, images, etc.) outside the check for whether the dequeued cell is nil. In this way, whether you just constructed a new cell or dequeued a previously-created cell, the data being displayed correctly correlates to the row being displayed.
Well actually your code this way is quite hard to decode,
but first of all, you declare a lot of tag that should be used to retrieve the UIViews you're declaring.
Unfortunately these are all the same (tags) so you might never get what you want. You should maybe put in some constant in your header file like:
#define kPubLabel 100
then use them.
then you usually retrieve these after declaring your cell to populate them.
In the mean time, retrieving your images from url in this method is one of the best way to have your application being slow, since each time you scroll, you'll have to re-fetch your image from where it come from causing your table view to "freeze".
Finally, the only thing that might change, according to your code is the imageView that you add to your frame. You usually have an ImageView in which you put an image that you might have stored/cached but rarely change the contentView of a cell.
ie
Maybe you should externalize your cell building code in a class overriding UITableViewCell.
Put different tags on every element in your cell to retrieve them in your method.
Retrieve your elements in your method according to there tags.
Populate and only populate them in your method.
Retrieve your image once and for all for performance improval.
I'm not sure this will help, but in my point of view, there are actually a lot work to do in your code.