iOS: UITableviewController is not updating the value of one label - ios

I have a tableview with custom cell and it has 2 labels, I am changing the tab and view controller Programatically I have called [tablview reloadData] after updating my list. But it is just updating one label value. here is the code of cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TimesheetCell *cell = [tableView dequeueReusableCellWithIdentifier:#"TimesheetCell"];
Timesheet *t = (self.timesheetArray)[indexPath.row];
cell.timesheetTitle.text = t.date;
cell.hours.text = [NSString stringWithFormat:#"%d", t.hours];
NSLog(#"%d", t.hours);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"EEEE, MMMM d";
NSString *currentDate = [formatter stringFromDate:[NSDate date]];
NSLog(#"%#",t.date);
if([t.date isEqualToString:currentDate]){
cell.timesheetTitle.textColor = [UIColor colorWithRed:0 green:0.478431 blue:1.0 alpha:1.0];
cell.hours.textColor = [UIColor colorWithRed:0 green:0.478431 blue:1.0 alpha:1.0];
}
if(_changeCellToBlack)
{
cell.timesheetTitle.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0];
cell.hours.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
}
if (indexPath.row == 7) {
[cell.timesheetTitle setFont:[UIFont fontWithName:#"Avenir-Black" size:19]];
[cell.hours setFont:[UIFont fontWithName:#"Avenir-Black" size:19]];
[cell setAccessoryType:UITableViewCellAccessoryNone];
if (t.hours < 40) {
cell.timesheetTitle.textColor = [UIColor redColor];
cell.hours.textColor = [UIColor redColor];
}
}
return cell;
}
Hours label is not updating its value, I have logged value from list it contains the value of hour.

After spending 4 hours I have found the issue. Issue was in my code just change the alpha of cell.hours.textColor, just changed this line:
cell.hours.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0];

Related

Where to set UITableViewCell other than in cellForRowAtIndexPath ? Objective-C

In a wish to improve UITableView performance, I'm thinking of relocating all of my UITableViewCell's settings outside cellForRowAtIndexPath.
At this moment, I have several custom cells inside my tableview that I set inside cellForRowAtIndexPath. But because of this, everytime I scroll inside my tableview and so everytime a cell has to be shown on screen, the settings is done again, causing a lag.
I've tried to put settings inside viewDidLoad by using a class variable for my cell but with no effect. Do you have any idea where I could move all of my cells' settings but especially if it's possible ?
Thanks in advance and have a nice day !
EDIT :
As asked, here is my cellForRowAtIndexPathMethod :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.delegate = self;
if ([appliedTheme hasPrefix:#"DarkMode"]) {
self.tableView.backgroundColor = [UIColor colorWithRed:0.20 green:0.20 blue:0.20 alpha:1.0];
cell.backgroundColor = [UIColor colorWithRed:0.20 green:0.20 blue:0.20 alpha:1.0];
cell.cardView.backgroundColor = [UIColor colorWithRed:0.30 green:0.30 blue:0.30 alpha:1.0];
cell.webServerIntroLabel.textColor = [UIColor whiteColor];
cell.webServerOptionsTitle.textColor = [UIColor whiteColor];
cell.webServerOptionsReachableAtLabel.textColor = [UIColor whiteColor];
}else {
self.tableView.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1.0];
cell.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1.0];
cell.cardView.backgroundColor = [UIColor whiteColor];
cell.webServerIntroLabel.textColor = [UIColor blackColor];
cell.webServerOptionsTitle.textColor = [UIColor blackColor];
cell.webServerOptionsReachableAtLabel.textColor = [UIColor blackColor];
}
if ([CellIdentifier isEqualToString:#"webServerIntro"]) {
cell.webServerIntroImageView.image = [UIImage imageNamed:webServerIntroIconToShow];
cell.webServerIntroLabel.adjustsFontSizeToFitWidth = YES;
cell.webServerIntroLabel.numberOfLines = 0;
[cell.webServerIntroLabel sizeToFit];
cell.webServerIntroLabel.text = #"Some text";
}else if ([CellIdentifier isEqualToString:#"webServerOptions"]) {
cell.webServerOptionsTitle.adjustsFontSizeToFitWidth = YES;
cell.webServerOptionsTitle.text = NSLocalizedString(#"Web Server Options", nil);
cell.webServerOptionsStatusLabel.adjustsFontSizeToFitWidth = YES;
if (!webServerStarted) {
cell.webServerOptionsImageView.image = [UIImage imageNamed:#"Not-Checked"];
cell.webServerOptionsStatusLabel.text = NSLocalizedString(#"Not Running", nil);
cell.serverURLLabel.text = NSLocalizedString(#"Not Reachable", nil);
cell.ipAddressURLLabel.text = NSLocalizedString(#"Not Reachable", nil);
[cell.startStopWebServerButton setTitle:NSLocalizedString(#"Start Web Server", nil) forState:UIControlStateNormal];
cell.webServerStartedBool = NO;
}else {
NSString *serverURL = [self deviceName];
serverURL = [serverURL stringByReplacingOccurrencesOfString:#" " withString:#"-"];
cell.webServerOptionsImageView.image = [UIImage imageNamed:#"Checked"];
cell.webServerOptionsStatusLabel.text = NSLocalizedString(#"Running", nil);
cell.serverURLLabel.text = [NSString stringWithFormat:#"http://%#.local", serverURL];
cell.ipAddressURLLabel.text = [NSString stringWithFormat:#"%#", webUploader.serverURL];
[cell.startStopWebServerButton setTitle:NSLocalizedString(#"Stop Web Server", nil) forState:UIControlStateNormal];
cell.webServerStartedBool = YES;
}
cell.webServerOptionsStatusLabel.textColor = [UIColor lightGrayColor];
cell.webServerOptionsReachableAtLabel.adjustsFontSizeToFitWidth = YES;
cell.webServerOptionsReachableAtLabel.text = NSLocalizedString(#"Reachable At :", nil);
cell.ipAddressURLLabel.adjustsFontSizeToFitWidth = YES;
cell.ipAddressURLLabel.textColor = [UIColor lightGrayColor];
cell.ipAddressURLLabel.layer.borderWidth = 1.0f;
cell.ipAddressURLLabel.layer.borderColor = [[UIColor lightGrayColor] CGColor];
cell.ipAddressURLLabel.layer.cornerRadius = 5;
cell.serverURLLabel.adjustsFontSizeToFitWidth = YES;
cell.serverURLLabel.textColor = [UIColor lightGrayColor];
cell.serverURLLabel.layer.borderWidth = 1.0f;
cell.serverURLLabel.layer.borderColor = [[UIColor lightGrayColor] CGColor];
cell.serverURLLabel.layer.cornerRadius = 5;
cell.startStopWebServerButton.layer.borderWidth = 1.0f;
cell.startStopWebServerButton.layer.borderColor = [[UIColor clearColor] CGColor];
cell.startStopWebServerButton.layer.cornerRadius = 5;
cell.startStopWebServerButton.titleLabel.adjustsFontSizeToFitWidth = YES;
if ([appliedTheme isEqualToString:#"Default-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0 green:0.478 blue:0.875 alpha:1]; /*#007ADF : Bleu iOS*/
}else if ([appliedTheme isEqualToString:#"Red-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.90 green:0.07 blue:0.00 alpha:1.0]; /*#E61100 : Rouge*/
}else if ([appliedTheme isEqualToString:#"Orange-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.83 green:0.33 blue:0.00 alpha:1.0]; /*#D35400 : Orange*/
}else if ([appliedTheme isEqualToString:#"Yellow-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.95 green:0.61 blue:0.07 alpha:1.0]; /*#F39C12 : Jaune*/
}else if ([appliedTheme isEqualToString:#"Green-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.15 green:0.68 blue:0.38 alpha:1.0]; /*#27AE60 : Vert*/
}else if ([appliedTheme isEqualToString:#"Purple-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.56 green:0.27 blue:0.68 alpha:1.0]; /*#8E44AD : Violet*/
}else if ([appliedTheme isEqualToString:#"Gray-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.74 green:0.76 blue:0.78 alpha:1.0]; /*#BDC3C7 : Gris*/
}else if ([appliedTheme isEqualToString:#"DarkGray-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.50 green:0.55 blue:0.55 alpha:1.0]; /*#7F8C8D : Gris foncé*/
}else if ([appliedTheme isEqualToString:#"DesaturatedBlue-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.17 green:0.24 blue:0.31 alpha:1.0]; /*#2C3E50 : Bleu désaturé*/
}else if ([appliedTheme isEqualToString:#"VeryDarkGray-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.20 green:0.20 blue:0.20 alpha:1.0]; /*#333333 : Gris très foncé*/
}else if ([appliedTheme isEqualToString:#"Black-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.00 green:0.00 blue:0.00 alpha:1.0]; /*#000000 : Noir*/
}else if ([appliedTheme isEqualToString:#"DarkModeDefault-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0 green:0.478 blue:0.875 alpha:1]; /*#007ADF : Bleu iOS*/
}else if ([appliedTheme isEqualToString:#"DarkModeRed-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.90 green:0.07 blue:0.00 alpha:1.0]; /*#E61100 : Rouge*/
}else if ([appliedTheme isEqualToString:#"DarkModeOrange-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.83 green:0.33 blue:0.00 alpha:1.0]; /*#D35400 : Orange*/
}else if ([appliedTheme isEqualToString:#"DarkModeYellow-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.95 green:0.61 blue:0.07 alpha:1.0]; /*#F39C12 : Jaune*/
}else if ([appliedTheme isEqualToString:#"DarkModeGreen-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.15 green:0.68 blue:0.38 alpha:1.0]; /*#27AE60 : Vert*/
}else if ([appliedTheme isEqualToString:#"DarkModePurple-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.56 green:0.27 blue:0.68 alpha:1.0]; /*#8E44AD : Violet*/
}else if ([appliedTheme isEqualToString:#"DarkModeGray-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.74 green:0.76 blue:0.78 alpha:1.0]; /*#BDC3C7 : Gris*/
}else if ([appliedTheme isEqualToString:#"DarkModeDarkGray-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.50 green:0.55 blue:0.55 alpha:1.0]; /*#7F8C8D : Gris foncé*/
}else if ([appliedTheme isEqualToString:#"DarkModeDesaturatedBlue-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.17 green:0.24 blue:0.31 alpha:1.0]; /*#2C3E50 : Bleu désaturé*/
}else if ([appliedTheme isEqualToString:#"DarkModeVeryDarkGray-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.20 green:0.20 blue:0.20 alpha:1.0]; /*#333333 : Gris très foncé*/
}else if ([appliedTheme isEqualToString:#"DarkModeBlack-Theme"]) {
cell.startStopWebServerButton.backgroundColor = [UIColor colorWithRed:0.00 green:0.00 blue:0.00 alpha:1.0]; /*#000000 : Noir*/
}
}else {
}
return cell;
}
While you have a lot of code there, nothing seems like it would be dreadfully slow.
It is a minor point in term of performance, but the code that sets the tableview's background colour should be in viewWillAppear rather than cellForRowAt as there is no need to set the background colour each time a cell is dequeued.
You can also simplify this function considerably by moving all of the theme-related code into the cell subclass. Call a function on the cell passing the theme and have the cell set all of that stuff; The same amount of code will be executed, but cellForRowAt will be smaller and easier to read.
Where I think you can get some performance is by eliminating the repeated string operations related to the theme. Create a class or struct that represents your theme and use an enumeration (This stuff is much easier is Swift, but you can still do it in ObjectiveC).
If you do this, then, for example,
if ([appliedTheme hasPrefix:#"DarkMode"])
becomes
if (appliedTheme.darkMode)
A complex string comparison is replaced with a simple boolean check; this is much faster
Similarly, using an enumeration for the theme you get something like
switch (appliedTheme.theme) {
case DarkModeGreen-Theme:
...
Now an expesnsive string comparison has been replaced with a quick integer comparison.
Finally, creating UIColors is relatively expensive. You can move that into your Theme object too. When you create an instance of a Theme, expose the colours as properties. This way you get rid of the switch statement I suggested and creating the colours each time and simply say something like
cell.startStopWebServerButton.backgroundColor = appliedTheme.buttonBackgroundColor
Have to tried prepareforreuse. Apart from loading images I don't see other code that could cause the delay. Few things that might help
Even though imagenamed should return a cache image if it's already loaded, try loading images async with GCD
Is the web service code causing any delay?
Run instruments (time profiler?) to see what is causing the lag or delay

UIProgressView update itself in custom UITableViewCell

I've a UITableView populated with several custom UITableViewCell. In one of them I have a UIProgressView that will represent a percentage of data.
However when I set a progress (let's say 50%), the progress bar appear at 50% and update itself until it reach 100%...
Here the code I use to update the data :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if ([appliedTheme hasPrefix:#"DarkMode"]) {
self.tableView.backgroundColor = [UIColor colorWithRed:0.20 green:0.20 blue:0.20 alpha:1.0];
cell.backgroundColor = [UIColor colorWithRed:0.20 green:0.20 blue:0.20 alpha:1.0];
cell.cardView.backgroundColor = [UIColor colorWithRed:0.30 green:0.30 blue:0.30 alpha:1.0];
cell.storageIntroLabel.textColor = [UIColor whiteColor];
cell.storageInfoTitle.textColor = [UIColor whiteColor];
cell.deviceStorageTitle.textColor = [UIColor whiteColor];
}else {
self.tableView.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1.0];
cell.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1.0];
cell.cardView.backgroundColor = [UIColor whiteColor];
cell.storageIntroLabel.textColor = [UIColor blackColor];
cell.storageInfoTitle.textColor = [UIColor blackColor];
cell.deviceStorageTitle.textColor = [UIColor blackColor];
}
if ([CellIdentifier isEqualToString:#"storageIntro"]) {
cell.storageIntroImageView.image = [UIImage imageNamed:storageIntroIconToShow];
cell.storageIntroLabel.adjustsFontSizeToFitWidth = YES;
cell.storageIntroLabel.numberOfLines = 0;
[cell.storageIntroLabel sizeToFit];
cell.storageIntroLabel.text = NSLocalizedString(#"Here you can find informations about your storage and how FileStore is using your memory by types of files", nil);
}else if ([CellIdentifier isEqualToString:#"storageInfo"]){
cell.storageInfoTitle.text = NSLocalizedString(#"Storage Informations", nil);
cell.deviceStorageTitle.adjustsFontSizeToFitWidth = YES;
cell.deviceStorageTitle.text = NSLocalizedString(#"Device Storage", nil);
cell.totalDeviceSpaceLabel.adjustsFontSizeToFitWidth = YES;
cell.totalDeviceSpaceLabel.text = NSLocalizedString(#"Total space :", nil);
cell.finalTotalDeviceSpaceLabel.text = totalDeviceSpaceString;
cell.freeDeviceSpaceLabel.adjustsFontSizeToFitWidth = YES;
cell.freeDeviceSpaceLabel.text = NSLocalizedString(#"Free space :", nil);
cell.finalFreeDeviceSpaceLabel.text = totalDeviceFreeSpaceString;
dispatch_async(dispatch_get_main_queue(), ^{
[cell.deviceStorageProgressView setProgress:50 animated:YES];
});
}else {
}
return cell;
}
Even if I doesn't use the bool animated:YES, the progress bar update itself over the time from the percentage I've set...
It doesn't mean anything to me ! Do I have to put the UIProgressView settings in another cell's delegate method ?
Thanks in advance for your help !
According to UIProgressView documentation
The current progress is represented by a floating-point value between 0.0 and 1.0, inclusive, where 1.0 indicates the completion of the task. The default value is 0.0. Values less than 0.0 and greater than 1.0 are pinned to those limits.
You are setting it to 50 which will basically be converted to upper value 1. You need to give value as 0.5 if you want 50% progress.

UITableview Cell Height is not calculated properly and cells overlap

I need to create and populate custom cells. These cells height are huge about 300-400.
So I have a populateCellView function , I call that function on heightForRowAtIndexPath to calculate height and in cellForRowAtIndexPath to add that view to cell content view, but somehow sometimes cells overlap.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [self populateCellView:indexPath.row].frame.size.height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIView *populateCell= nil;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
populateCell=[[UIView alloc] initWithFrame:CGRectZero];
[[populateCell layer] setBorderWidth:2.0f];
populateCell= [self populateCellView:indexPath.row];
[[cell contentView] addSubview:populateCell];
}
if (!populateCell){
populateCell = (UIView*)[cell viewWithTag:1];
populateCell= [self populateCellView:indexPath.row];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
populate view function:
-(UIView *)populateCellView:(int)forCase
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width,500)];
[view setBackgroundColor:[UIColor colorWithRed:40/255.0 green:150/255.0 blue:213/255.0 alpha:1.0]];
view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
if ([view subviews]){
for (UIView *subview in [view subviews]) {
[subview removeFromSuperview];
}
}
UILabel *caseNumberLabel;
UILabel *caseNameLabel;
UILabel *caseSummaryLabel;
UILabel *caseAncLabel;
UILabel *caseNumberText;
UILabel *caseNameText;
UILabel *caseSummaryText;
UILabel *caseAncText;
//static label for case number
caseNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,10,250, 50)];
caseNumberLabel.textColor = [UIColor whiteColor];
caseNumberLabel.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:24.0f];
caseNumberLabel.text =#"Case Number:";
caseNumberLabel.backgroundColor = [UIColor clearColor];
caseNumberLabel.numberOfLines=0;
caseNumberLabel.adjustsFontSizeToFitWidth = NO;
caseNumberLabel.tag=forCase;
caseNumberLabel.frame=[self calculateLabelFrame:caseNumberLabel];
//case number from plist
caseNumberText = [[UILabel alloc] initWithFrame:CGRectMake(caseNumberLabel.frame.size.width+50,caseNumberLabel.frame.origin.y,self.view.frame.size.width-caseNumberLabel.frame.size.width-50, 50)];
caseNumberText.textColor = [UIColor whiteColor];
caseNumberText.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:24.0f];
caseNumberText.text =[[self agenda] getCaseNumber:forCase];
caseNumberText.backgroundColor = [UIColor clearColor];
caseNumberText.numberOfLines=0;
caseNumberText.adjustsFontSizeToFitWidth = NO;
caseNumberText.tag=forCase;
//[caseNumberLabel sizeToFit];
caseNumberText.frame=[self calculateLabelFrame:caseNumberText];
//static label for case name
caseNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,caseNumberText.frame.origin.y + caseNumberText.bounds.size.height+5,250, 50)];
caseNameLabel.textColor =[UIColor whiteColor];
caseNameLabel.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:24.0f];
caseNameLabel.text =#"Case Name:";
caseNameLabel.backgroundColor = [UIColor clearColor];
caseNameLabel.numberOfLines=0;
caseNameLabel.adjustsFontSizeToFitWidth = NO;
caseNameLabel.tag=forCase;
//[caseNumberLabel sizeToFit];
caseNameLabel.frame=[self calculateLabelFrame:caseNameLabel];
//case name from plist
caseNameText = [[UILabel alloc] initWithFrame:CGRectMake(caseNameLabel.frame.size.width+50,caseNameLabel.frame.origin.y,self.view.frame.size.width-caseNameLabel.frame.size.width-50, 50)];
caseNameText.textColor = [UIColor whiteColor];
caseNameText.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:24.0f];
caseNameText.text =[[self agenda] getCaseName:forCase];
caseNameText.backgroundColor = [UIColor clearColor];
caseNameText.numberOfLines=0;
//caseNameText.adjustsFontSizeToFitWidth = NO;
caseNameText.tag=forCase;
[caseNameText sizeToFit];
//caseNameText.lineBreakMode = NSLineBreakByWordWrapping;
caseNameText.frame=[self calCellLabelFrame:caseNameText previousLabel:caseNameLabel];
//static label for case summary
caseSummaryLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,caseNameText.frame.origin.y + caseNameText.bounds.size.height+20,250, 50)];
caseSummaryLabel.textColor = [UIColor whiteColor];
caseSummaryLabel.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:24.0f];
caseSummaryLabel.text =#"Case Summary:";
caseSummaryLabel.backgroundColor = [UIColor clearColor];
caseSummaryLabel.numberOfLines=0;
caseSummaryLabel.adjustsFontSizeToFitWidth = NO;
caseSummaryLabel.tag=forCase;
//[caseSummaryLabel sizeToFit];
caseSummaryLabel.frame=[self calculateLabelFrame:caseSummaryLabel];
//case name from plist
caseSummaryText = [[UILabel alloc] initWithFrame:CGRectMake(caseSummaryLabel.frame.size.width+50,caseSummaryLabel.frame.origin.y,self.view.frame.size.width-caseSummaryLabel.frame.size.width-100, 50)];
caseSummaryText.textColor = [UIColor whiteColor];
caseSummaryText.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:24.0f];
caseSummaryText.text =[[self agenda] getCaseSummary:forCase];
caseSummaryText.backgroundColor = [UIColor clearColor];
caseSummaryText.numberOfLines=0;
//caseSummaryText.adjustsFontSizeToFitWidth = NO;
caseSummaryText.tag=forCase;
[caseSummaryText sizeToFit];
//caseSummaryText.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
caseSummaryText.frame=[self calCellLabelFrame:caseSummaryText previousLabel:caseSummaryLabel];
//static label for anc
caseAncLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,caseSummaryText.frame.origin.y + caseSummaryText.bounds.size.height+15,250, 50)];
caseAncLabel.textColor = [UIColor whiteColor];
caseAncLabel.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:24.0f];
caseAncLabel.text =#"ANC:";
caseAncLabel.backgroundColor = [UIColor clearColor];
caseAncLabel.numberOfLines=0;
caseAncLabel.adjustsFontSizeToFitWidth = NO;
caseAncLabel.tag=forCase;
//[ccaseAncLabel sizeToFit];
caseAncLabel.frame=[self calculateLabelFrame:caseAncLabel];
//case name from plist
caseAncText = [[UILabel alloc] initWithFrame:CGRectMake(caseAncLabel.frame.size.width+50,caseAncLabel.frame.origin.y,self.view.frame.size.width-caseAncLabel.frame.size.width-50, 50)];
caseAncText.textColor = [UIColor whiteColor];
caseAncText.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:24.0f];
caseAncText.text =[[self agenda] getCaseAnc:forCase];
caseAncText.backgroundColor = [UIColor clearColor];
caseAncText.numberOfLines=0;
//caseAncText.adjustsFontSizeToFitWidth = NO;
caseAncText.tag=forCase;
[caseAncText sizeToFit];
//caseSummaryText.lineBreakMode = NSLineBreakByWordWrapping;
caseAncText.frame=[self calCellLabelFrame:caseAncText previousLabel:caseAncLabel];
//add button
UIButton *acceptButton=[UIButton buttonWithType:UIButtonTypeCustom];
[acceptButton setTag:forCase];
acceptButton.titleLabel.tag=1;
[acceptButton setFrame:CGRectMake(caseAncLabel.frame.size.width+50,caseAncText.frame.origin.y + caseAncText.bounds.size.height+20,181,39)];
NSString *radioButtonImage=#"view_attachment.png";
UIImage *acceptbuttonImage = [UIImage imageNamed:radioButtonImage];
[acceptButton setBackgroundImage:acceptbuttonImage forState:UIControlStateNormal];
[acceptButton addTarget:self action:#selector(showAttachements:) forControlEvents:UIControlEventTouchUpInside];
//whie line
UIView *anotherline = [[UIView alloc] initWithFrame:CGRectMake(0, acceptButton.frame.origin.y + acceptButton.bounds.size.height+15, self.view.frame.size.width, 1)];
anotherline.backgroundColor=[UIColor whiteColor];
anotherline.tag=forCase;
[view addSubview:acceptButton];
[view addSubview: caseNumberLabel];
[view addSubview:caseNumberText];
[view addSubview:caseNameLabel];
[view addSubview:caseNameText];
[view addSubview:caseSummaryLabel];
[view addSubview:caseSummaryText];
[view addSubview:caseAncText];
[view addSubview:caseAncLabel];
[view addSubview:anotherline];
view.frame = CGRectMake(0,0, self.view.frame.size.width,anotherline.frame.origin.y + anotherline.bounds.size.height+5);
CGFloat redLevel = rand() / (float) RAND_MAX;
CGFloat greenLevel = rand() / (float) RAND_MAX;
CGFloat blueLevel = rand() / (float) RAND_MAX;
view.backgroundColor = [UIColor colorWithRed: redLevel
green: greenLevel
blue: blueLevel
alpha: 1.0];
return view;
}
I call reload data like this
dispatch_async(dispatch_get_main_queue(), ^{
[_agenda loadFromPlist];
[[self tableView] reloadData];
});
Any idea why cells overlaps?
Its your UITableview cellforIndexPath causing overlap
try this
if (!populateCell){
populateCell = (UIView*)[cell viewWithTag:1];
populateCell= [self populateCellView:indexPath.row];
if ([[cell contentView] subviews]){
for (UIView *subview in [[cell contentView] subviews]) {
[subview removeFromSuperview];
}
}
[[cell contentView] addSubview:populateCell];
}
cell.backgroundColor=[UIColor clearColor];
At the end of the UITableViewDataSource method, tableView:cellForRowAtIndexPath:, you have to call [[cell contentView] addSubView:populateCell]. If the table dequeued a cell successfully, you have to retrieve the appropriate populateCell and add it to the contentView.
If you use storyboards, and set a different height for the cells, and the rows in the tableview, the height will usually overlap. The other answers are also highly valid, but usually when I've encountered this problem, the bug lies in a different value on row height and cell height in the tableview object in your storyboard...

Pixelated UILabel in table cell

To add a UILabel to a table cell I use
UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(270, 10, 40, 12)];
timeLabel.text = #"2s";
timeLabel.backgroundColor = [UIColor clearColor];
timeLabel.font = [UIFont systemFontOfSize:12];
timeLabel.textColor = [UIColor lightGrayColor];
timeLabel.highlightedTextColor = [UIColor whiteColor];
timeLabel.textAlignment = UITextAlignmentRight;
timeLabel.frame = CGRectIntegral(timeLabel.frame);
[cell.contentView addSubview:timeLabel];
in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath.
This works fine until I scroll the table or select a cell. Then the label becomes pixelated.
At load:
after action:
I also tried to add the label by subclassing UITableViewCell and load it in
- (void) layoutSubviews.
I already found related questions here and here but nothing worked.
EDIT: It's not possible to use the standard cell labels since they're already in use. I need to add an additional label.
I finally got it working with a dirty fix.
In - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I set
cell.selectionStyle = UITableViewCellSelectionStyleNone;.
In a subclass of UITableViewCell I load the timeLabel in initWithStyle as follows:
timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(270, 10, 40, 12)];
timeLabel.text = #"2s";
timeLabel.backgroundColor = [UIColor whiteColor];
timeLabel.font = [UIFont systemFontOfSize:12];
timeLabel.textColor = [UIColor lightGrayColor];
timeLabel.highlightedTextColor = [UIColor whiteColor];
timeLabel.textAlignment = UITextAlignmentRight;
[self.contentView addSubview:timeLabel];
then I override these two functions:
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
if(highlighted == YES){
UIImage *image = [UIImage imageNamed:#"cellBg#2x.png"];
//scale custom cell background to necessary height
UIImage *scaledImage = [image scaleToSize:CGSizeMake(1,self.contentView.frame.size.height)];
//set cell background
self.backgroundColor = [UIColor colorWithPatternImage:scaledImage];
//set textcolor for default labels
self.textLabel.textColor = [UIColor whiteColor];
self.detailTextLabel.textColor = [UIColor whiteColor];
//set textcolor for custom label
timeLabel.textColor = [UIColor whiteColor];
//cope background for custom label background since timeLabel.backgroundColor = [UIColor clearColor] doesnt work
CGImageRef ref = CGImageCreateWithImageInRect(scaledImage.CGImage, CGRectMake(0, 10, 12, 20));
UIImage *img = [UIImage imageWithCGImage:ref];
//set custom label background
timeLabel.backgroundColor = [UIColor colorWithPatternImage:img];
} else {
//set unselected colors
self.backgroundColor = [UIColor whiteColor];
self.textLabel.textColor = [UIColor darkGrayColor];
self.detailTextLabel.textColor = UIColorFromRGB(0x808080);
timeLabel.textColor = UIColorFromRGB(0x808080);
//white background works without the label pixelates
timeLabel.backgroundColor = [UIColor whiteColor];
}
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
if(selected == YES){
UIImage *image = [UIImage imageNamed:#"cellBg#2x.png"];
UIImage *scaledImage = [image scaleToSize:CGSizeMake(1,self.contentView.frame.size.height)];
self.backgroundColor = [UIColor colorWithPatternImage:scaledImage];
self.textLabel.textColor = [UIColor whiteColor];
self.detailTextLabel.textColor = [UIColor whiteColor];
timeLabel.textColor = [UIColor whiteColor];
CGImageRef ref = CGImageCreateWithImageInRect(scaledImage.CGImage, CGRectMake(0, 10, 12, 20));
UIImage *img = [UIImage imageWithCGImage:ref];
timeLabel.backgroundColor = [UIColor colorWithPatternImage:img];
} else {
self.backgroundColor = [UIColor whiteColor];
self.textLabel.textColor = [UIColor darkGrayColor];
self.detailTextLabel.textColor = UIColorFromRGB(0x808080);
timeLabel.textColor = UIColorFromRGB(0x808080);
timeLabel.backgroundColor = [UIColor whiteColor];
}
}
hope this helps some people!

Poor scrolling performance in UITableView with cell subviews.

I have a UITableView with many rows. Each row has a UIView in it that I edit in the cellForRowAtIndexPath. My problem is that this lags terribly on an iPod touch. I am not sure why this is happening as the iPod should be able to take more than 11 UIViews on the screen at once? Does anyone know why this is happening?
EDIT - here is the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIView *colorView = (UIView *)[cell viewWithTag:1];
UILabel *label = (UILabel *)[cell viewWithTag:2];
colorView.layer.cornerRadius = 10;
colorView.layer.borderColor = [UIColor blackColor].CGColor;
colorView.layer.borderWidth = 2.0f;
colorView.layer.masksToBounds = YES;
switch (indexPath.row) {
case 0:
label.text = #"White";
colorView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
break;
case 1:
label.text = #"Olive";
colorView.backgroundColor = [UIColor colorWithRed:0.814 green:0.551 blue:0.119 alpha:1];
break;
case 2:
label.text = #"Dark Blue";
colorView.backgroundColor = [UIColor colorWithRed:0.036 green:0 blue:1 alpha:1];
break;
case 3:
label.text = #"Dark Green";
colorView.backgroundColor = [UIColor colorWithRed:0 green:0.387 blue:0.006 alpha:1];
break;
case 4:
label.text = #"Orange";
colorView.backgroundColor = [UIColor colorWithRed:1 green:0.500 blue:0 alpha:1];
break;
case 5:
label.text = #"Dark Brown";
colorView.backgroundColor = [UIColor colorWithRed:0.399 green:0.269 blue:0.137 alpha:1];
break;
case 6:
label.text = #"Dark Red";
colorView.backgroundColor = [UIColor colorWithRed:0.530 green:0.017 blue:0 alpha:1];
break;
case 7:
label.text = #"Maroon";
colorView.backgroundColor = [UIColor colorWithRed:0.502 green:0 blue:0.251 alpha:1];
break;
case 8:
label.text = #"Yellow";
colorView.backgroundColor = [UIColor colorWithRed:0.865 green:0.864 blue:0.002 alpha:1];
break;
case 9:
label.text = #"Purple";
colorView.backgroundColor = [UIColor colorWithRed:0.460 green:0 blue:0.865 alpha:1];
break;
default:
label.text = #"";
colorView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
break;
}
return cell;
}
Thanks
There are a few things you can do here.
Turn on quartz debugging (on the device using instruments or, as of quite recently, in the simulator). Colour blended layers - you should see mostly green. If you don't, then make sure your new views are marked as opaque. Scrolling transparent views takes a lot of performance.
If that doesn't help, profile using the time profiler in instruments. Where is the time being spent? Should you, for example, be caching the colours you are using for the background?
Your layer properties should ideally only be set once. awakeFromNib in a custom cell subclass would be a good place to do this.

Resources