1

UITableViewCell をサブクラス化しました。基本的に何が起こるかというと、セルのレイヤーで UITableViewCell の描画を押すと、セルが異なって表示されるということです。ただし、セルを削除すると、その図面はその下のセルにドロップされます。これは、セルのフォーマットが通常どおり再利用されていることを示しているようです。したがって、以下に示すように、CellForRowAtIndexPath のセルを再描画しました...

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
AGProgressViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
//NSLog(@"progress value = %f", [cell.progress floatValue]);

if (!cell) {
    cell = [[AGProgressViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

Task * task = nil;

if (indexPath.section == 0){
    task = [self.tasksByDay[@"anyday"] objectAtIndex:indexPath.row];
} else if (indexPath.section == 1){
    task = [self.tasksByDay[@"monday"] objectAtIndex:indexPath.row];
} else if (indexPath.section == 2){
    task = [self.tasksByDay[@"tuesday"] objectAtIndex:indexPath.row];
} else if (indexPath.section == 3){
    task = [self.tasksByDay[@"wednesday"] objectAtIndex:indexPath.row];
} else if (indexPath.section == 4){
    task = [self.tasksByDay[@"thursday"] objectAtIndex:indexPath.row];
} else if (indexPath.section == 5){
    task = [self.tasksByDay[@"friday"] objectAtIndex:indexPath.row];
} else if (indexPath.section == 6){
    task = [self.tasksByDay[@"saturday"] objectAtIndex:indexPath.row];
} else if (indexPath.section == 7){
    task = [self.tasksByDay[@"sunday"] objectAtIndex:indexPath.row];
}
cell.progress = [NSNumber numberWithFloat:([task.timeSpent floatValue]/[task.time floatValue])];
// this is calling the redrawing method in the cell
[cell drawFillInAtPercent:[task.timeSpent floatValue]/[task.time floatValue]];

//NSLog(@"progress value = %f vs. time spent = %f", [cell.progress floatValue], [task.timeSpent floatValue]/[task.time floatValue]);

cell.textLabel.text = task.name;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d minutes",
                             [task.time intValue] - [task.timeSpent intValue]];

return cell;
}

ただし、これで問題は解決しませんでした。これらすべての NSLog は、再描画ごとにセルが適切なレベルにあることを示していました。これは、何らかの理由で、削除されるセルが cellForRowAtIndex パスで呼び出されていないことを意味します。奇妙なことは、テキスト ラベルが変更されていることです。UITableViewCell サブクラスで行っているカスタム描画が変更されていないだけです。

これは、サブクラスで呼び出しているメソッドです。

-(void) drawFillInAtPercent: (float) percent{
//if (percent > 0){
    NSLog(@"progress layer at percent %f", percent);
    _progressLayer = [CAGradientLayer layer];
    _progressLayer.frame = CGRectMake(self.bounds.origin.x,
                                      self.bounds.origin.y,
                                      self.bounds.size.width * percent,
                                      self.bounds.size.height);
    _progressLayer.colors = @[(id)[[UIColor colorWithRed:0/255.0 green:0/250.0 blue:250.0/255.0 alpha:1.0f] CGColor],
    (id)[[UIColor colorWithRed:150.0/200.0 green:150.0/200.0 blue:150.0/200.0 alpha:.5] CGColor],
    (id)[[UIColor colorWithRed:200.0/200.0 green:200.0/200.0 blue:200.0/200.0 alpha:.5] CGColor],
    (id)[[UIColor colorWithWhite:0.3f alpha:0.1f] CGColor]];
    _progressLayer.locations = @[@0.00f, @0.2f, @0.90f, @1.00f];
    [self.layer insertSublayer:_progressLayer atIndex:1];
//}
}

何が起こっているのかわからず、再描画するために再利用されたセルにアクセスできないようです。

削除方法は次のとおりです。

   - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

   if (editingStyle == UITableViewCellEditingStyleDelete) {

    [self deactivateTimers];
    NSArray * days = [NSArray arrayWithObjects: @"anyday", @"monday", @"tuesday",       @"wednesday", @"thursday", @"friday", @"saturday", @"sunday", nil];

    Task * task = self.tasksByDay[days[indexPath.section]][indexPath.row];

    if ([task.weekly boolValue]){
        task.finished = [NSNumber numberWithBool:1];
    } else {
        [managedObjectContext deleteObject:task];
    }

    [self.managedObjectContext save:nil];

    [self grabTasksFromContext];

   }

}

