1

我正在以编程方式实现表视图,其中我将 (NSIndexPath) 类型的 mSelectedSubUnitIndex 属性设置为非原子并在 .m 中保留和合成。当我加载我的 tableviewcontroller 然后方法:

(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;   
} 

运行得很好。当我从其他控制器返回到我的 tableviewcontoller 时,它在 NSLog 的第 5 行的相同方法处崩溃(对象消息发送),并在[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];
    }
}
  }

这是在 iOS 4 上运行的,但是当我通过 Xcode 4.2 版和 iOS 5(2011 年 10 月 12 日发布)构建时,它会崩溃。无法弄清楚问题所在。帮帮我!!

cellForRowAtIndex 方法

现在,当我滚动时,我在 mSelectedsubunitindex.row 的 if 语句中遇到异常,但我解决了只检查 if (indexPath.row){},并且当我将错误返回到我的 tableview 控制器时,行仍然保持选中状态

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

}

}
4

2 回答 2

1

由于某种原因 NSIndexPath 赋值 ( =) 和相等 ( ==) 在 IOS5 中不起作用。我已经解决了self在任何NSIndexPath对象之前使用的问题,例如

self.mSelectedSubUnitIndex

还有另一种使用副本解决此分配的方法,如下所示:

anIndexPath = (NSIndexPath*) [anotherIndexPath  copy];

平等以同样的方式运作。像:

if([self.mSelectedSubUnitIndex isEqual:anotherIndexPath])
{

}
于 2011-10-31T08:35:20.890 回答
0

刚刚快速浏览过您的代码。我突然想到两件事:

tableView:cellForRowAtIndexPath:1:您通话的最后一行[self.tableview reloadData]

这是不必要的,因为返回的单元格将在您刚刚配置时显示。这似乎也会导致绘图循环(“reloadData->cellForRowAtIndexPath->reloadData->cellForRowAtIndexPath-> 等..)。尝试删除此行,看看这是否能解决您的问题。

2:您似乎没有重复使用单元格,尽管您的评论之一似乎暗示您认为自己是。我希望方法的开头类似于下面的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        /* Load a custom cell from a NIB
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
        cell = (UITableViewCell *)[nib objectAtIndex:0];
        */
        // Assume SubUnitCell exists somewhere
        SubUnitCell *cell = [[[SubUnitCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Retrieve config data and configure the cell as required in the code below - real config code needs to be added
    NSNumber *isDone = [NSNumber numberWithBool:NO];
    SubUnit *subUnit = (SubUnit*)[mSubUnitsArray objectAtIndex:[indexPath row]];

    [cell setTitle:subUnit.title];

    if([myNotes count] ==0)
    {
        [cell setExer1Done:NO];
        [cell setExer2Done:NO];
    }
    else
    {
        NSLog(@"My notes count:--------unitviewcontroller----->%d",[myNotes count]);    
    }

    // All your other configuration code
    .......
    .......
    .......

    return cell; 
}

另外请尝试发布符号化的崩溃日志,以便人们可以看到确切的错误和错误路径。

于 2011-10-18T12:30:46.487 回答