removeObjectAtIndex crashing the app - objective-c

In the .h file I have set the below line.
NSMutableArray *repArray;
In the .m file I have placed the below lines.(I put just the required lines of code for this ex)
-(void)read_data_fromDB
{
objectsForCharacters = [[NSMutableDictionary alloc]init];
sqlite3 *db = [contactAppDelegate getNewDBConnection];
NSMutableString *query = nil;
query = [NSMutableString stringWithFormat:#"select representative, title, business_name, uid from corporate where profiler_type <> 'OWN_CORPORATE' order by representative asc"];
const char *sql = [query UTF8String];
sqlite3_stmt *selectAllStmt = nil;
if(sqlite3_prepare_v2(db,sql, -1, &selectAllStmt, NULL)!= SQLITE_OK)
NSAssert1(0,#"error preparing statement",sqlite3_errmsg(db));
else
{
repArray = [[NSMutableArray alloc]init];
businessArray = [[NSMutableArray alloc]init];
titleArray = [[NSMutableArray alloc]init];
uidArray = [[NSMutableArray alloc]init];
while(sqlite3_step(selectAllStmt)==SQLITE_ROW)
{
char *chrstr =(char *)sqlite3_column_text(selectAllStmt, 0);
if(chrstr !=NULL)
{
corpRepresentative = [NSString stringWithUTF8String:chrstr];
[repArray addObject:corpRepresentative];
}
chrstr =(char *)sqlite3_column_text(selectAllStmt, 1);
if(chrstr !=NULL)
{
corpTitle = [NSString stringWithUTF8String:chrstr];
[titleArray addObject:corpTitle];
}
chrstr =(char *)sqlite3_column_text(selectAllStmt, 2);
if(chrstr !=NULL)
{
corpBusiness_name = [NSString stringWithUTF8String:chrstr];
[businessArray addObject:corpBusiness_name];
}
chrstr =(char *)sqlite3_column_text(selectAllStmt, 3);
if(chrstr !=NULL)
{
corporteUid = [NSString stringWithUTF8String:chrstr];
[uidArray addObject:corporteUid];
}
}
}
sqlite3_finalize(selectAllStmt);
sqlite3_close(db);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UILongPressGestureRecognizer *longPressGesture =
[[[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(longPress:)] autorelease];
[cell addGestureRecognizer:longPressGesture];
}
// Configure the cell...
UILabel *txtLbl = [[UILabel alloc]initWithFrame:CGRectMake(70, 0, 175, 26)];
//[txtLbl setBackgroundColor:[UIColor greenColor]];
[txtLbl setTextAlignment:UITextAlignmentLeft];
NSString *txtStr = [repArray objectAtIndex:indexPath.row];
UIFont *fontTxtLbl = [UIFont fontWithName: #"FuturaMdBT" size:16];
[txtLbl setFont:fontTxtLbl];
[txtLbl setText:txtStr];
[cell.contentView addSubview:txtLbl];
[txtLbl release];
UILabel *txtDetailLbl = [[UILabel alloc]initWithFrame:CGRectMake(70, 27, 175, 20)];
// [txtDetailLbl setBackgroundColor:[UIColor redColor]];
[txtDetailLbl setTextAlignment:UITextAlignmentLeft];
NSString *txtDetailStr = [titleArray objectAtIndex:indexPath.row];
UIFont *fontTxtDetailLbl = [UIFont fontWithName: #"Arial" size:12];
[txtDetailLbl setFont:fontTxtDetailLbl];
[txtDetailLbl setText:txtDetailStr];
[cell.contentView addSubview:txtDetailLbl];
[txtDetailLbl release];
UILabel *txtDetailLbl1 = [[UILabel alloc]initWithFrame:CGRectMake(70, 47, 175, 20)];
//[txtDetailLbl1 setBackgroundColor:[UIColor blueColor]];
[txtDetailLbl1 setTextAlignment:UITextAlignmentLeft];
NSString *txtDetailStr1 = [businessArray objectAtIndex:indexPath.row];
UIFont *fontTxtDetailLbl1 = [UIFont fontWithName: #"Arial" size:12];
[txtDetailLbl1 setFont:fontTxtDetailLbl1];
[txtDetailLbl1 setText:txtDetailStr1];
[cell.contentView addSubview:txtDetailLbl1];
[txtDetailLbl1 release];
// cell.textLabel.text = [repArray objectAtIndex:indexPath.row];
//cell.detailTextLabel.text = [titleArray objectAtIndex:indexPath.row];
cell.imageView.image=[imagearray objectAtIndex:indexPath.row];
UIView *viewSelected = [[[UIView alloc] init] autorelease];
viewSelected.backgroundColor = [UIColor colorWithRed:224.0/255.0
green:229.0/255.0
blue:241.0/255.0
alpha:1.0];
cell.selectedBackgroundView = viewSelected;
cell.textLabel.highlightedTextColor=[UIColor colorWithRed:46.0/255.0
green:77.0/255.0
blue:141.0/255.0
alpha:1.0];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *tempUID = [uidArray objectAtIndex:indexPath.row];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:tempUCID forKey:#"corpUcid"];
[prefs setObject:#"PROFILER" forKey:#"selProfiler"];
CorpPrevFront *corpPrevFront = [[CorpPrevFront alloc]initWithNibName:#"CorpPrevFront" bundle:nil];
[self.navigationController pushViewController:corpPrevFront animated:NO];
[corpPrevFront release];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//FOR VIEW
if (buttonIndex == 1 && [alertView tag] == 1) {
// IT WILL BE MATCH ON CorpPrevFront CLASS
}
//FOR DELETE
if(buttonIndex == 2 && [alertView tag] == 1){
} else if(buttonIndex == 1 && [alertView tag] == 2){
DeleteProfiler *delProfiler = [[DeleteProfiler alloc]init];
BOOL del = [delProfiler deleteProfiler:selectedCpUid];
//[delProfiler deleteProfiler:selectedCpUcid];
if (del == YES) {
[repArray removeObjectAtIndex:selectedIndexPath.section];
[businessArray removeObjectAtIndex:selectedIndexPath.row];
[titleArray removeObjectAtIndex:selectedIndexPath.row];
[ucidArray removeObjectAtIndex:selectedIndexPath.row];
[self.corporateTable reloadData];
}
}
}
I am trying to delete an array element from a method. If I execute the below line then the app crashes automatically in my iPod Touch. But it works fine in simulator without any errors.
[repArray removeObjectAtIndex:selectedIndexPath.row];
or
[repArray removeObjectAtIndex:0];
both crashes the app even if I have more than one elements in array.

When you app crashes like that, 99% of the time you are trying to remove an object beyond the bounds of array.

Related

Uncheck (unfavorite) items in Favorites Table View

I have a tableview that displays a list of "favorited" items. Users favorite items in another tableview, and favorited items are listed in this tableview (FavoritesTableView.m).
For some reason, I can't get checked items (favorited items) to "uncheck" from the FavoritesTableView? What am I missing?
See the .m file below...
FavoritesViewController.h
#import <UIKit/UIKit.h>
#import "StrainTableCell.h"
#interface FavoritesViewController : UITableViewController
{
}
#property (strong, nonatomic) NSArray *favoritesArrayset;
#property (strong, nonatomic) IBOutlet UITableView *favoritesTable;
#property (nonatomic, strong) NSMutableArray * favoritesArray;
- (IBAction)backbuttonpressed: (UIBarButtonItem *)sender;
#end
FavoritesViewController.m
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
if (favoritesArray == Nil)
{
favoritesArray = [[NSMutableArray alloc] init];
}
else
{
[favoritesArray removeAllObjects];
}
NSData *dataSave = [[NSUserDefaults standardUserDefaults] objectForKey:#"strains"];
if (dataSave != Nil)
{
favoritesArrayset = [NSKeyedUnarchiver unarchiveObjectWithData:dataSave];
for (NSDictionary *item in favoritesArrayset)
{
BOOL isChecked = [[item objectForKey:#"checked"] boolValue];
if (isChecked == YES )
{
[favoritesArray addObject:item];
}
}
}
[favoritesTable reloadData];
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return favoritesArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *Strains = [favoritesArray copy];
NSArray *dataArray = [favoritesArray copy];
static NSString *strainTableIdentifier = #"StrainTableCell";
StrainTableCell *cell = (StrainTableCell *)[tableView dequeueReusableCellWithIdentifier:strainTableIdentifier];
if (cell == nil)
{
cell = [[StrainTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strainTableIdentifier] ;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"StrainTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.titleLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:#"Title"];
cell.descriptionLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:#"Description"];
cell.ratingLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:#"Rating"];
cell.ailmentLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:#"Ailment"];
cell.actionLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:#"Action"];
cell.ingestLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:#"Ingestion"];
cell.whatCellamI = [NSNumber numberWithInt:indexPath.row];
NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];
cell.textLabel.text = [item objectForKey:#"text"];
[item setObject:cell forKey:#"StrainTableCell"];
}
BOOL checked = [[item objectForKey:#"checked"] boolValue];
UIImage *image = (checked) ? [UIImage imageNamed:#"checked.png"] : [UIImage imageNamed:#"unchecked.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self action:#selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
StrainDetailViewController *detailViewController = [[StrainDetailViewController alloc]
initWithNibName:#"StrainDetailViewController" bundle:nil];
detailViewController.title = [[favoritesArray objectAtIndex:indexPath.row] objectForKey:#"Title"];
detailViewController.strainDetail = [favoritesArray objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 82;
}
- (IBAction)backbuttonpressed:(id)sender
{
[self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
}
- (void)checkButtonTapped:(id)sender event:(id)event
{
NSLog(#"made it here and event is %#",event);
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.favoritesTable];
NSIndexPath * indexPath ;
indexPath = [self.favoritesTable indexPathForRowAtPoint: currentTouchPosition];
NSLog(#"indexpath is below");
NSLog(#"%#",indexPath);
if (indexPath != Nil)
{
NSMutableDictionary *item = [favoritesArray objectAtIndex:indexPath.row];
BOOL isItChecked = [[item objectForKey:#"checked"] boolValue];
/*
if (isItChecked == NO) {
NSMutableArray *tmpArray = [[NSMutableArray alloc] init];
NSMutableArray *tmpArray2 = [[NSMutableArray alloc] initWithArray:[favoritesArray allObjects]];
NSString *text1 = [item objectForKey:#"Title"];
for (NSDictionary * object in tmpArray2) {
NSString *text2 = [object objectForKey:#"Title"];
if (![text1 isEqualToString:text2]) {
[tmpArray addObject:object];
}
}
// [favoritesArray removeAllObjects];
favoritesArray = [tmpArray copy];
}
*/
NSMutableArray *quickArray = [[NSMutableArray alloc] initWithArray:favoritesArray];
[quickArray replaceObjectAtIndex:indexPath.row withObject:item];
[item setObject:[NSNumber numberWithBool:!isItChecked] forKey:#"checked"];
favoritesArray = [quickArray copy];
// [self.favoritesArray addObject:item];
// NSLog(#"you have added %d items to favorites", self.favoritesArray.count);
[favoritesTable reloadData];
}
#end
In your .h take one NSMutableDictionary and do property to it.
In your .m synthesize it,and in viewdidLoad alloc the Dictionary.
Now put this below code in CellForRowAtIndex
if([idDictonary objectForKey:[NSString stringWithFormat:#"%d",[indexPath row]]])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
and put this below code in DidSelectRowAtIndex
UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
if (thisCell.accessoryType == UITableViewCellAccessoryNone)
{
thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
[idDictonary setValue:[dataArray objectAtIndex:indexPath.row] forKey:[NSString stringWithFormat:#"%d",[indexPath row]]];
}
else
{
if([idDictonary objectForKey:[NSString stringWithFormat:#"%d",[indexPath row]]])
{
[idDictonary removeObjectForKey:[NSString stringWithFormat:#"%d",[indexPath row]]];
thisCell.accessoryType = UITableViewCellAccessoryNone;
}
}
[myTableView reloadData];
NSLog(#"idDictonary = %#",idDictonary);
i hope this will helps u Brittany...
Every time -tableView:cellForRowAtIndexPath: runs, you create a new button, set it to the desired properties and … throw it away. (Seems to be the running joke this week.) You have to use the existing button.

UITableViewController application crashes

The UITableViewController freezes when scrolling or when viewDidAppear:. I see that when the problem starts, the app start to allocate objects uncontrollably.
I have the following project for iOS: basically download information from CoreData, but when I go to display the data in the table, the application freezes (both the device and the simulator). But if I show the arrangement in a downloaded NSLog no errors. The code is as follows:
I see that when the problem starts, the app start to allocate objects uncontrollably.
The UITableViewController freezes when scrolling or when viewDidAppear:
TableViewController.h
#interface Set_ScheduleViewController : UITableViewController
{
STCore *core;
UILabel *label;
NSArray *objects;
}
#end
TableViewController.m:
#implementation Set_ScheduleViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
core = [[STCore alloc] init];
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)reloadData
{
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:YES];
objects = [core getAllClassesUsingSortDescriptions:#[sort]];
[[self tableView] reloadData];
}
- (void)viewDidAppear:(BOOL)animated
{
[self reloadData];
[super viewDidAppear:animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [objects count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 20)];
label.textColor = [UIColor colorWithHex:0x88B6DB];
label.font = [UIFont systemFontOfSize:12.0f];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentRight;
label.minimumFontSize = 12.0f;
tableView.separatorColor = [UIColor colorWithHex:0xDAE1E6];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor colorWithHex:0x393B40];
cell.textLabel.font = [UIFont boldSystemFontOfSize:16.0f];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.textColor = [UIColor colorWithHex:0x393B40];
cell.detailTextLabel.font = [UIFont systemFontOfSize:12.0f];
}
cell.textLabel.text = [[objects objectAtIndex:indexPath.row] name];
cell.detailTextLabel.text = [[objects objectAtIndex:indexPath.row] location];
STMultipleDate *dates = [STMultipleDate new];
for (Schedule *sch in [[objects objectAtIndex:indexPath.row] schedule]) {
STDate *date = [STDate new];
[date setWeekday:[[sch day] integerValue]];
[date setHour:[[sch hour] integerValue]];
[date setMinutes:[[sch minutes] integerValue]];
[dates addDate:date];
}
label.text = [STCore splitDates:dates];
cell.accessoryView = label;
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
if ([objects count] < 1) {
return NSLocalizedString(#"SETSCH_FOOTER_DEFAULT_404", #"");
}
return nil;
}
STCore.m
- (NSArray *)getAllClassesUsingSortDescriptions:(NSArray *)descs
{
if (![self isReady]) {
return #[];
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Classes" inManagedObjectContext:_context];
[fetchRequest setEntity:entity];
[fetchRequest setSortDescriptors:descs];
NSError *error;
NSArray *returned = [_context executeFetchRequest:fetchRequest error:&error];
if (error) {
[_delegate core:self didRecivedAnError:error];
}
return returned;
}
In your viewDidLoad you're locking the context with [[core context] lock];, which is causing your getAllClassesUsingSortDescriptions to wait for the lock to clear. Remove the lock.

cell specification in uitableview [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
UITableView create 4 square
I want to have 4 cell in one row in uiTableview but nothing appear, I don't know why would you please help me
this a the picture for cell
Thanks in advance
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *keys = [[NSMutableArray alloc] init];
NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];
NSString *monKey = #"Monday";
NSString *tueKey = #"Tuesday";
NSString *wedKey = #"Wednday";
NSString *thuKey = #"Thusday";
NSString *friKey = #"Friday";
NSString *satKey = #"Satuarday";
NSString *sunKey = #"Sunnday";
[contents setObject:[NSArray arrayWithObjects:#"Work Time", #"Absence", nil] forKey:monKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", #"Absence", nil] forKey:wedKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:tueKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:thuKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:friKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:satKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:sunKey];
[keys addObject:tueKey];
[keys addObject:monKey];
[keys addObject:wedKey];
[keys addObject:thuKey];
[keys addObject:friKey];
[keys addObject:satKey];
[keys addObject:sunKey];
[self setSectionKeys:keys];
[self setSectionContents:contents];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:#selector(addNewItem)];
self.navigationItem.rightBarButtonItem = rightButton;
}
cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
int column = 4;
for (int i=0; i<column; i++) {
UIImageView *aBackgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(32+184*i,10, 167,215)];
aBackgroundImageView.tag = (column*indexPath.row)+i;
[cell.contentView addSubview:aBackgroundImageView];
// [aBackgroundImageView release];
}
return cell;
}
Edit 1
this is the picture when I added this line
aBackgroundImageView.backgroundColor = [UIColor redColor];
Edit 2 :
with Rog method
It seems to me that in:
for (int i=0; i<column; i++) {
UIImageView *aBackgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(32+184*i,10, 167,215)];
aBackgroundImageView.tag = (column*indexPath.row)+i;
[cell.contentView addSubview:aBackgroundImageView];
// [aBackgroundImageView release];
}
you are not providing a content for your views...
so that should explain why you don't see anything... Try adding:
aBackgroundImageView.backgroundColor = [UIColor redColor];
and see if anything changes.
Here i create the one demo application like you want and that some of the code put into here.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSUInteger devide=[self.tableArray count] / 3;
if ([self.tableArray count] % 3 >0 ) {
devide+=1;
} else {
devide=devide;
}
return devide;
}
And cellForRowAtIndexPath.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil){
cell=[[[NSBundle mainBundle] loadNibNamed:#"CCell" owner:self options:nil] objectAtIndex:0];
UIButton *imgV1=(UIButton*)[cell viewWithTag:1];
UIButton *imgV2=(UIButton*)[cell viewWithTag:2];
UIButton *imgV3=(UIButton*)[cell viewWithTag:3];
[imgV1 addTarget:self action:#selector(btnTapped:) forControlEvents:UIControlEventTouchDown];
[imgV2 addTarget:self action:#selector(btnTapped:) forControlEvents:UIControlEventTouchDown];
[imgV3 addTarget:self action:#selector(btnTapped:) forControlEvents:UIControlEventTouchDown];
HJManagedImageV *img1=[[HJManagedImageV alloc] initWithFrame:CGRectMake(0, 0, imgV1.frame.size.width, imgV1.frame.size.height)];
HJManagedImageV *img2=[[HJManagedImageV alloc] initWithFrame:CGRectMake(0, 0, imgV2.frame.size.width, imgV2.frame.size.height)];
HJManagedImageV *img3=[[HJManagedImageV alloc] initWithFrame:CGRectMake(0, 0, imgV3.frame.size.width, imgV3.frame.size.height)];
img1.tag=img2.tag=img3.tag=10000;
img1.backgroundColor=img2.backgroundColor=img3.backgroundColor=[UIColor clearColor];
[imgV1 addSubview:img1];
[imgV2 addSubview:img2];
[imgV3 addSubview:img3];
}
int x=indexPath.row;
x=x*3;
UIButton *imgV1=(UIButton*)[cell viewWithTag:1];
UIButton *imgV2=(UIButton*)[cell viewWithTag:2];
UIButton *imgV3=(UIButton*)[cell viewWithTag:3];
HJManagedImageV *img1=(HJManagedImageV*)[imgV1 viewWithTag:10000];
HJManagedImageV *img2=(HJManagedImageV*)[imgV2 viewWithTag:10000];
HJManagedImageV *img3=(HJManagedImageV*)[imgV3 viewWithTag:10000];
[img1 clear];
[img2 clear];
[img3 clear];
[imgV1 setTitle:[self.tableArray objectAtIndex:x] forState:UIControlStateDisabled];
img1.url=[NSURL URLWithString:[self.tableArray objectAtIndex:x]];
[img1 showLoadingWheel];
[GlobalCacheManager manage:img1];
if((x+1) >= self.tableArray.count){
imgV2.hidden=imgV3.hidden=YES;
[imgV2 setBackgroundImage:nil forState:UIControlStateNormal];
[imgV3 setBackgroundImage:nil forState:UIControlStateNormal];
} else if( (x+2)>=self.tableArray.count) {
img2.url=[NSURL URLWithString:[self.tableArray objectAtIndex:x+1]];
[img2 showLoadingWheel];
[GlobalCacheManager manage:img2];
imgV1.hidden=imgV2.hidden=YES;
imgV3.hidden=YES;
[imgV2 setTitle:[self.tableArray objectAtIndex:x+1] forState:UIControlStateDisabled];
[imgV3 setBackgroundImage:nil forState:UIControlStateNormal];
} else {
[img2 showLoadingWheel];
[img3 showLoadingWheel];
img2.url=[NSURL URLWithString:[self.tableArray objectAtIndex:x+1]];
[GlobalCacheManager manage:img2];
img3.url=[NSURL URLWithString:[self.tableArray objectAtIndex:x+2]];
[GlobalCacheManager manage:img3];
imgV2.hidden=imgV3.hidden=NO;
[imgV2 setTitle:[self.tableArray objectAtIndex:x+1] forState:UIControlStateDisabled];
[imgV3 setTitle:[self.tableArray objectAtIndex:x+2] forState:UIControlStateDisabled];
}
return cell;
}
Your method is a bit of a mess. Your main problem is how you are setting the frame origin for the UIImageView instances as they are 1) too big for the cell 2) out of the cell bounds.
Try this (ps: there may be typos as I've written it off the top of my head):
- (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]
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
int numberOfColumns = 4;
CGSize gridSize = CGSizeMake(cell.contentView.frame.size.width / numberOfColumns, cell.contentView.frame.size.height / 2); // Height size is arbitrary here, width is obtained by dividing total width by number of columns
for (int i=0; i < column; i++)
{
UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(gridSize.width * i, cell.contentView.frame.size.height/2 - gridSize.height/2, gridSize.width, gridSize.height)];
aview.backgroundColor = [UIColor colorWithWhite:1.0 alpha:1/i+1]; // This is just so you can see the different gridCells within the cell
[cell.contentView addSubview:aView];
[aView release];
}
return cell;
}

When scrolling a tableview back again cells appear blank?

After loading a tableview with custom cells all looks fine and scrolling down is fine.
When I scroll up the cells above appear blank. They are in fact fully functional as they can still be selected and then they go off to the details pages.
The code cellForRowAtIndexPath is executed for the returning row and returns the cell as I would expect with the right details. It just isn't displayed.
Any thoughts/help would be appreciated.
The code below is what I have been using.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomCellIdentifier = #"CustomCellIdentifier";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
for (id oneObject in nib) {
if ([oneObject isKindOfClass:[CustomCell class]]) {
cell = (CustomCell *)oneObject;
}
}
}
// Configure the cell.
Book *aBook = [appDelegate.sortedArray objectAtIndex:indexPath.row];
//name
NSString *trimmedString = [aBook.Name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
cell.nameLabel.text = trimmedString;
//catagory
NSString *trimmedExperience = [aBook.PrimaryExperience stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([trimmedExperience isEqualToString:#"1"]) {
cell.catagoryLabel.text = #"None";
}
else if([trimmedExperience isEqualToString:#"2"]){
cell.catagoryLabel.text = #"Limited";
}
else if ([trimmedExperience isEqualToString:#"4"]) {
cell.catagoryLabel.text = #"Full";
}
else {
cell.catagoryLabel.text = #"";
}
cell.distanceLabel.text = [NSString stringWithFormat:#"%1.1f",aBook.distance];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
The problem lies with the xib file and how it loaded. To cut out the issue and gain total control the following code was substituted for the IB version of the custom cell.
static NSString *CellTableIdentifier = #"CellTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellTableIdentifier] autorelease];
CGRect nameLabelRect = CGRectMake(15, 5, 200, 15);
UILabel *nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect];
nameLabel.textAlignment = UITextAlignmentLeft;
nameLabel.font = [UIFont boldSystemFontOfSize:14];
nameLabel.tag = kNameTag;
[cell.contentView addSubview:nameLabel];
[nameLabel release];
CGRect catagoryLabelRect = CGRectMake(15, 26, 100, 15);
UILabel *catagoryLabel = [[UILabel alloc]initWithFrame:catagoryLabelRect];
catagoryLabel.textAlignment = UITextAlignmentLeft;
catagoryLabel.font = [UIFont systemFontOfSize:12];
catagoryLabel.tag = kExperienceTag;
[cell.contentView addSubview:catagoryLabel];
[catagoryLabel release];
CGRect distanceLabelRect = CGRectMake(210, 26, 70, 15);
UILabel *distanceLabel = [[UILabel alloc] initWithFrame:distanceLabelRect];
distanceLabel.textAlignment = UITextAlignmentRight;
distanceLabel.font = [UIFont boldSystemFontOfSize:12];
distanceLabel.tag = kDistanceTag;
[cell.contentView addSubview:distanceLabel];
[distanceLabel release];
}
Thanks for helping think this one through. Now the scrolling works perfectly.

message sent to deallocated instance (probably UITableViewCell)

I know that a lot of folks have had this problem before and I've read a lot of suggested solutions, none of which has worked out for me. I'm working on a part of a larger project, involving three different views: the first view contains a UITableView with custom made cells called WeekdaySelectionWithPopoverCellController. This cell contains a label designating the weekday (Monday through Friday), a UISwitch and the following elements that are displayed or hidden respectively based on the state of the switch. If the switch is on, the cell looks like this:
----------------------------------------------------------------------
| Monday [ON/off] between ( N/A ) and ( N/A ) |
----------------------------------------------------------------------
If it's off it looks like this:
----------------------------------------------------------------------
| Monday [on/OFF] |
----------------------------------------------------------------------
The (N/A) elements are UIButtons. If the user hits one of those buttons a UIPopover with a date picker opens. The idea is that the user selects a time from the picker that should be displayed on the UIButtons instead of the (N/A):
----------------------------------------------------------------------
| Monday [ON/off] between ( 12:30 ) and ( 16:00 ) |
----------------------------------------------------------------------
The hiding of the elements on hitting the UISwitch works as does the display of the popover. However, if the user hits the "Done" button in the popover and the popover is dismissed the app crashes with the following error:
-[UITableViewCell isKindOfClass:]: message sent to deallocated instance 0xf42a100
Strangely enough the only place containing code that calls isKindOfClass is ModalTableViewController (the parent view of the popover and the custom cells) and the address of the instance displayed does not belong to it. Neither is it the address of the popover which only leaves the class of the custom cells (WeekdaySelectionWithPopoverCellController).
Let me give you the relevant parts of the ModalTableViewController.m:
#implementation ModalTableViewController
#synthesize mtvcNavigationBar;
#synthesize mtvcTableView;
#synthesize cell;
#synthesize lable;
#synthesize button;
// WeekdaySelectionWithPopoverCell
#synthesize wswpMainLabel;
#synthesize wswpFromLabel;
#synthesize wswpToLabel;
#synthesize wswpOClockFromLabel;
#synthesize wswpOClockToLabel;
#synthesize wswpSwitch;
#synthesize wswpFromValueButton;
#synthesize wswpToValueButton;
#synthesize popoverController;
// more code...
- (ModalTableViewController *)initWithParam:(NSString *)title andData:(NSArray *)myData andSelection:(NSArray *)selection {
[self initSwipeLeftNavigation];
mtvcNavigationBar.topItem.title = title;
mtvcSectionSize = [myData count];
mtvcSectionname = #"";
mtvcLabels = myData;
mtvcData = selection;
[mtvcLabels retain];
[mtvcData retain];
[otherValues retain];
mtvSingleton = [ModalTableViewSingleton sharedInstance];
return self;
}
// more code...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cellRet;
NSString *MyIdentifier = #"WeekdaySelectionWithPopoverCellController";
cellRet = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cellRet == nil) {
[[NSBundle mainBundle] loadNibNamed:MyIdentifier owner:self options:nil];
cellRet = self.cell;
}
NSDictionary *tempValues;
tempValues = [mtvcData objectAtIndex:indexPath.row];
wswpSwitch = (UISwitch *)[cellRet viewWithTag:2];
wswpSwitch.on = [[tempValues valueForKey:#"company_id"] isKindOfClass:[NSNull class]] ? false: true;
BOOL negatedState = !wswpSwitch.on;
NSLog(#"tempValues: %#", tempValues);
lable = (UILabel *)[cellRet viewWithTag:1];
lable.text = [NSString stringWithFormat:#"%#", [tempValues valueForKey:#"short"]];
lable.font = [UIFont boldSystemFontOfSize:16];
lable = (UILabel *)[cellRet viewWithTag:3];
lable.font = [UIFont boldSystemFontOfSize:16];
lable.text = [NSString stringWithFormat:#"%#", #"von"];
lable.hidden = negatedState;
lable = (UILabel *)[cellRet viewWithTag:5];
lable.text = [NSString stringWithFormat:#"%#", #"bis"];
lable.font = [UIFont boldSystemFontOfSize:16];
lable.hidden = negatedState;
lable = (UILabel *)[cellRet viewWithTag:7];
lable.text = #"Uhr";
lable.font = [UIFont boldSystemFontOfSize:16];
lable.hidden = negatedState;
lable = (UILabel *)[cellRet viewWithTag:8];
lable.text = #"Uhr";
lable.font = [UIFont boldSystemFontOfSize:16];
lable.hidden = negatedState;
button = (UIButton *)[cellRet viewWithTag:4];
[self setButtonLabel:button forKey:#"beginning" fromDict:tempValues];
button.hidden = negatedState;
/*
if (![[tempValues valueForKey:#"beginning"] isKindOfClass:[NSNull class]]) {
button.titleLabel.text = [NSString stringWithFormat:#"%#", [tempValues valueForKey:#"beginning"]];
} else {
button.titleLabel.text = #"k.A.";
}
*/
button = (UIButton *)[cellRet viewWithTag:6];
[self setButtonLabel:button forKey:#"until" fromDict:tempValues];
button.hidden = negatedState;
/*
if (![[tempValues valueForKey:#"until"] isKindOfClass:[NSNull class]]) {
button.titleLabel.text = [NSString stringWithFormat:#"%#", [tempValues valueForKey:#"until"]];
} else {
button.titleLabel.text = #"k.A.";
}
*/
self.cell = nil;
return cellRet;
}
- (void)setButtonLabel:(UIButton *)btn forKey:(NSString *)key fromDict:(NSDictionary *)dict {
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
NSString *text = [dict objectForKey:key];
if ([mtvSingleton.sharedData objectForKey:key]) {
[btn setTitle:[mtvSingleton.sharedData objectForKey:key] forState:UIControlStateNormal];
} else {
if ([text isKindOfClass:[NSNull class]] ||
[text isEqualToString:#""])
{
[btn setTitle:#"k.A." forState:UIControlStateNormal];
} else {
[btn setTitle:text forState:UIControlStateNormal];
}
}
}
// more code...
- (IBAction)switchButtonValueChanged:(id)sender
{
//[self changeDisplayState:wsdSwitch.on forObj:sender];
// UISwitch *tmp = (UISwitch *)sender;
UIView *v = (UIView *)sender;
do {
v = v.superview;
} while (![v isKindOfClass:[UITableViewCell class]]);
// WeekdaySelectionWithPopoverCellController *cell1 = (WeekdaySelectionWithPopoverCellController *)v;
cell = (UITableViewCell *)v;
wswpSwitch = (UISwitch *)[cell viewWithTag:2];
BOOL negatedState = !wswpSwitch.on;
lable = (UILabel *)[cell viewWithTag:3];
lable.hidden = negatedState;
lable = (UILabel *)[cell viewWithTag:5];
lable.hidden = negatedState;
lable = (UILabel *)[cell viewWithTag:7];
lable.hidden = negatedState;
lable = (UILabel *)[cell viewWithTag:8];
lable.hidden = negatedState;
button = (UIButton *)[cell viewWithTag:4];
button.hidden = negatedState;
button = (UIButton *)[cell viewWithTag:6];
button.hidden = negatedState;
/*
wswpSwitch = (UISwitch *)[cell1 viewWithTag:2];
BOOL negatedState = !wswpSwitch.on;
lable = (UILabel *)[cell1 viewWithTag:3];
lable.hidden = negatedState;
lable = (UILabel *)[cell1 viewWithTag:5];
lable.hidden = negatedState;
lable = (UILabel *)[cell1 viewWithTag:7];
lable.hidden = negatedState;
lable = (UILabel *)[cell1 viewWithTag:8];
lable.hidden = negatedState;
button = (UIButton *)[cell1 viewWithTag:4];
button.hidden = negatedState;
button = (UIButton *)[cell1 viewWithTag:6];
button.hidden = negatedState;
*/
}
#pragma mark - handlers
- (IBAction)toButtonHandler:(id)sender {
dppvc = [[DatePickerPopoverViewController alloc] initWithCaller:self andKey:#"beginning"];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:dppvc];
popover.delegate = self;
self.popoverController = popover;
[popover release];
[self.popoverController presentPopoverFromRect:[(UIButton *)sender frame]
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
- (IBAction)fromButtonHandler:(id)sender {
dppvc = [[DatePickerPopoverViewController alloc] initWithCaller:self andKey:#"until"];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:dppvc];
popover.delegate = self;
self.popoverController = popover;
[popover release];
CGRect senderFrameRect = [(UIButton *)sender frame];
[self.popoverController presentPopoverFromRect:senderFrameRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
#pragma mark - UIPopoverControllerDelegate
//---called when the user clicks outside the popover view---
- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController {
// NSLog(#"popover about to be dismissed");
return YES;
}
//---called when the popover view is dismissed---
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
// NSLog(#"popover dismissed");
}
- (void)dismissPopover {
if (popoverController != nil) {
[popoverController dismissPopoverAnimated:YES];
NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
int i;
for (i = 0; i < [mtvcLabels count]; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
[mtvcTableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:YES];
}
}
- (void)dealloc {
[mtvcSectionname release];
[mtvcLabels release];
[mtvcData release];
[cell release];
[super dealloc];
}
The crash occurs in main.m:
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// crashes:
int retVal = UIApplicationMain(argc, argv, nil, nil); // <--
[pool release];
return retVal;
}
I have enabled NSZombieEnabled, MallocStackLoggingNoComp and NSAutoreleaseFreedObjectCheckEnabled but the compiler doesn't give me any more information than what I posted.
I've spent the entire day trying to resolve this but to no avail. Any hint in the right direction is appreciated!
I've been able to take care of this particular error. The problem was that cellRet in cellForRowAtIndexPath had not been retained. The following snippet resolves the error mentioned above:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cellRet;
NSString *MyIdentifier = #"WeekdaySelectionWithPopoverCellController";
cellRet = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cellRet == nil) {
[[NSBundle mainBundle] loadNibNamed:MyIdentifier owner:self options:nil];
cellRet = self.cell;
[cellRet retain];
}
// ...
}
Now however, I'm stuck with a new, similar error. This time it says:
*** -[CALayer release]: message sent to deallocated instance 0xc656d30
The following topic has some more information on the problem: "[CALayer release]: message sent to deallocated instance" when dismissing modal view controller
I have tried to change the property of the popover view controller to assign instead of retain but that gave me the message sent to deallocated instance error again, this time for the popover view controller.
Somebody with an idea? TIA!
The way I usually handle this is to create a subclass of UITableViewCell for my custom cells. and use the nib. I create a custom init that loads the cell from the nib. as follows.
- (id)init
{
self = [[[[NSBundle mainBundle] loadNibNamed:#"NibName" owner:self options:nil] objectAtIndex:0] retain];
if (self)
{
// any further initialization
}
return self;
}
in the nib set the cell's class to your new class.
then to use your cell do the following.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[[CustomCell alloc] init] autorelease];
}
// setup your cell
return cell;
}
You should definitely be using assign on your delegate properties.