-(void) grabTasksFromContext{
   NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
   NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"Task"  
   inManagedObjectContext:managedObjectContext];
   [fetchRequest setEntity:entity];
   NSError *error;
   NSMutableArray * managedObjects = [NSMutableArray arrayWithArray:[managedObjectContext  
   executeFetchRequest:fetchRequest error:&error]];
   int numObjects = [managedObjects count];

   for (int i = 0; i < numObjects; i ++){
       Task * task = [managedObjects objectAtIndex:i];
       NSLog(@"name %@", task.name);
    // if the task is finished we don't want it to be displayed in the list
       if ([task.finished boolValue]){
           NSLog(@"finished");
           [managedObjects removeObject:task];
           i -= 1;
           numObjects -= 1;
       }
    }

    self.tasks = managedObjects;

// This implementation is pretty ugly
// I'm sorry about that and will fix it in the future
// probably the more attractive way to do this is to make an array of the days, and then              cycle through that and check through the array of tasks
monday = tuesday = wednesday = thursday = friday = saturday = sunday = anyday = 0;

NSMutableArray * mondayArray = [[NSMutableArray alloc] init];
NSMutableArray * tuesdayArray = [[NSMutableArray alloc] init];
NSMutableArray * wednesdayArray = [[NSMutableArray alloc] init];
NSMutableArray * thursdayArray = [[NSMutableArray alloc] init];
NSMutableArray * fridayArray = [[NSMutableArray alloc] init];
NSMutableArray * saturdayArray = [[NSMutableArray alloc] init];
NSMutableArray * sundayArray = [[NSMutableArray alloc] init];
NSMutableArray * anydayArray = [[NSMutableArray alloc] init];

for (Task * task in self.tasks){
    if ([task.day isEqualToString:@"monday"]){
        mondayArray[monday] = task;
        monday++;
    } else if ([task.day isEqualToString:@"tuesday"]){
        tuesdayArray[tuesday] = task;
        tuesday++;
    } else if ([task.day isEqualToString:@"wednesday"]){
        wednesdayArray[wednesday] = task;
        wednesday++;
    } else if ([task.day isEqualToString:@"thursday"]){
        thursdayArray[thursday] = task;
        thursday++;
    } else if ([task.day isEqualToString:@"friday"]){
        fridayArray[friday] = task;
        friday++;
    } else if ([task.day isEqualToString:@"saturday"]){
        saturdayArray[saturday] = task;
        saturday++;
    } else if ([task.day isEqualToString:@"sunday"]){
        sundayArray[sunday] = task;
        sunday++;
    } else {
        anydayArray[anyday] = task;
        anyday++;
    }
 }

    self.tasksByDay = [[NSDictionary alloc] initWithObjectsAndKeys: mondayArray,@"monday",     
    tuesdayArray, @"tuesday", wednesdayArray, @"wednesday", thursdayArray, @"thursday",   
    fridayArray, @"friday", saturdayArray, @"saturday", sundayArray, @"sunday", 
    anydayArray, @"anyday", nil];

     [self.tableView reloadData];
 }

何が起こっているのかについての助けや考えをいただければ幸いです。

4

1 に答える 1

2

ただし、セルを削除すると、その図面はその下のセルにドロップされます。

これは重要な観察事項です。これは、再描画が行われている時点で、モデル (つまり、あなたのself.tasksByDay[dayName]) がまだ更新されていないことを示しています。特定の行のセルが削除されるtaskByDayと、対応する日の を更新して、 から対応する行を削除する必要がありますNSArray。これが発生しない場合、削除されたタスクのデータが次のインデックスのセルの描画に影響を与えるため、ビジュアルが 1 行ずつ「ドロップ」するように見えます。あなたの説明から、これがまさに起こっていることのように聞こえます。

UITableViewテーブルが更新される (またはセル削除の通知が に送信される) までに、行が削除されないようにモデルが既に更新されていることを確認する必要があります。このようにして、期待どおりにテーブル ビジュアルが更新されます。

問題とは直接関係ありませんが、配列を作成すると

NSArray *dayName = @[@"anyday", @"monday", @"tuesday", @"wednesday", etc.];

ifs の長いチェーンを次のように置き換えることができます

task = [self.tasksByDay[dayName objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

コードのもう 1 つの問題は、 を呼び出すたびdrawFillInAtPercent:に、新しいCAGradientLayerが追加されることです。上下にスクロールすると、再利用されたセルは、以前に追加されたレイヤーを削除することなく、新しいレイヤーを蓄積します。コードを変更してグラデーション レイヤーを 1 回だけ追加し、メソッド内で既存のレイヤーを再利用する必要がありますdrawFillInAtPercent:。たとえば、 の指定されたイニシャライザにレイヤを追加し、AGProgressViewCellそれをインスタンス変数に割り当てて、_progressLayer一度レイヤ階層に追加することができます。それ以降は、毎回新しいものを作成するのではなくdrawFillInAtPercent:、既存のものを変更します。_progressLayer

于 2013-02-28T02:43:29.477 に答える