0

セルの行を移動してからそのプロパティにアクセスすると、本当に奇妙な不具合が発生します。正直に説明するのが難しいので、iOS シミュレーターで発生しているグリッチを記録しました。ストーリーボードは使用していません。

ここにビデオがあります: iPhoneグリッチ

関連するコードを次に示します。

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]];

}
4

1 に答える 1

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

ビューコントローラーをプッシュしてポップした後にのみエラーが表示される理由は、ユーザーが行を移動しても行がリロードされないためだと思います。リロードした場合、その時点で不具合が発生していたはずです。

お役に立てれば!

于 2013-07-01T20:26:05.707 に答える