App suddenly crashes on an NSLog of my NSMutableArray - iphone

I have a very strange problem. I've made an app, everything works ok.
But now it suddenly crashes on my NSMutableArray.
Here is a screenshot of the situation
A few days ago everything worked normal. Can it be that this comes because of the update of XCODE and IOS 7 or is it something else?
Can somebody help me? if you need more detailed information. Please ask.
EDIT
The array comes from a mutableCopy of my fetchRequest results.
NSArray *matches = [context executeFetchRequest:fetchRequest error:nil];
newsArray = [matches mutableCopy];
EDIT 2
-(void)fetchNews{
newsArray = [[NSMutableArray alloc]init];
RKManagedObjectStore *store = [[TerbremeDataModel sharedDataModel] objectStore];
NSManagedObjectContext *context = store.mainQueueManagedObjectContext;
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:#"News"];
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:#"new_id" ascending:NO];
fetchRequest.sortDescriptors = #[descriptor];
NSArray *matches = [context executeFetchRequest:fetchRequest error:nil];
newsArray = [matches mutableCopy];
[tableview reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"NewsCell";
NewsCell *cell = (NewsCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"NewsCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"tablecellbg.png"]];
cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:#"tableArrow.png" ]];
News *news = [newsArray objectAtIndex:indexPath.row];
NSLog(#"news is %#",news);
static NSDateFormatter *df;
static NSDateFormatter *df2;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
df2 = [[NSDateFormatter alloc] init];
[df2 setDateFormat:#"d MMMM yyyy"];
});
cell.lblText.font = [UIFont fontWithName:#"GillSans" size:15.0];
cell.lblDate.font = [UIFont fontWithName:#"GillSans" size:15.0];
cell.lblDate.textColor = [UIColor colorWithRed:(154/255.0) green:(202/255.0) blue:(0/255.0) alpha:100];
cell.lblText.text = news.new_title;
NSDate *date = [df dateFromString:news.new_datepublished];
cell.lblDate.text = [df2 stringFromDate:date];
return cell;
}

If you are using ARC I would assign the newsArray using the objective c facilities, i.e. the generated accessor method.
So it would be
self.newsArray = [[NSMutableArray alloc]init];

Related

getting data in connectionDidFinishLoading,but ubable to use those data in cellForRowAtIndexPath method

I am developing an iphone application. In this i have to generate user profile form dynamically according to field information coming from the server.
So, if there are 5 fields in response i want ton create 5 labels from those data to display in cell of uitableview.
Not that i am getting the name of fields for user profile, not the values of profile.
I want to generate form dynamically from those data.
I'm able to get those data in NSMutableArray but in cellForRowAtIndexPath method its showing null.
How can i solve this?
My code snippet is as follow.
-(void) connectionDidFinishLoading:(NSURLConnection *)connection {
if (connection)
{
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
//You've got all the data now
//Do something with your response string
// NSLog(#"Response:%#",responseString);
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *object = [parser objectWithString:responseString error:nil];
NSString *pec_count = [object valueForKey:#"peculiarity_count"];
NSDictionary *pecs = [object valueForKey:#"peculiarities"];
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:nil];
[array addObject:#""];
for (int j= 1; j <= [pec_count integerValue] ; j++) {
NSString *val = [NSString stringWithFormat:#"%#%d",#"pec_",j];
NSString *pec_i = [pecs valueForKey:val];
NSString *modifiedString = [pec_i stringByReplacingOccurrencesOfString:#"_" withString:#" "];
NSString *capitalisedSentence = [modifiedString stringByReplacingCharactersInRange:NSMakeRange(0,1)
withString:[[modifiedString substringToIndex:1] capitalizedString]];
[array insertObject:capitalisedSentence atIndex:j];
}
self.peculiarity = array;
[self.table reloadData];
}
for (int j=0 ; j < [self.peculiarity count] ; j++) {
NSLog(#"info:%#", [self.peculiarity objectAtIndex: j]);
}
}
- (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];
UIButton *racebtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
racebtn.frame = CGRectMake(240, 7, 10, 15);
[racebtn setBackgroundImage:[UIImage imageNamed:#"select.png"] forState:UIControlStateNormal];
[racebtn addTarget:self action:#selector(selectRace:)forControlEvents:UIControlEventTouchUpInside];
NSLog(#"cell=%#",[self.peculiarity objectAtIndex:3]);
}
Any help will be appreciated.
Thank you.
in connectionDidFinishLoading: method replace the following line:
self.peculiarity = array; with,
self.peculiarity = [[NSMutableArray alloc] initWithArray:array];
and in cellForRowAtIndexPath method add the following line of code:
cell.textLabel.text = [self.peculiarity objectAtIndex:indexPath.row];
hope this will help you.
Note: If you did not allocated self.peculiarity NSMutableArray then alloc that array like bellow..
if (self.peculiarity == nil)
self.peculiarity = [[NSMutableArray alloc] init];
and then assign that array to this array like bellow..
self.peculiarity = array;
after in cellForRowAtIndexPath: method just set that value or name to the textLabel Of the UITableViewCell like bellow..
cell.textLabel.text = [self.peculiarity objectAtIndex:indexPath.row];
see whole example with that method..
- (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];
UIButton *racebtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
racebtn.frame = CGRectMake(240, 7, 10, 15);
[racebtn setBackgroundImage:[UIImage imageNamed:#"select.png"] forState:UIControlStateNormal];
[racebtn addTarget:self action:#selector(selectRace:)forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:racebtn];
cell.textLabel.text = [self.peculiarity objectAtIndex:indexPath.row];
}
return cell;
}
I already have checked all above solutions before, Then finally i got solution for this. The problem was that my cell was getting loaded before arrival of data. So i used synchronous request to the server.
Code is below for this :
NSString *path = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"path"];
NSString *address = [NSString stringWithFormat:#"%#%#%#%#", path,#"users/",#"peculiarity/",self.tablename];
NSURL *URL = [NSURL URLWithString:address];
NSLog(#"%#",address);
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[URL host]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLCacheStorageAllowedInMemoryOnly
timeoutInterval:60.0];
[request setHTTPMethod:#"GET"];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (data) {
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"data:%#",responseString);
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *object = [parser objectWithString:responseString error:nil];
pecs = [object valueForKey:#"pec_values"];
for (int j =0; j < [pecs count] ; j++) {
NSLog(#"values:%#",[pecs objectAtIndex:j]);
}
self.peculiarity = array;
}
else {
// Handle error by looking at response and/or error values
NSLog(#"%#",error);
}

Array goes empty in DidSelectRowAtIndexPath

I have in my coreDatabase an entity "Day". In a method I fetch the data from that entity and put it in a mutuableArray dayObjects. You can see the code over here.
NSManagedObjectContext *context = [RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext;
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:#"Day"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:
#"p_date >= %# and p_date <= %#",dateString,dateString2];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:#"p_id" ascending:YES];
fetchRequest.sortDescriptors = #[descriptor];
NSArray *matches = [context executeFetchRequest:fetchRequest error:nil];
NSLog(#"matches = %#",[matches valueForKey:#"p_date"]);
arrDateObjects = [matches mutableCopy];
In my cellForRow I do this
Day *objDay = [arrDateObjects objectAtIndex:indexPath.row-1];
Cell.titlelabel.text = day.p_from;
In my tableview the data is showed correctly. The problem is dat when I do the same thing in my DidSelectRowAtIndexPath. I always get a Null when I log day.p_from.
Can anybody help me ?
EDIT
My numberOfSections
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
int sections = 0;
sections = 1;
return sections;
}
My numberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int rows = 0;
rows = 8; //showing 7 days + 1 row for a little title
return rows;
}
My CellForRowAtIndexPath
static NSString *simpleTableIdentifier = #"OpeningCell";
OpeningCell *cell = (OpeningCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"OpeningCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if(indexPath.row == 0){
cell.lblDay.text = NSLocalizedString(#"lblDag", nil);
cell.lblFrom.text = NSLocalizedString(#"lblVan", nil);
cell.lblTill.text = NSLocalizedString(#"lblTot", nil);
}else{
Day *objDay = [arrDateObjects objectAtIndex:indexPath.row-1];
NSLog(#"Day object in cell from = %#",objDay.p_from);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd"];
NSDate *date = [dateFormat dateFromString:objDay.p_date];
NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];
[dateFormat2 setDateFormat:#"dd/MM"];
NSString *dateString = [dateFormat2 stringFromDate:date];
NSLog(#"date: %#", dateString);
cell.lblShort.text = dateString;
cell.lblDay.text = [arrDays objectAtIndex:indexPath.row-1];
NSString *txtFrom = [objDay.p_from substringWithRange:NSMakeRange(0, 5)];
cell.lblFrom.text = txtFrom;
NSString *txtTill = [objDay.p_till substringWithRange:NSMakeRange(0, 5)];
cell.lblTill.text = txtTill;
}
return cell;
NSLog(#"matches = %#",[matches valueForKey:#"p_date"]);
arrDateObjects=[[NSMutableArray alloc]init];
[arrDateObjects addobject:[matches objectAtindex:indexpath.row-1]];
Instead of
NSLog(#"matches = %#",[matches valueForKey:#"p_date"]);
arrDateObjects = [matches mutableCopy];
//try
arrDateObjects = [NSMutableArray arrayWithArray:matches];
//instead of
arrDateObjects = [matches mutableCopy];

how to display array of dictionaries in a tableview in iphone

hi friend i want display firstname and lastname both on cell but only firstname is displaying i try answer some one given me this link
This is the code used to obtain the 1st letter of each US State(stateName) in order to group them together alphabetically:
personarray = [[NSMutableArray alloc]init];
[personarray addObjectsFromArray:dislist.Peoplelistarray];
//short the personarray value:
NSSortDescriptor *asortDescriptor;
asortDescriptor=[NSSortDescriptor sortDescriptorWithKey:#"FirstName" ascending:YES selector:#selector(caseInsensitiveCompare:)];
//asortDescriptor=[[NSSortDescriptor alloc]initWithKey:#"TagName" ascending:YES];
NSArray *sortDescriptors=[NSArray arrayWithObject:asortDescriptor];
[self.personarray sortUsingDescriptors:sortDescriptors];
//---create the index---
Frstname = [[NSMutableArray alloc] init];
//check
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
for (NSDictionary *row in personarray) {
[tempArray addObject:[row valueForKey:#"FirstName"]];
for (int i=0; i<[tempArray count]; i++){
char alphabet = [[tempArray objectAtIndex:i] characterAtIndex:0];
NSString *uniChar = [NSString stringWithFormat:#"%C", alphabet];
if (![Frstname containsObject:uniChar]){
[Frstname addObject:uniChar];
}
I am having issues understanding how to populate a tableview cell with BOTH the #“FirstName" and #“LastName"(as the subtitle) after I use NSPredicate to sort the array
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [Mytableview dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
if (isSearchOn) {
NSString *cellValue = [searchResult objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
}
else {
//---get the letter in the current section---
NSString* alphabet = [Frstname objectAtIndex:[indexPath section]];
//---get all states beginning with the letter---
NSPredicate *predicate =
[NSPredicate predicateWithFormat:#"SELF beginswith[c] %#", alphabet];
NSArray *Names = [[personarray valueForKey:#"FirstName"] filteredArrayUsingPredicate:predicate];
if ([Names count]>0) {
//---extract the relevant firstname from the Names object---
NSString *cellValue = [Names objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
}
How about something like this, at the end of your code:
NSArray *firstNames = [[personarray valueForKey:#"FirstName"] filteredArrayUsingPredicate:predicate];
NSArray *lastNames = [[personarray valueForKey:#"LastName"] filteredArrayUsingPredicate:predicate];
if ([firstNames count]>0) {
//---extract the relevant firstname from the Names object---
NSString *firstName = [firstNames objectAtIndex:indexPath.row];
NSString *lastName = [lastNames objectAtIndex:indexPath.row];
NSString *cellValue = [NSString stringWithFormat:#"%# %#",firstName, lastName];
cell.textLabel.text = cellValue;
}

Cell's String Text Not Being Updated When 'Reload Table"

My cells' string text (TDBadgedCell) is only updated when I edit data in the table's child table.
If I edit the same data from a different tab, then go back to this table, the string will not update until I relaunch the app.
I have this code as well:
- (void)viewWillAppear:(BOOL)animated
{
[self refreshFetchedResultsController];
[self.logTableView reloadData];
}
Edit:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
TDBadgedCell *cell = [[[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(TDBadgedCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
cell.textLabel.textColor = [UIColor blackColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = [logArray objectAtIndex:indexPath.row];
cell.backgroundColor = [UIColor clearColor];
cell.imageView.image = [UIImage imageNamed:#"17-bar-chart.png"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM d, y"];
NSDate *date = nil;
NSDate *today = [NSDate date];
NSDate *thisWeek = [today dateByAddingTimeInterval: -604800.0];
NSDate *thisMonth = [today dateByAddingTimeInterval: -2629743.83];
if (indexPath.row == 0)
{
date = [NSDate date];
NSString *dateString = [dateFormatter stringFromDate:date];
cell.badgeString = dateString;
}
else if (indexPath.row == 1)
{
if ([[self.fetchedResultsController fetchedObjects]count] > 1)
{
self.session = [[self.fetchedResultsController fetchedObjects]objectAtIndex:1];
NSDate *date = self.session.timeStamp;
NSString *dateString = [dateFormatter stringFromDate:date];
cell.badgeString = dateString;
}
else
{
cell.badgeString = #"None";
}
}
else if (indexPath.row == 2)
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setPredicate: [NSPredicate predicateWithFormat:#"(timeStamp >= %#) AND (timeStamp <= %#)", thisWeek, today]];
[fetchRequest setEntity:[NSEntityDescription entityForName:#"Session" inManagedObjectContext:managedObjectContext]];
NSError *error = nil;
NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSLog(#"Fetch error: %#", error);
cell.badgeString = [NSString stringWithFormat:#"%i", [results count]];
[fetchRequest release];
}
else if (indexPath.row == 3)
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:#"(timeStamp >= %#) AND (timeStamp <= %#)", thisMonth, today]];
[fetchRequest setEntity:[NSEntityDescription entityForName:#"Session" inManagedObjectContext:managedObjectContext]];
NSError *error = nil;
NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSLog(#"Fetch error: %#", error);
cell.badgeString = [NSString stringWithFormat:#"%i", [results count]];
[fetchRequest release];
}
else if (indexPath.row == 4)
{
cell.badgeString = [NSString stringWithFormat:#"%i", [[self.fetchedResultsController fetchedObjects] count]];
NSLog(#"%i", [[self.fetchedResultsController fetchedObjects] count]);
}
UIImageView *myImageView = nil;
if (indexPath.row == 0 || indexPath.row == 1)
{
int myInt = cell.badgeString.length;
if (myInt > 11)
{
myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"silvercell7.png"]];
}
else if (myInt < 5)
{
myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"silvercell9.png"]];
}
else
{
myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"silvercell8.png"]];
}
}
else
{
myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"silvercell5.png"]];
}
[cell setBackgroundView:myImageView];
[myImageView release];
cell.badgeColor = [UIColor colorWithRed:24/255.0 green:83/255.0 blue:170/255.0 alpha:1.0];
[cell.badge setNeedsDisplay];
[dateFormatter release];
}
I think, you might be able to see the image for the newly added row. But not text Because you don't have support for that in your code.
In you code, you are always creating the new cell, Try with reusing the cell with below code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableString *CellIdentifier = [[NSMutableString alloc] initWithCapacity:10]
[CellIdentifier appendFormat:#"Cell_%d_%d",indexPath.section,indexPath.row];
TDBadgedCell *cell = (TDBadgedCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
TDBadgedCell *cell = [[[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}

UITableView Custom Cell very slow

I have a UITableView and I have created a custom cell to display my table. I have 6 UILables displayed and although I have only 20 records to show, it is very slow when I scroll.
This is how my - tableView: cellForRowAtIndexPath: looks like:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomCellIdentifier = #"CustomCellIdentifier";
HistoryCell *cell = (HistoryCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"HistoryCell" owner:nil options:nil];
for (id oneObject in nib)
if ([oneObject isKindOfClass:[UITableViewCell class]])
cell = (HistoryCell *) oneObject;
}
NSArray *object;
object = [cours objectForKey: [NSString stringWithFormat:#"%d", indexPath.section]];
History *rowData = [object objectAtIndex:indexPath.row];
if (rowData.month == 99) {
cell.hour.frame = CGRectMake(10, 0, 135, 35);
cell.data.hidden = YES;
cell.hour.textColor = [UIColor blackColor];
cell.hour.font = [UIFont fontWithName:#"Verdana" size:17];
} else {
cell.data.hidden = NO;
cell.hour.frame = CGRectMake(10, 16, 135, 19);
cell.hour.textColor = [UIColor grayColor];
cell.hour.font = [UIFont fontWithName:#"Verdana" size:12];
}
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"d (EEEE)"];
[formatter setLocale:self.selectedLanguageLocale];
NSString *stringFromDate = [formatter stringFromDate:rowData.data];
[formatter release];
cell.data.text = stringFromDate;
cell.hour.text = rowData.ora;
float Var1 = [rowData.Var2 floatValue];
float Var2 = [rowData.Var2 floatValue];
cell.R1.text = [self floatToStringFormat: [rowData.R1 floatValue]];
cell.R2.text = [self floatToStringFormat: [rowData.R2 floatValue]];
if (Var1 <= 0) {
cell.Var1.textColor = [UIColor greenColor];
} else {
cell.Var1.textColor = [UIColor redColor];
}
if (Var2 <= 0) {
cell.Var2.textColor = [UIColor greenColor];
} else {
cell.Var2.textColor = [UIColor redColor];
}
cell.Var1.text = [self floatToStringFormat:Var1];
cell.Var2.text = [self floatToStringFormat:Var2];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
The reason for running that slow on scroll is because of all the things I am doing here (NSDateFormatter, CGMakeRect, floatToStringFormat...) or is there something wrong with reusing cells?
floatToStringFormat is a function to format a number to 4 decimals:
- (NSString *)floatToStringFormat:(float)number{
NSNumberFormatter *myFloat = [[NSNumberFormatter alloc] init];
[myFloat setFormatterBehavior:NSNumberFormatterBehavior10_4];
[myFloat setNumberStyle:NSNumberFormatterDecimalStyle];
[myFloat setRoundingMode:NSNumberFormatterRoundHalfUp];
[myFloat setMinimumFractionDigits:4];
[myFloat setMaximumFractionDigits:4];
NSString *res = [myFloat stringFromNumber:[NSNumber numberWithFloat:number]];
[myFloat release];
return res;
}
Creating and setting up formatter objects is an expensive operation indeed, so I'd start with reusing your formatter objects since they are the same on each function call. So either make them static variables or instant variables in your data source class and create the following way:
//static variable case
NSDateFormatter *formatter = nil;
if (!formatter){
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"d (EEEE)"];
[formatter setLocale:self.selectedLanguageLocale];
}
NSString *stringFromDate = [formatter stringFromDate:rowData.data];
...
Firstly, you are using two different identifiers: CustomCellIdentifier and BanciHistoryCellIdentifier.
Secondly, do you really need to do everything after NSArray *object; every time a new cell is displayed? Because if you don't, you should move it to the if (cell == nil) { block.
From my experience the drawing of table view cells is significantly slowed down if you have three or more subviews (also depends on the device and the views though). Try to directly draw the content in drawRect: instead of using subviews, this should speed things up.
What are you doing here:
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"HistoryCell" owner:nil options:nil];
for (id oneObject in nib)
if ([oneObject isKindOfClass:[UITableViewCell class]])
cell = (HistoryCell *) oneObject;
}
Go read the documentation on how to do this properly. Secondly, if this is taking too long to convert the dates and numbers to strings then store the string values instead, and turn them into values when you need to modify them.
Do you have the CellIdentifier set in Interface builder? It must match exactly what you are using in code. Set a breakpoint where it loads the cell from the nib and make sure it's reusing cells when you scroll.