移动单元格然后加载其属性时出现奇怪错误



我在移动单元格行然后访问它们的属性时遇到了一个非常奇怪的小故障。老实说,这太难解释了,所以我记录了iOS模拟器上发生的故障。我没有使用故事板。

以下是视频:iPhone Glitch

以下是一些相关代码:

TableViewController.m:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (!cell)
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCell"];
    NSString *detailText = [NSString stringWithFormat:@"%.0f", [[taskArray objectAtIndex:[indexPath row]] timeInterval]];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    [cell setBackgroundColor:[UIColor colorWithRed:236.0/255 green:240.0/255 blue:241.0/255 alpha:1.0f]];
    cell.textLabel.textColor = turquoiseColor;
    [[cell detailTextLabel] setText:detailText];
    [[cell detailTextLabel] setFont:[UIFont fontWithName:@"Avenir-Black" size:12]];
    [[cell textLabel] setFont:[UIFont fontWithName:@"AvenirNext-DemiBold" size:16]];
    if([indexPath section] == 0){
    cell.textLabel.text = [[[taskArray objectAtIndex:[indexPath row]] taskName] uppercaseString];
    } else if ([indexPath section] == 1) {
    cell.textLabel.text = [[[completedArray objectAtIndex:[indexPath row]] taskName] uppercaseString];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    Tasks *task = [[Tasks alloc]init];
    task.taskName = [[taskArray objectAtIndex:[indexPath row]] taskName];
    task.timeInterval = [[taskArray objectAtIndex:[indexPath row]] timeInterval];
    task.dateCreated = [[taskArray objectAtIndex:[indexPath row]] dateCreated];
    DetailViewController *dvc = [[DetailViewController alloc]init];
    [dvc setTestTask:task];
    [[self navigationController] pushViewController:dvc animated:YES];
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
   Tasks *t = [[Tasks alloc]init];
    if (sourceIndexPath.row > destinationIndexPath.row) {
        [taskArray insertObject:t atIndex:destinationIndexPath.row];
        [taskArray removeObjectAtIndex:(sourceIndexPath.row + 1)];
    }
    else if (sourceIndexPath.row < destinationIndexPath.row) {
        [taskArray insertObject:t atIndex:(destinationIndexPath.row + 1)];
        [taskArray removeObjectAtIndex:(sourceIndexPath.row)];
    }
}

DetailViewController.h(单元格属性)

@class Tasks;
@interface DetailViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *timeLeft;
@property (weak, nonatomic) IBOutlet UILabel *timerLabel;
@property (nonatomic, strong) Tasks *testTask;
@end

DetailViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    [_timeLeft setFont:[UIFont fontWithName:@"BebasNeue" size:25]];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSString *time = [NSString stringWithFormat:@"%.0f", [testTask timeInterval]];
    [_timerLabel setText:time];
    [_timerLabel setFont:[UIFont fontWithName:@"BebasNeue" size:60]];
    [[self navigationItem] setTitle:[testTask taskName]];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [testTask setTaskName:[testTask taskName]];
    [testTask setTimeInterval:[testTask timeInterval]];
}

每当用户移动一行时,都会插入一个空的Tasks对象。相反,尝试在数据源数组中移动项目:

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    Tasks *taskToMove = [taskArray objectAtIndex:sourceIndexPath.row];
    [taskArray removeObjectAtIndex:sourceIndexPath.row];
    [taskArray insertObject:taskToMove atIndex:destinationIndexPath.row];
}

我认为,只有在推送视图控制器并弹出它之后才会看到错误的原因是,当用户移动一行时,该行不会重新加载。如果它重新加载,那么你就会看到故障。

希望这能有所帮助!

相关内容

最新更新