由UITextField引起的cellForRow的奇怪行为

时间:2012-06-30 16:11:51

标签: iphone ios xcode

我有一个TableView,它有2个部分,第2个部分仅在选择第一个部分的第3行时显示。只有第一部分有图像。一切都很好。第2部分中的第3行显示选中的TextField和按钮,您可以通过按完成按钮在此处保存日期。工作也很好。您可以选择该行并转到其他行,一切正常。但是,如果您点击TextField并且不保存输入或不写任何内容,那么它就会出错。 它将以随机行的形式重新加载表格。 我认为这是弹出的键盘,所以我试图手动关闭它。 Bug还在那里。 我很好奇为什么会这样,所以我希望能得到帮助!在此先感谢!!

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

1 个答案:

答案 0 :(得分:0)

此处滥用单元格标识符。删除单元标识符将是此处最快的解决方案,每次都重新分配单元格。 重写代码以包含单元格标识符将使其更加健壮。