Ios UITableViewCell格式在删除后重复使用

Ios UITableViewCell格式在删除后重复使用,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我将UITableViewCell子类化。基本上,当您按下单元图层上的UITableViewCell图形时,会导致单元显示不同。但是,当我删除一个单元格时,该图形会下降到它下面的单元格中。在我看来,这似乎表明,细胞的格式正在得到重新使用,这将是正常的。因此,我重新绘制了CellForRowAtIndexPath中的单元格,如下所示 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NS

我将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显示,对于每次重新绘制,单元处于正确的级别。这意味着,由于某种原因,要删除的单元格不会在CellForRowatinex路径中被调用。奇怪的是,文本标签正在更改,只是我在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];
 }
-(void)tableView:(UITableView*)tableView提交的编辑样式:(UITableViewCellEditingStyle)行的编辑样式索引路径:(NSIndexPath*)索引路径{
如果(editingStyle==UITableViewCellEditingStyleDelete){
[自失活单体];
NSArray*days=[NSArray阵列,其对象为:@“anyday”、@“周一”、@“周二”、@“周三”、@“周四”、@“周五”、@“周六”、@“周日”,无];
Task*Task=self.tasksByDay[days[indexPath.section]][indexPath.row];
if([task.weekly布尔值]){
task.finished=[NSNumber numberWithBool:1];
}否则{
[managedObjectContext删除对象:任务];
}
[self.managedObjectContext保存:nil];
[自抓取任务来自上下文];
}
}
-(void)从上下文抓取任务{
NSFetchRequest*fetchRequest=[[NSFetchRequest alloc]init];
NSEntityDescription*实体=[NSEntityDescription]
entityForName:@“任务”
inManagedObjectContext:managedObjectContext];
[FetchRequestSetEntity:entity];
n错误*错误;
NSMutableArray*managedObjects=[NSMutableArray arrayWithArray:[managedObjectContext]
executeFetchRequest:fetchRequest错误:&错误]];
int numObjects=[managedObjects计数];
for(int i=0;i
如果您对正在发生的事情有任何帮助或想法,我们将不胜感激

但是,当我删除一个单元格时,该图形会下降到它下面的单元格中

这是关键的观察结果:它向我表明,在重画发生时,模型(即您的
self.tasksByDay[dayName]
)尚未更新。删除某行的单元格时,需要更新相应日期的
taskByDay
,以从
NSArray
中删除相应的行。如果不发生这种情况,则已删除任务的数据将影响下一个索引中单元格的绘制,因此视觉效果将显示为“下降”一行。从你的描述看来,这正是正在发生的事情

您需要确保在刷新表(或将单元格删除通知发送到
UITableView
)时,模型已经更新,不会删除该行。这样,表格的视觉效果将按照您的预期进行更新

与问题没有直接关系,但如果创建数组

NSArray *dayName = @[@"anyday", @"monday", @"tuesday", @"wednesday", etc.];
如果
s的长链被替换为

task = [self.tasksByDay[dayName objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
代码中的另一个问题是每次