0

2 つのセクションを持つ TableView があり、2 番目のセクションは最初のセクションの 3 行目が選択されている場合にのみ表示されます。最初のセクションのみ画像があります。すべてが正常に機能しています。2 番目のセクションの 3 行目には、TextField と選択されたボタンが表示され、[終了] ボタンを押すとそこに日付を保存できます。正常に動作します。その行を選択して他の行に移動すると、すべてが正常になります。ただし、TextField をタップして入力を保存しないか、何も書き込まないと、バグが発生します。ランダムな行の画像でテーブルをリロードします。ポップアップするのはキーボードだと思ったので、手動で閉じようとしました。バグはまだあります。どうしてこうなったのか、とても気になりますので、ご協力よろしくお願いします!前もって感謝します!!

私の cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...

    BOOL tooltipValue = [[NSUserDefaults standardUserDefaults] boolForKey:@"tooltipSetting"];
    BOOL compactSettingValue = [[NSUserDefaults standardUserDefaults] boolForKey:@"compactModeSetting"];

   NSDictionary *dictionary = [self.settingsArray objectAtIndex:indexPath.section];
   NSArray *array = [dictionary objectForKey:@"Items"];
   NSString *cellValue = [array objectAtIndex:indexPath.row];
   cell.textLabel.text = cellValue;

   if (indexPath.section == 0 && self.iconSetCount < 3) {
        NSDictionary *imagedictionary = [self.imagesArray objectAtIndex:indexPath.section];
        UIImage *cellImage = [[imagedictionary objectForKey:@"images"] objectAtIndex:indexPath.row];
        cell.imageView.image = cellImage;
        self.iconSetCount++;
        NSLog(@"%d",self.iconSetCount);
    }


    if (indexPath.section == 0){
        if (indexPath.row == 0) {
            if (tooltipValue){
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            } else {
                cell.accessoryType = UITableViewCellAccessoryNone;
            }

        }
        if (indexPath.row == 1) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        if (indexPath.row == 2) {
            if (!compactSettingValue){
                cell.accessoryType = UITableViewCellAccessoryNone;
            } else {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
        }
    } else if (indexPath.section == 1){
            // if the sellected row is the one with a custom date 
            if (indexPath.row == 2 && indexPath.row == isSelected) {
                // if an archive exists for the custom date then get the data from the archive
                if ([[NSFileManager defaultManager] fileExistsAtPath:[self getDocumentPathForCustomDate]]) {
                    NSString *customDate = [NSKeyedUnarchiver unarchiveObjectWithFile:[self getDocumentPathForCustomDate]];
                    cell.textLabel.text = customDate;
                } else{
                    //show a label with a done button where you can type in your custom date and save it
                    if (self.textField == nil) {
                        self.textField = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 200, 35)];
                        self.textField.backgroundColor = [UIColor whiteColor];
                        self.textField.textColor = [UIColor blackColor];
                        self.textField.font = [UIFont systemFontOfSize:16.0];
                        self.textField.borderStyle = UITextBorderStyleRoundedRect;
                    }

                    if (self.button == nil) {
                        self.button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
                        self.button.frame = CGRectMake(210, 5, 50, 35);
                        [self.button setTitle:NSLocalizedString(@"done", @"") forState:UIControlStateNormal];
                        [self.button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
                    }

                [cell.contentView addSubview: self.textField];
                [cell.contentView addSubview: self.button];

                }
            }
            if ((indexPath.row == self.isSelected)){
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }else{
                cell.accessoryType = UITableViewCellAccessoryNone;
            }
        }
    return cell;

}

私の didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    // Navigation logic may go here. Create and push another view controller.
    /*
    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"Nib name" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
     */

    BOOL tooltipValue = [[NSUserDefaults standardUserDefaults] boolForKey:@"tooltipSetting"];
    BOOL compactSettingValue = [[NSUserDefaults standardUserDefaults] boolForKey:@"compactModeSetting"];

    if (indexPath.section == 0) {
        //tooltips
        if (indexPath.row == 0) {
            if (!tooltipValue){
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"tooltipSetting"];
            } else {
                [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"tooltipSetting"];
            }
            [self.tableView reloadData];
        }
        //go to Set your Signature Extra
        if (indexPath.row == 1) {
            AdditionalSignature *additionalSigi = [[AdditionalSignature alloc] initWithNibName:@"additionalSignature" bundle:nil];
            [self.navigationController pushViewController:additionalSigi animated:YES];
            [additionalSigi release];
        }

        //compact version
        if (indexPath.row == 2) {
            if (!compactSettingValue){
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"compactModeSetting"];                
            } else {
                [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"compactModeSetting"];
            }      
            [self.tableView reloadData];
        }
    }
    // if in optional compactmode settings
    if (indexPath.section == 1){
        // and "none" is selected remove first the custom date
        if (indexPath.row == 0) {
            [self removeCustomDateArchive];

            //then change the userdefault for this setting
            if (![self isOptionNoDate]) {
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"dateSetting"];
            }else{
                [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"dateSetting"];
            }
        }
        if ( indexPath.row == 1) {
            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"dateSetting"];
            [self removeCustomDateArchive];
        }
        if (indexPath.row == 2) {
            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"dateSetting"];

        }
        self.isSelected = indexPath.row;
        [self.tableView reloadData];
    }
}
4

1 に答える 1

0

ここでは、セル識別子が乱用されています。ここでは、セル識別子を削除するのが最も迅速な解決策であり、毎回セルを再割り当てします。セル識別子を含めるようにコードを書き直すと、より堅牢になります。

于 2012-06-30T19:13:23.143 に答える