Reverse array content? - iphone

I am populating a table view with information, but would like it to be populated in reverse, so that any newly added cells (new email messages) would appear on the top, not the bottom.
What am I doing wrong?
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"MailCell";
MailCell *cell = (MailCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MailCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
// SLICK
// Anything that should be the same on EACH cell should be here.
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor colorWithRed:15.0/255.0 green:140.0/255.0 blue:198.0/255.0 alpha:1];
cell.selectedBackgroundView = myBackView;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.messageText.textAlignment = NSTextAlignmentLeft;
cell.messageText.lineBreakMode = NSLineBreakByTruncatingTail;
}
NSUInteger row = [indexPath row];
// Extract Data
// Use the message object instead of the multiple arrays.
CTCoreMessage *message = [[self allMessages] objectAtIndex:row];
// Sender
CTCoreAddress *sender = [message sender];
NSString *senderName = [sender name];
// Subject
NSString *subject = [message subject];
if ([subject length] == 0)
{
subject = #"(No Subject)";
}
// Body
BOOL isPlain = YES;
NSString *body = [message bodyPreferringPlainText:&isPlain];
body = [body stringByReplacingOccurrencesOfString:#"\n" withString:#" "];
body = [body stringByReplacingOccurrencesOfString:#"\r" withString:#" "];
// Populate Cell
[[cell nameText] setText:senderName];
[[cell subjectField] setText:subject];
[[cell messageText] setText:body];
if ([message isUnread])
{
UIColor *myColor = [UIColor colorWithRed:15.0/255.0 green:140.0/255.0 blue:198.0/255.0 alpha:1.0];
cell.nameText.textColor = myColor;
}
else
{
cell.nameText.textColor = [UIColor blackColor];
}
return cell;
}
How I am loading the array:
- (NSMutableArray *)allMessages
{
if (_allMessages == nil)
{
_allMessages = [[NSMutableArray alloc] init];
}
return _allMessages;
}

You are pulling from NSArray Index [indexPath row] meaning you are starting at index 0 and going to n. Which means you are not in reverse order. You need to reverse your array first. A simple way would be:
- (void)viewWillAppear:(BOOL)animated
{
NSArray *allMessages = [self allMessages];
NSArray* reversedMessages = [[allMessages reverseObjectEnumerator] allObjects];
}
Then in your cellForRowAtIndexPath method you can do:
CTCoreMessage *message = [reversedMessages objectAtIndex:row];

Related

Table display only 2 labels (out of 4) when use the search Bar

I have a table with 4 labels which works fine. When I use the search bar, which also works fine, the table displays only two labels:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"airports" ofType:#"json"];
NSString *JSONData = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
NSArray *airports = [NSJSONSerialization JSONObjectWithData:[JSONData dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
finalArray = [[NSArray alloc]init];
finalArray = airports;
}
-(void)filterContentForSearchText:(NSString *)searchText{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"SELF.airport_name contains[cd] %#", searchText];
self.searchResults = [finalArray filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
[self filterContentForSearchText:searchString];
return YES;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.tableView) {
return [finalArray count];
}else{
return [searchResults count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{static NSString *simpleTableIdentifier = #"AirportCell";
AirportCell *cell = (AirportCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"AirportCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSString *airportName = [[NSString alloc]init];
NSString *iataCode = [[NSString alloc]init];
NSString *icaoCode = [[NSString alloc]init];
NSString *countryAirport = [[NSString alloc]init];
if (tableView == self.tableView) {
airportName = [[finalArray objectAtIndex:indexPath.row] objectForKey:#"airport_name"];
iataCode = [[finalArray objectAtIndex:indexPath.row] objectForKey:#"iata_code"];
icaoCode = [[finalArray objectAtIndex:indexPath.row] objectForKey:#"icao_code"];
countryAirport = [[finalArray objectAtIndex:indexPath.row] objectForKey:#"country"];
cell.iataCodeLabel.text = iataCode;
cell.iataCodeLabel.font = [UIFont fontWithName:#"Verdana" size:13];
cell.icaoCodeLabel.text = icaoCode;
cell.icaoCodeLabel.font = [UIFont fontWithName:#"Verdana" size:13];
cell.airportNameLabel.text = airportName;
cell.airportNameLabel.font = [UIFont fontWithName:#"Verdana" size:13];
cell.countryLabel.text = countryAirport;
cell.countryLabel.font = [UIFont fontWithName:#"Verdana" size:13];
}else{
airportName = [[searchResults objectAtIndex:indexPath.row] objectForKey:#"airport_name"];
iataCode = [[searchResults objectAtIndex:indexPath.row] objectForKey:#"iata_code"];
icaoCode = [[searchResults objectAtIndex:indexPath.row] objectForKey:#"icao_code"];
countryAirport = [[searchResults objectAtIndex:indexPath.row] objectForKey:#"country"];
cell.iataCodeLabel.text = iataCode;
cell.iataCodeLabel.font = [UIFont fontWithName:#"Verdana" size:13];
cell.icaoCodeLabel.text = icaoCode;
cell.icaoCodeLabel.font = [UIFont fontWithName:#"Verdana" size:13];
cell.airportNameLabel.text = airportName;
cell.airportNameLabel.font = [UIFont fontWithName:#"Verdana" size:13];
cell.countryLabel.text = countryAirport;
cell.countryLabel.font = [UIFont fontWithName:#"Verdana" size:13];
}
return cell;
}
I believe that your searchDisplayController is altering the height of your cells.
The answer located on this question may be of help to you.
You may find that "AirportCell" is registered only for the main tableview - and not for the search tableview.
[self.searchDisplayController.searchResultsTableView registerClass:[AirportCell class] forCellReuseIdentifier: simpleTableIdentifier];

using of two different type of custom cell in same table view?

thanks in advance.
in my app, i have a tableview, in which i have to use two different style of custom cell, i made two custom cell, and in tableView cellForRowAtIndexPath method i used two identifier for cell, even i tried for two section. but its not working. it is giving me "EXE BAD Excess" or some time other kind of error. below is my code.
Error : thread1_EXE_BAD_Access(code = 2 ,address 0 x 0)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
//CatIdentifier
static NSString *CellIdentiFier = #"CatIdentifier";
static NSString *Cell1IdentiFier = #"CatIdentifier1";
if (indexPath.section == 0)
{
CommitteCell *cell = ( CommitteCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentiFier];
if(cell == nil)
{
cell = ( CommitteCell *)[[[NSBundle mainBundle] loadNibNamed:#"CommitteeCell" owner:self options:nil] objectAtIndex:0];
}
if (indicator == 1)
{
cell.lblName.text = str;
}
else
{
cell.lblName.text = [arrayName objectAtIndex:indexPath.row];
cell.lblPost.text = [arrayPost objectAtIndex:indexPath.row];
cell.picimg.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[arrayimage objectAtIndex:indexPath.row]]]];
}
cell.backgroundView = [[UIImageView alloc]init];
UIImage *img = [UIImage imageNamed:#"link-bg 2.png"];
((UIImageView *)cell.backgroundView).image = img;
return cell;
}
else
{
Committee2Cell *cell1 = (Committee2Cell *)[tableView dequeueReusableCellWithIdentifier:Cell1IdentiFier];
if(cell1 == nil)
{
cell1 = (Committee2Cell *)[[[NSBundle mainBundle] loadNibNamed:#"Committee2Cell" owner:self options:nil] objectAtIndex:0];
}
cell1.lblPost1.text = strPost;
cell1.txtName.text = strName;
cell1.backgroundView = [[UIImageView alloc]init];
UIImage *img = [UIImage imageNamed:#"link-bg 2.png"];
((UIImageView *)cell1.backgroundView).image = img;
return cell1;
}
}
section in tableview and rows in section method are as below.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section)
{
case 0:
return [arrayName count]-1;
break;
case 1:
return 1;
break;
default:
break;
}
return 0;
}
please if anyone can fine that where is my mistake . thanks again.
data of array and label is as below.
-(void)NewsParser:(NSMutableDictionary *)dic
{
NSLog(#"dic = %#",dic);
arrayName = [[NSMutableArray alloc]init];
arrayPost = [[NSMutableArray alloc]init];
arrayimage= [[NSMutableArray alloc]init];
strPost = [[NSString alloc]init];
strName = [[NSString alloc]init];
strPost = [[dic valueForKey:#"post"]objectAtIndex:8];
strName = [[dic valueForKey:#"name"]objectAtIndex:8];
NSLog(#"Name = %#",strName);
NSLog(#"Post = %#",strPost);
for(int i=0;i<[dic count]-1;i++)
{
[arrayName addObject:[[dic valueForKey:#"name"]objectAtIndex:i]];
[arrayPost addObject:[[dic valueForKey:#"post"]objectAtIndex:i]];
[arrayimage addObject:[[dic valueForKey:#"pic"]objectAtIndex:i]];
}
NSLog(#"array = %#",arrayName);
NSLog(#"array = %#",arrayPost);
NSLog(#"array = %#",arrayimage);
[table1 reloadData];
}
I think a cleaner approach would be to use a container view with two different kind of cells and then selectively show/hide the view relevant for that cell. This would be easier to code and maintain but might consume a little more memory.
You R making reUsable identifier as only once . Do something like this :
-(UITableViewCell *)tableView : (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString* identifier;
if(indexPath.section == 0)
identifier = #"0";
else
identifier = #"1";
self.tableView.dataSource = self;
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];
if( cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] ;
if (indexPath.section == 0)
{
if(cell == nil)
{
cell = ( CommitteCell *)[[[NSBundle mainBundle] loadNibNamed:#"CommitteeCell" owner:self options:nil] objectAtIndex:0];
}
if (indicator == 1)
{
cell.lblName.text = str;
}
else
{
cell.lblName.text = [arrayName objectAtIndex:indexPath.row];
cell.lblPost.text = [arrayPost objectAtIndex:indexPath.row];
cell.picimg.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[arrayimage objectAtIndex:indexPath.row]]]];
}
cell.backgroundView = [[UIImageView alloc]init];
UIImage *img = [UIImage imageNamed:#"link-bg 2.png"];
((UIImageView *)cell.backgroundView).image = img;
return cell;
}
Use like follow its work in my code smoothly , if you need more help let me know :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(YOUR CONDITION HERE)
ShareActionViewCell *shareCell;
NSString *ShareCellId = [NSString stringWithFormat:#"ShareCell%d",indexPath.row];
shareCell = (ShareActionViewCell *)[tableView dequeueReusableCellWithIdentifier:ShareCellId];
if(!shareCell) {
shareCell = [[ShareActionViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ShareCellId];
}
shareCell.selectionStyle = UITableViewCellSelectionStyleNone;
shareCell.ShareTitle.text = [NSString stringWithFormat:#"%#",[tbldata objectAtIndex:indexPath.row]];
} else {
CustCell *dataCell;
NSString *DataCellId = [NSString stringWithFormat:#"DataCell%d",indexPath.row];
dataCell = (CustCell *)[tableView dequeueReusableCellWithIdentifier:DataCellId];
if(!dataCell) {
dataCell = [[CustCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DataCellId];
}
shareCell.selectionStyle = UITableViewCellSelectionStyleNone;
shareCell.ShareTitle.text = [NSString stringWithFormat:#"%#",[tbldata objectAtIndex:indexPath.row]];
}
}
Suggested using -objectForKey for a Dictionary:
[[dic objectForKey:#"post"] objectAtIndex:8];
Make sure there is a NSArray object at name/post/ pic keyed to dic
And, in your for loop:
for(int i=0;i<[dic count]-1;i++)
{
[arrayName addObject:[[dic valueForKey:#"name"] objectAtIndex: i]];
[arrayPost addObject:[[dic valueForKey:#"post"]objectAtIndex:i]];
[arrayimage addObject:[[dic valueForKey:#"pic"]objectAtIndex:i]];
}
are you sure [dic count] <= [dic objectForKey:#"name"]?
add a nil to array will be crashed.
4.Where did you call the method -(void)NewsParser:(NSMutableDictionary *)dic;, If your data array is correct, maybe the [table1 reloadData]; crashed.

NSMutableArray Display Null Value in Table View

- (void)viewDidLoad {
self.detailView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];
self.detailView.dataSource = self;
self.detailView.delegate = self;
self.detailView.multipleTouchEnabled=YES;
[self.view addSubview:self.detailView];
[super viewDidLoad];
self.title = NSLocalizedString(#"Routes", nil);
self.detailArray = [[NSMutableArray alloc] init];
NSString *url = [NSString stringWithFormat:#"http://maps.googleapis.com/maps/api/directions/json?origin=Chennai&destination=Madurai&sensor=false"];
NSURL *googleRequestURL=[NSURL URLWithString:url];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
NSString *someString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSError* error;
NSMutableDictionary* parsedJson = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSArray *allkeys = [parsedJson allKeys];
for(int i = 0; i < allkeys.count; i++){
if([[allkeys objectAtIndex:i] isEqualToString:#"routes"]){
NSArray *arr = [parsedJson objectForKey:#"routes"];
NSDictionary *dic = [arr objectAtIndex:0];
NSArray *legs = [dic objectForKey:#"legs"];
for(int i = 0; i < legs.count; i++){
NSArray *stepsArr = [[legs objectAtIndex:i] objectForKey:#"steps"];
for (int i = 0; i < stepsArr.count; i++) {
NSLog(#"HTML INSTRUCTION %#", [[stepsArr objectAtIndex:i] objectForKey:#"html_instructions"]);
NSLog(#"############################");
[self.detailArray addObject:[[stepsArr objectAtIndex:i] objectForKey:#"html_instructions"] ];
}
}
}
}
});
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{
return [self.detailArray count];
}
-(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];
}
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 5.0f, 300.0f, 30.0f)];
label.text=[NSString stringWithFormat:#"%#",[self.detailArray objectAtIndex:indexPath.row]];
label.numberOfLines = 3;
label.font = [UIFont fontWithName:#"Helvetica" size:(12.0)];
label.lineBreakMode = UILineBreakModeWordWrap;
label.textAlignment = UITextAlignmentLeft;
[cell.contentView addSubview:label];
[label release];
return cell;
}
i want to display the detailArray in the Table View.But it display null values in Table View.But in NSlog it display current values.The detailArray having set of direction values. is any possible to display in TableView.if i give constant value in displayArray its show correctly in TableView. please help me if Any one know.i am waiting for your valuable Answers,Thanks.
UPDATED
After add whole data (values) in after retain it and reload TableView like bellow...
[self.detailArray retain];
[self.detailView reloadData];
i am sure your problem solved... :)
Remove [self.detailView reloadData]; from cellForRowAtIndexPath method. and also remove tableView.delegate = self; in same method.
and remember [super viewDidLoad]; must write at top of viewDidLoad.
Edit
just change your four array as below.
NSArray *legs=(NSArray *)[dic objectForKey:#"legs"];
NSArray *arr = (NSArray *)[parsedJson objectForKey:#"routes"];
NSArray *legs = (NSArray *)[dic objectForKey:#"legs"];
NSArray *stepsArr = (NSArray *)[[legs objectAtIndex:i] objectForKey:#"steps"];
Why are you setting detailView delegate and datasource 2 times in viewDidLoad. Please format the code first then it will be easy for us to describe the problem.
self.detailView.dataSource=self;
self.detailView.delegate=self;
self.detailView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];
self.detailView.dataSource = self;
self.detailView.delegate = self;

UITableview doubts

i am having an application which can share Notes to evernote,it works fine i can upload and download my Note to Evernote within the app,i have a Note title and Note subject,i uploaded the Notes to evernote viz UITableview controller.The Textlabel.text is the title for the note and detailtextlabel.text is the note subject.i only get the Notesubject correctly means ,if i have two notes in tableview with title,like this formate 1) title :firstitle ,notesubject :firstNotessubject 2)title :secondtitle ,notesubject :secondNotessubject,,then i press the upload button it uploads the notes to evernote,but the title remains firstitle only with notes,like this formate 1) title :firstitle ,notesubject :firstNotessubject 2)title :firsttitle ,notesubject :secondNotessubject.my code for uplode button is
-(IBAction)sendNoteEvernote:(id)sender{
NSMutableString *strtitle = [[NSMutableString alloc] initWithString:#""];
for (int t = 0; t<[appDelegate.indexArray count]; t++) {
NSString * aStringtitle = [[NSString alloc] initWithString:[appDelegate.indexArray objectAtIndex:t]] ;
note.title =aStringtitle;
}
NSMutableString *str = [[NSMutableString alloc] initWithString:#"NOTES:"];
for (int i = 0; i<[appDelegate.notesArray count]; i++) {
NSString * aString = [[NSString alloc] initWithString:[appDelegate.notesArray objectAtIndex:i]] ;
NSString * ENML= [NSString stringWithFormat:#"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">\n<en-note>%#",aString];
ENML = [NSString stringWithFormat:#"%#%#", ENML, #"</en-note>"];
NSLog(#"%#", ENML);
// Adding the content & resources to the note
[note setContent:ENML];
// [note setTitle:aStringtitle];
// [note setResources:resources];
// Saving the note on the Evernote servers
// Simple error management
#try {
[[eversingleton sharedInstance] createNote:note];
_acteverbackup.hidden = YES;
_actimageeverbackup.hidden =YES;
}
#catch (EDAMUserException * e) {
_acteverbackup.hidden = YES;
_actimageeverbackup.hidden =YES;
NSString * errorMessage = [NSString stringWithFormat:#"Error saving note: error code %i", [e errorCode]];
proAlertView *alert = [[proAlertView alloc]initWithTitle:#"Evernote" message:errorMessage delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alert setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0] withStrokeColor:[UIColor colorWithHue:0.0 saturation:0.0 brightness:0.0 alpha:1.0]];
[alert show];
[alert release]; return;
}
in the above code appdelegate.indexarray is the title and appdelegate.notearray is the Subject..my UItableview code look like this.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return[appDelegate.notesArray count];
}
/*-(UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypesForRowWithIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellAccessoryCheckmark;
}*/
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
NSString *CellIdentifier;
CellIdentifier=[NSString stringWithFormat:#"cell %d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.numberOfLines = 0;
UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"bgCELL3#2X-1"]];
[cell setBackgroundView:img];
[img release];
count++;
}
NSMutableString *strr=[[NSMutableString alloc]initWithString:[appDelegate.indexArray objectAtIndex:indexPath.section]];
cell.textLabel.text =strr ;
cell.textLabel.text = [appDelegate.indexArray objectAtIndex:row];
cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:14.0];
cell.textLabel.textColor = [UIColor brownColor];
//NSMutableString *notes=[[NSMutableString alloc]initWithString:[appDelegate.notesArray objectAtIndex:row]];
//cell.detailTextLabel.text =notes;
cell.detailTextLabel.font = [UIFont fontWithName:#"Georgia" size:14.0];
cell.detailTextLabel.textColor = [UIColor darkGrayColor];
cell.detailTextLabel.text = [appDelegate.notesArray objectAtIndex:row];
//textView.text=notes;
//[notes release];
cell.backgroundColor=[UIColor clearColor];
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
whats the error in my code,please help me to do this.
thanks in advance
Try changing the line:
NSMutableString *strr = [[NSMutableString alloc] initWithString:[appDelegate.indexArray objectAtIndex:indexPath.section]];
to:
NSMutableString *strr = [[NSMutableString alloc] initWithString:[appDelegate.indexArray objectAtIndex: row]];
Also get rid of the:
NSUInteger row = [indexPath row];
NSString *CellIdentifier;
CellIdentifier = [NSString stringWithFormat:#"cell %d",indexPath.row];
simply use:
CellIdentifier = #"noteCellIdentifier";
This doesn't have anything to do with your question but it's what cell reusing is all about.
EDIT:
This part of code also looks suspitious:
for (int t = 0; t<[appDelegate.indexArray count]; t++) {
NSString * aStringtitle = [[NSString alloc] initWithString:[appDelegate.indexArray objectAtIndex:t]] ;
note.title =aStringtitle;
}
It's not clear what you want to do but you'd probably want to move this to:
for (int i = 0; i<[appDelegate.notesArray count]; i++) {
NSString * aStringtitle = [[NSString alloc] initWithString:[appDelegate.indexArray objectAtIndex:i]]; //note: t changed to i
note.title =aStringtitle;
NSString * aString = [[NSString alloc] initWithString:[appDelegate.notesArray objectAtIndex:i]];
NSString * ENML= [NSString stringWithFormat:#"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">\n<en-note>%#",aString];
ENML = [NSString stringWithFormat:#"%#%#", ENML, #"</en-note>"];
NSLog(#"%#", ENML);
//....

iPhone app crashes after execution of cellForRowAtIndexPath method of tableView

App crashes after execution of cellForRowAtIndexPath method of tableView
It goes into:
UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:]
and crashes out.
There is no error shown in Console. It shows EXC_BAD_EXCESS in Status bar of Xcode.
What could be wrong?
EDIT 1:
This is the whole code in my tableView's cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
static NSString *newCellIdentifier = #"NewCell";
UITableViewCell *cell = nil;
NSUInteger row = [indexPath row];
NSUInteger count;
if (searching==YES) {
count = [searchCellTextArray count];
}else {
count = [cellTextArray count];
}
if(row==count)
{
cell = [tableView dequeueReusableCellWithIdentifier:newCellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:newCellIdentifier] autorelease];
}
cell.textLabel.text = #"Load more items...";
cell.textLabel.textColor = [UIColor blueColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
UIImage *cellImage = [[UIImage alloc] init];
NSString *imgName;
//Searching
if(searching==YES && [searchCellTextArray count] != 0)
{
NSString *decodedImageName = [NSString stringWithUTF8String:[[[showSearchImageArray objectAtIndex:indexPath.row]valueForKey:#"image"] cStringUsingEncoding:[NSString defaultCStringEncoding]]];
imgName = decodedImageName;
cell.textLabel.textColor = [UIColor redColor];
cell.textLabel.text=[searchCellTextArray objectAtIndex:indexPath.row];
cell.detailTextLabel.font= [UIFont boldSystemFontOfSize:18];
cell.detailTextLabel.textColor = [UIColor blackColor];
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.numberOfLines= [lableNameArray count]+2;
cell.detailTextLabel.text = [searchDetailTextArray objectAtIndex:indexPath.row];
}
else
{ //searching
NSString *decodedImageName = [NSString stringWithUTF8String:[[[showImageArray objectAtIndex:indexPath.row] valueForKey:#"image"] cStringUsingEncoding:[NSString defaultCStringEncoding]]];
imgName = decodedImageName;
cell.textLabel.textColor= [UIColor redColor];
cell.textLabel.text = [cellTextArray objectAtIndex:indexPath.row];
cell.detailTextLabel.font= [UIFont boldSystemFontOfSize:18];
cell.detailTextLabel.textColor = [UIColor blackColor];
cell.detailTextLabel.numberOfLines= [lableNameArray count]+2;
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.text = [detailTextArray objectAtIndex:indexPath.row];
}
if (imgName !=(NSString*)[NSNull null] && ![imgName isEqualToString:#""] && ![imgName isEqualToString:#"X"])
{
NSLog(#" Image Name : %#",imgName);
NSString *documentsDirectory = [self getImagePath];
NSError *error1;
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error1];
if (files == nil)
{
NSLog(#"Error reading contents of documents directory: %#", [error1 localizedDescription]);
}
for (NSString *file in files)
{
NSLog(#"Loop Entered");
if([file isEqualToString:[NSString stringWithFormat:#"%#_thumb.png",imgName]])
{
NSLog(#"Image: %# %#",file,imgName);
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:file];
cellImage = [UIImage imageWithContentsOfFile:fullPath];
NSLog(#"Full Path: %#",fullPath);
}
}
cell.imageView.image = cellImage;
}
else
{
cell.imageView.image = [UIImage imageNamed:#"GlossaryGhostImg1.png"];
}
}
NSLog(#"Cell For Row At Index Path Done");
return cell;
}
Are there memory leaks here, and have I over-released any objects?
Could be many things: You return a garbage cell. You released your cell too many times. You autoreleased some component of your cell too many times. Sounds like a memory management issue, check your retain/releases carefully. And post your cellForRowAtIndexPath code.
Try to add NSZombie environment variable. Then check your app. May be you will find the root cause of the crash.