删除后重用UITableViewCell格式

时间:2013-02-28 02:35:24

标签: ios objective-c uitableview

我将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];
 }

任何有关正在发生的事情的帮助或想法都将受到赞赏。

1 个答案:

答案 0 :(得分:2)

  

然而,当我删除一个单元格时,该绘图会下降到它下面的单元格。

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

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

与问题没有直接关系,但是如果你创建了一个数组

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

您可以用

替换if s的长链
task = [self.tasksByDay[dayName objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

代码中的另一个问题是,每次拨打drawFillInAtPercent:时,都会添加新的CAGradientLayer。当您向上和向下滚动时,重用的单元格会累积新的图层,而不会删除之前添加的图层。您需要更改代码以仅添加一次渐变图层,然后重复使用drawFillInAtPercent:方法中的现有图层。例如,您可以在AGProgressViewCell的指定初始值设定项中添加图层,将其分配给_progressLayer实例变量,并将其添加到图层层次结构中一次。从那时起,drawFillInAtPercent:将更改现有的_progressLayer,而不是每次都创建新的。{/ p>