i was implementing the table view programmatically, where i set property of mSelectedSubUnitIndex of (NSIndexPath) type as non atomic and retain and synthesized in .m . When i load my tableviewcontroller then method:
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath
:(NSIndexPath *)indexPath
{
CGFloat height = 0.0;
SubUnit *subUnit = (SubUnit*)[mSubUnitsArray objectAtIndex:indexPath.row];
NSArray *subUnitExercises = [self sortArray:[subUnit.subUnitExercise allObjects]];
NSLog(@"mSelectedSubUnitIndex.row........%d",mSelectedSubUnitIndex.row);
NSLog(@"subUnitExercises........%d",[subUnitExercises count]);
if (indexPath.row == mSelectedSubUnitIndex.row && [subUnitExercises count]>1) {
height =CELL_EXPAND_HEIGHT ;
}
else {
height = CELL_NORMAL_HEIGHT;
}
return height;
}
run quite fine. When i return back to my tableviewcontoller from other controller then it crashes(object message send) at same method on line number 5 at NSLog, and give exception at method at [self.tableView reloadData];. It is resolved by commenting[self.tableView reloadData];.
-(void)viewDidAppear:(BOOL)animated
{
DebugLog(@"start");
//[self.tableview reloadData];
execountarray=[[NSMutableArray alloc]init];
for(int k=0;k<[mSubUnitsArray count];k++)
{
SubUnit *subUnit = (SubUnit*)[mSubUnitsArray objectAtIndex:k];
NSArray *subUnitExercises = [subUnit.subUnitExercise allObjects];
[execountarray addObject:[NSString stringWithFormat:@"%d",[subUnitExercises
count]]];
}
///////////////
if (managedObjectContext){
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"TSubUnitExerciseProgress" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
// Order the events by creation date, most recent first.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"editDate" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor,
nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];
// Execute the fetch -- create a mutable copy of the result.
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext
executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
// Handle the error.
myNotes = nil;
[myNotes removeAllObjects];
}
else
{
[myNotes setArray: mutableFetchResults];
}
//NSLog(@"My notes count:--------unitviewcontroller--------------->%d",
[myNotes count]);
if([myNotes count] ==0)
{
setExer1Done:NO;
setExer2Done:NO;
}
else
{
NSLog(@"hey :P");
}
}
// [self.tableview reloadData];
}
didSelectRowAtIndex
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath
{
DebugLog(@" -start- \n");
mSelectedSubUnitIndex = indexPath;
SubUnit *subUnit = (SubUnit*)[mSubUnitsArray objectAtIndex:indexPath.row];
NSArray *subUnitExercises = [self sortArray:[subUnit.subUnitExercise allObjects]];
if([subUnitExercises count]!=1)
{
NSArray* paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:
[mSelectedSubUnitIndex row] inSection:0]];
[tableview beginUpdates];
[tableview deleteRowsAtIndexPaths:paths
withRowAnimation:UITableViewRowAnimationFade];
[tableview insertRowsAtIndexPaths:paths
withRowAnimation:UITableViewRowAnimationFade];
[tableview endUpdates];
}
else
{
//SubUnit *subUnit = (SubUnit*)[mSubUnitsArray objectAtIndex:
[mSelectedSubUnitIndex row]];
SubUnit *subUnit = (SubUnit*)[mSubUnitsArray objectAtIndex:indexPath.row];
NSArray *subUnitExercises = [self sortArray:[subUnit.subUnitExercise
allObjects]];
if ([subUnitExercises count] > 0) {
SubUnitExercise *subUnitExercise = [subUnitExercises
objectAtIndex:0];
[self loadSubUnitExercise:subUnitExercise];
}
}
}
this is running on iOS 4 but when i build through Xcode version 4.2 and iOS 5 (released oct-12, 2011) then it crashes. Can’t figure out the problem. help me out!!
cellForRowAtIndex method
now i am getting exception at if-statement at mSelectedsubunitindex.row when i scroll, but i resolved only checking that if (indexPath.row){}, and also row remain selected when i return bad to my tableview controller
- (void)tableView:(UITableView *)tableView willDisplayCell:
(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
DebugLog(@"-start- \n");
SubUnit *subUnit = (SubUnit*)[mSubUnitsArray objectAtIndex:indexPath.row];
NSArray *subUnitExercises = [self sortArray:[subUnit.subUnitExercise allObjects]];
NSString *str1 = [[subUnit performSelector:@selector(title)]copy];
if ([str1 isEqualToString:@"1. was/were"])
{
global = str1;
}
//if(indexPath.row==mSelectedSubUnitIndex.row)
if(indexPath.row)
{
if ([subUnitExercises count] != 1)
{
SubUnit *subUnit = (SubUnit*)[mSubUnitsArray objectAtIndex:
[mSelectedSubUnitIndex row]];
NSArray *subUnitExercises = [self sortArray:
[subUnit.subUnitExercise allObjects]];
SubUnitCell *subUnitcell = (SubUnitCell*) cell;
mCellSubTopicLabel.text = subUnit.title;
if([myNotes count] == 0)
{
NSNumber *isDone = [[subUnitExercises objectAtIndex:1] isDone];
[subUnitcell setExer2Done:NO];
mExer2Checkbox.image = [UIImage imageNamed:[isDone boolValue]?
kExerciseCheckmark :kExerciseWrongmark];
isDone = [[subUnitExercises objectAtIndex:0] isDone];
[subUnitcell setExer1Done:NO];
mExer1Checkbox.image = [UIImage imageNamed:[isDone boolValue]?
kExerciseCheckmark :kExerciseWrongmark];
}
else
{
NSNumber *isDone = [[subUnitExercises objectAtIndex:1] isDone];
[subUnitcell setExer2Done:[isDone boolValue]];
mExer2Checkbox.image = [UIImage imageNamed:[isDone boolValue]?
kExerciseCheckmark :kExerciseWrongmark];
isDone = [[subUnitExercises objectAtIndex:0] isDone];
[subUnitcell setExer1Done:[isDone boolValue]];
mExer1Checkbox.image = [UIImage imageNamed:[isDone boolValue]?
kExerciseCheckmark :kExerciseWrongmark];
}
[subUnitcell.contentView addSubview:mCellSubTopicContentView];
}
}
For some reason NSIndexPath assignment (
=) and equality(==) is not working in IOS5. I have solved the problem usingselfbefore anyNSIndexPathobject e.g.There is another way solving this assignment using copy like this:
Equality works in same way. Like: