UIProgressView update itself in custom UITableViewCell - ios

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.

Related

iOS Objective C - UITableView performance issue

My app is using CoreData as persistence data storage. Below is my code for the tableview. On the simulator it runs fine, but when running it on the phone it gets very laggy. Any suggestions on optimization is appreciated :)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
Journal* journal = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.titleLabel.text = journal.title.uppercaseString;
cell.titleLabel.font = [UIFont fontWithName:#"SourceSansPro-Bold" size:25];
cell.titleLabel.textColor = [UIColor blackColor];
cell.detailLabel.text = journal.detail;
cell.detailLabel.font = [UIFont fontWithName:#"SourceSansPro-SemiBold" size:18];
cell.detailLabel.textColor = [UIColor blackColor];
NSDate *currentDate = journal.timeStamp;
cell.dateLabel.text = [self.dateFormatter stringFromDate: currentDate];
cell.dateLabel.font = [UIFont fontWithName:#"SourceSansPro-SemiBold" size:16];
cell.dateLabel.textColor = [UIColor blackColor];
cell.locationLabel.text = [NSString stringWithFormat:#"%#, %#", journal.city, journal.country];
cell.locationLabel.font = [UIFont fontWithName:#"SourceSansPro-SemiBold" size:18];
cell.locationLabel.textColor = [UIColor blackColor];
cell.tempLabel.text = [NSString stringWithFormat:#"%g°C", round(journal.temp)];
cell.tempLabel.font = [UIFont fontWithName:#"SourceSansPro-SemiBold" size:18];
cell.tempLabel.textColor = [UIColor blackColor];
cell.weatherIcon.image = [UIImage imageNamed:journal.condition];
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageWithData:journal.image] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
cell.backgroundView.contentMode = UIViewContentModeScaleAspectFill;
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageWithData:journal.image] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
cell.selectedBackgroundView.contentMode = UIViewContentModeScaleAspectFill;
cell.backgroundView.alpha = 0.5;
cell.selectedBackgroundView.alpha = 0.5;
return cell;
}
You are experiencing low FPS lags because of this API usage:
[UIImage imageWithData:journal.image]
The UIImage imageWithData: method is NOT asynchronous, so as the table view loads each new cell, it has to process the data, locking up the app while doing so.
Try to find an async way to load/create/cache an image on a background thread making your UI responsive.
Check out this popular image loading/caching library SDWebImage for more ideas/inspiration and potential solution for your needs.
First, you can customize a cell, some code is unnecessary, such as set Font and TextColor in every time.
Next, you can use instrument-->Profile to find the code where is time consuming.
Hope it can help you!

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

UITableViewCell with subviews is not working properly

I have UITableView in my iOS app and I want to add some subviews to cell. I do it by using
[cell.contentView addSubview:someView];
and it works well, but... When I scroll down, subviews are starting to hide from cells that are on top and when I scroll back to top, they wont appear again... What I'm doing wrong? Is there some solution please?
EDIT
Mainly, I'm talking about "detailtext" label, but I have those problems in more cases...
Here is whole code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell;
switch (indexPath.row) {
case 0:
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
break;
default:
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
break;
}
UIView *separatorLine = [[UIView alloc] init];
separatorLine.frame = CGRectMake(15.0f, 60 - 0.5f, cell.frame.size.width-15.0f, 0.5f);
separatorLine.tag = 4;
separatorLine.backgroundColor = [UIColor lightGrayColor];
cell.layer.masksToBounds = NO;
tableView.backgroundColor = [UIColor colorWithRed:33.0 / 255.0 green:157.0 / 255.0 blue:147.0 / 255.0 alpha:1.0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIView *row2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
UIView *profileBorder = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-50, 50, 102, 102)];
profileBorder.layer.borderColor = [UIColor whiteColor].CGColor;
profileBorder.layer.borderWidth = 5; //2
profileBorder.layer.cornerRadius = 50;
NZCircularImageView *profileImage = [[NZCircularImageView alloc] initWithFrame:CGRectMake(1,1, 100, 100)];
profileImage.image = profilePhoto;
profileImage.contentMode = UIViewContentModeScaleAspectFill;
UITapGestureRecognizer *showBigProfilePhoto = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showImage:)];
profileImage.userInteractionEnabled = YES;
[profileImage addGestureRecognizer:showBigProfilePhoto];
[profileBorder addSubview:profileImage];
UILabel *numberFeelings = [[UILabel alloc] initWithFrame:CGRectMake(10, 100-25, 100, 50)];
numberFeelings.text = [NSString stringWithFormat:#"%#\nFeelings", feelings];
numberFeelings.font = [UIFont boldSystemFontOfSize:16];
numberFeelings.textAlignment = NSTextAlignmentCenter;
numberFeelings.textColor = [UIColor whiteColor];
numberFeelings.numberOfLines = 0;
UILabel *numberFriends = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2+60, 100-25, 100, 50)];
numberFriends.text = [NSString stringWithFormat:#"%#\nFollowers", friends];
numberFriends.font = [UIFont boldSystemFontOfSize:16];
numberFriends.textColor = [UIColor whiteColor];
numberFriends.numberOfLines = 0;
numberFriends.textAlignment = NSTextAlignmentCenter;
[row2 addSubview:profileBorder];
[row2 addSubview:numberFriends];
[row2 addSubview:numberFeelings];
int rectButtons = cell.frame.size.width-246;
UIImageView *graph = [[UIImageView alloc] initWithFrame:CGRectMake(rectButtons/2, -20, 82, 82)];
UIImageView *badgets = [[UIImageView alloc] initWithFrame:CGRectMake(rectButtons/2+82, -20, 82, 82)];
UIImageView *photos = [[UIImageView alloc] initWithFrame:CGRectMake(rectButtons/2+164, -20, 82, 82)];
graph.image = [UIImage imageNamed:#"graph.jpg"];
badgets.image = [UIImage imageNamed:#"badgets.jpg"];
photos.image = [UIImage imageNamed:#"photos.jpg"];
graph.userInteractionEnabled = YES;
badgets.userInteractionEnabled = YES;
photos.userInteractionEnabled = YES;
UITapGestureRecognizer *graphTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showGraph:)];
[graph addGestureRecognizer:graphTap];
NSArray *jmenoCasti = [name componentsSeparatedByString:#" "];
krestni = [jmenoCasti objectAtIndex:0];
int indexOfPost = indexPath.row-3;
NSMutableAttributedString *str;
int countFeeling;
int countString;
int countBeforeFeeling;
if (indexPath.row >=3) {
str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:#"%# was %#", krestni, [naladyHim objectAtIndex:[[[posts objectAtIndex:indexOfPost] objectForKey:#"_feel"] integerValue]]]];
countFeeling = [[naladyHim objectAtIndex:[[[posts objectAtIndex:indexOfPost] objectForKey:#"_feel"] integerValue]] length];
countString = krestni.length+5+countFeeling;
countBeforeFeeling = countString-countFeeling+1;
int rangeStart = countBeforeFeeling-1;
int rangeStop = str.length-rangeStart;
NSLog(#"%i ... %i", countBeforeFeeling-1, countString-1);
[str addAttribute:NSFontAttributeName value: [UIFont fontWithName:#"Helvetica-Bold" size:16.0f] range:NSMakeRange(rangeStart, rangeStop)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:32.0 / 255.0 green:147.0 / 255.0 blue:138.0 / 255.0 alpha:1.0] range:NSMakeRange(rangeStart, rangeStop)];
}
UILabel *mainText = [[UILabel alloc] initWithFrame:CGRectMake(15, 70, cell.frame.size.width-10, 20)];
mainText.attributedText = str;
UILabel *detailText;
if (!detailText) {
detailText = [[UILabel alloc] initWithFrame:CGRectMake(15, 90, cell.frame.size.width-10, 30)];
}
detailText.textColor = [UIColor grayColor];
detailText.font = [UIFont systemFontOfSize:13];
switch (indexPath.row) {
case 0:
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.text = name;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont systemFontOfSize:20];
cell.backgroundColor = [UIColor clearColor];
break;
case 1:
[cell.contentView addSubview:row2];
cell.backgroundColor = [UIColor clearColor];
break;
case 2:
cell.backgroundColor = [UIColor colorWithRed:236.0 / 255.0 green:235.0 / 255.0 blue:210.0 / 255.0 alpha:1.0];
[cell.contentView addSubview:graph];
[cell.contentView addSubview:badgets];
[cell.contentView addSubview:photos];
break;
default:
detailText.text = [[posts objectAtIndex:indexPath.row-3] objectForKey:#"_text"];
[cell.contentView addSubview:detailText];
cell.textLabel.attributedText = str;
cell.backgroundColor = [UIColor colorWithRed:236.0 / 255.0 green:235.0 / 255.0 blue:210.0 / 255.0 alpha:1.0];
break;
}
return cell; }
This is an easy way thats works for me:
for(UIView *subview in cell.contentView.subviews)
{
if([subview isKindOfClass: [UIView class]])
{
[subview removeFromSuperview];
}
}
You can use it at the begin of
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
In your tableView:cellForRowAtIndexPath:, you hide the info when you don't want it to be shown, but you don't explicitly unhide it for cells where it should be shown.
Look at the first two lines in that method: What you are - correctly - doing is reusing your cells, so when cells are scrolled out of view, they are removed from the UITableView and put into the reuse queue. Then, when cells should become visible, the TableView gets cells from that queue - or creates new ones if none are available.
This all goes very well, but after a while, cells with hidden info buttons are put on the queue. And then, some time later, those cells are reused - and sometimes for rows in which there should be info visible.
There are two solutions to this: You could either explicitly unhide the information for those rows where you want it to be shown, or you could use two different kinds of cell, one with hidden info, and one with visible info. You then give each of those cells a different identifier, and based on what row the cells are in, set the identifier before dequeuing/creating cells.
You should create a subclass of UITableViewCell for each different cell and add all your view related code that doesn't change depending on the data into an initialization method. Then create a method in each cell called something like configureWithData and pass in the data relevant to the cell. The creation of your attributed string and modification of label frames can occur in this configuration method.
It will dramatically reduce the clutter in your UITableViewController and is much better design wise. There is no real need for your view controller to know what your table cells look like.
Here is an example of what I am talking about:
-(void)awakeFromNib
{
if( self.accessoryType == UITableViewCellAccessoryDisclosureIndicator )
{
DTCustomColoredAccessory *accessory = [DTCustomColoredAccessory accessoryWithColor:[UIColor whiteColor]];
accessory.highlightedColor = [UIColor blackColor];
self.accessoryView = accessory;
}
}
-(void)configureCellWithObject:(id)inObject
{
TableDataModel *dataObject = (TableDataModel *)inObject;
self.titleLabel.text = dataObject.titleString;
self.subtitleLabel.text = dataObject.subtitleString;
self.accessibilityIdentifier = dataObject.accessIdString;
if( dataObject.imageUrlString != nil )
{
UIImage *iconImage = [UIImage imageNamed:dataObject.imageUrlString];
if( iconImage != nil )
{
NSInteger yOffset = [StaticTools centerYOffset:self.frame.size objectFrameSize:iconImage.size];
self.iconImageView.image = iconImage;
CGRect frame = self.iconImageView.frame;
frame.origin.y = yOffset;
frame.size = iconImage.size;
[self.iconImageView setFrame:frame];
}
else
{
[self.iconImageView loadImageFromUrl:dataObject.imageUrlString];
}
}
}

iOS switching between UIWebViews causing memory warning

Hi i have created a custom tab bar that hides and shows certain web views depending on which tab your on i'm currently loading all three web views on an operation queue then making them visable and invisable depending on the selected tab. problem is its so slow to load and scroll and i'm getting a recieved memory warning
heres what i have done so far
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSArray *subviews = [cell subviews];
for(UIView *subview in subviews) if([subview tag] == 4) [subview removeFromSuperview];
UIView *loadingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 800)];
UIView *sendingMessage = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
UIActivityIndicatorView *sendingSpinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(25, 15, 50, 50)];
UILabel *sendingLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 65, 100, 20)];
int sendingViewLeft = (loadingView.frame.size.width/2) - (sendingMessage.frame.size.width/2);
int sendingViewTop = 160 - (sendingMessage.frame.size.height/2);
loadingView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
sendingMessage.frame = CGRectMake(sendingViewLeft, sendingViewTop, sendingMessage.frame.size.width, sendingMessage.frame.size.height);
sendingMessage.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
sendingMessage.layer.cornerRadius = 10;
sendingSpinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
sendingLabel.textColor = [UIColor whiteColor];
sendingLabel.backgroundColor = [UIColor clearColor];
sendingLabel.text = #"Loading...";
sendingLabel.textAlignment = UITextAlignmentCenter;
sendingLabel.font = [UIFont boldSystemFontOfSize:12];
sendingLabel.shadowColor = [UIColor blackColor];
sendingLabel.shadowOffset = CGSizeMake(0, -1);
[sendingSpinner startAnimating];
[sendingMessage addSubview:sendingLabel];
[sendingMessage addSubview:sendingSpinner];
[loadingView addSubview:sendingMessage];
[loadingView setTag:4];
cell.textLabel.text = #"";
if([self.webViewStatus isEqualToString:#"FALSE"]){
[cell addSubview:loadingView];
self.tableView.userInteractionEnabled = NO;
} else {
[cell addSubview:self.fixturesWebView];
[cell addSubview:self.resultsWebView];
[cell addSubview:self.tablesWebView];
self.tableView.userInteractionEnabled = YES;
}
if(self.selectedTabNumber == 1){
self.fixturesWebView.alpha = 1;
self.resultsWebView.alpha = 0;
self.tablesWebView.alpha = 0;
}
if(self.selectedTabNumber == 2){
self.fixturesWebView.alpha = 0;
self.resultsWebView.alpha = 1;
self.tablesWebView.alpha = 0;
}
if(self.selectedTabNumber == 3){
self.fixturesWebView.alpha = 0;
self.resultsWebView.alpha = 0;
self.tablesWebView.alpha = 1;
}
return cell;
}
First off, you should consider creating a custom cell rather than creating it at the tableview datasource. Second, when you are getting the webview content, consider using Asynchronous calls to avoid lock ups. This could be done in a block too. If you write delegate methods on the custom cell, you can let the tableview know when things are loaded or not etc.
But it looks like you are making it do too much work when the tableview needs to draw a cell.

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!

Resources