iPhone app crashes after execution of cellForRowAtIndexPath method of tableView - iphone

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.

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.

Reverse array content?

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];

UITableViewCell(s) with default image overwritten with other images upon scrolling

Oops,I am facing an issue with table view cells having no image,i.e. the cell with default image.Upon scrolling the default image disappears and some image from a cell is appearing on it.I have implemented the suggestion from Mr.Rckoenes here .Even then I was unable to fix the issue.Here is my implementation code for understanding:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ReminderClass *reminderToDisplay = [self.remindersArray objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
// Now create the cell to display the data
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier] autorelease];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:kHelvetica size:17.0];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.backgroundColor = [UIColor clearColor];
}
......
if (Image != nil)
{
UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(240, 3, 70, 63)];
imageView.backgroundColor=[UIColor clearColor];
[imageView setImage:Image];
cell.accessoryView = imageView;
[imageView release];
}
else
{
UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(240, 3, 70, 63)];
imageView.backgroundColor=[UIColor clearColor];
UIImage *defaultImage = [UIImage imageNamed:kDefaultImage];
[imageView setImage:defaultImage];
cell.accessoryView = imageView;
[imageView release];
}
cell.textLabel.text = reminderDetailsString;
return cell;
}
Can any one please help me,thanks in advance :)
just set dequeueReusableCellWithIdentifier to nil and also reuseIdentifier to nil like bellow..
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
and
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
and also add your other code in this if (cell == nil) if condition..
UPDATE:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell%d%d",indexPath.section,indexPath.row];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:kHelvetica size:17.0];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.backgroundColor = [UIColor clearColor];
tableView.backgroundColor = [UIColor clearColor];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc]init]autorelease];
[dateFormat setDateFormat:kDateFormat];
NSDate *reminderDate = [dateFormat dateFromString:reminderToDisplay.Date];
[dateFormat setDateFormat:kMinDateFormat];
NSString *dateString = [dateFormat stringFromDate:reminderDate];
NSString *valueString = [NSString stringWithFormat:kNameEvent,reminderToDisplay.Name,reminderToDisplay.Event];
NSString *onString = [NSString stringWithFormat:kOn,dateString];
NSString *reminderDetailsString = [valueString stringByAppendingString:onString];
ABAddressBookRef addressbook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressbook);
CFIndex numPeople = ABAddressBookGetPersonCount(addressbook);
for (int i=0; i < numPeople; i++)
{
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
NSString *firstName=(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName=(NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSMutableDictionary *contactsDictionary = [[[NSMutableDictionary alloc]init]autorelease];
if(firstName != nil && firstName != NULL)
{
[contactsDictionary setObject:firstName forKey:kFirstName];
CFRelease(firstName);
}
else
{
[contactsDictionary setObject:#"" forKey:kFirstName];
}
if(lastName != nil && lastName != NULL)
{
[contactsDictionary setObject:lastName forKey:kLastName];
CFRelease(lastName);
}
else
{
[contactsDictionary setObject:#"" forKey:kLastName];
}
//Get the first name and last name added to dict and combine it to form contact name
firstName = [[[NSString alloc]initWithString:[contactsDictionary objectForKey:kFirstName]]autorelease];
lastName = [NSString stringWithFormat:#" %#",[contactsDictionary objectForKey:kLastName]];
self.contactName = [firstName stringByAppendingString:lastName];
//Now check whether the contact name is same as your reminderToDisplay.Name
if([reminderToDisplay.Name isEqualToString:contactName] && ABPersonHasImageData(person))
{
CFDataRef imageData = ABPersonCopyImageData(person);
self.reminderImage = [UIImage imageWithData:(NSData *)imageData];
CFRelease(imageData);
}
}
CFRelease(allPeople);
CFRelease(addressbook);
if ([reminderToDisplay.reminderGroup isEqualToString:kFacebook])
{
NSString *imageName = [NSString stringWithFormat:kImageName,reminderToDisplay.Name];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *reminderString=[documentsDirectory stringByAppendingPathComponent:[NSString stringWithString:imageName]];
self.reminderImage = [UIImage imageWithContentsOfFile:reminderString];
}
if (reminderImage != nil)
{
UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(240, 3, 70, 63)];
imageView.backgroundColor=[UIColor clearColor];
[imageView setImage:reminderImage];
cell.accessoryView = imageView;
[imageView release];
}
else
{
UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(240, 3, 70, 63)];
imageView.backgroundColor=[UIColor clearColor];
UIImage *defaultImage = [UIImage imageNamed:kDefaultImage];
[imageView setImage:defaultImage];
cell.accessoryView = imageView;
[imageView release];
}
cell.textLabel.text = reminderDetailsString;
}
return cell;
}

Insert NSarray items into specified UItableviewcell

I have a UItableview with 10 cells.I need to display a message from an NSarray into this tableview.The array contains 3 items and needs to be displayed in the any of the 3 cells .
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomTableView *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ CustomTableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSString *order = [orderArray objectAtIndex:indexPath.row];
NSString *sdate = [dateArray objectAtIndex:indexPath.row];
//Label positions
UILabel *ordernum = [[UILabel alloc] initWithFrame:CGRectMake(5,28,95,21)];
UILabel *date = [[UILabel alloc] initWithFrame:CGRectMake(100,28,80,21)];
UILabel *orderStatusMessage = [[UILabel alloc] initWithFrame:CGRectMake(200,28,80,21)];
NSString *ordertypeName = [ordertypeArray objectAtIndex:indexPath.row];
ordernum.textColor = [UIColor blackColor];
[cell.contentView addSubview:ordernum];
date.textColor = [UIColor blackColor];
[cell.contentView addSubview:date];
orderStatusMessage.textColor = [UIColor blackColor];
if ([ordertypeName isEqualToString:#"saved"]) {
ordernum.text = [NSString stringWithString:#"Saved Order"];
date.text = [NSString stringWithFormat:#"%#",sdate];
}
else{
if (![order isEqualToString:#""]) {
for (int i=0; i<[reversed count]; i++)
{
stat=[reversed objectAtIndex:i];
}
}
ordernum.text = [NSString stringWithFormat:#"%#",order];
date.text = [NSString stringWithFormat:#"%#",sdate];
orderStatusMessage.text =[NSString stringWithFormat:#"%#",stat];
[cell.contentView addSubview:orderStatusMessage];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
// }
return cell;
}
With the above code I am getting only the last item from reversed array.Please help
To start, your for loop is bogus
if (![order isEqualToString:#""]) {
for (int i=0; i<[reversed count]; i++)
{
stat=[reversed objectAtIndex:i];
}
}
Why are you looping and overwriting the reference to stat with each iteration? That's clearly not your intent.
Second, I would advise refactoring, since this code snippit is extremely confusing and it is very close to impossible to understand the intent. Please repost after renaming variables and cleaning the code a bit.