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

时间:2013-07-01 19:56:28

标签: iphone ios objective-c uitableview

当移动细胞行然后访问它们的属性时,我有一个非常奇怪的故障。说实话太难以解释所以我记录了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]];

}

1 个答案:

答案 0 :(得分:0)

每当用户移动一行时,您都会插入一个空的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];
}

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

希望这有帮助!