在滚动之前加载UITableView中的所有单元格

Esh*_*nya 8 iphone uitableview

我有一个UITableView,总共有8个单元格(部分),其中6个包含textFields作为子视图,2个包含按钮,另一个包含按钮,另一个包含文本视图.现在我遇到了一个问题,因为我们都知道只有视图出现,可见单元格加载.所以当我的应用程序加载时,我能够查看5个单元格,前4个包含文本字段,第5个单元格带按钮.我可以看到当我显示时数组计数为4它通过登录控制台即

there are 4 objects in the array
Run Code Online (Sandbox Code Playgroud)

现在滚动表视图后,我可以观察到以下内容:

there are 6 objects in the array
Run Code Online (Sandbox Code Playgroud)

所以,正在发生的是如果我要保存在表视图中输入的数据,或者说编辑以更改现有数据,我被迫滚动表视图并在最后一个单元格中输入值.所以不能是总是与用户一起使用,因为我们不能指望他/她滚动表格视图并输入完整的表格/表格视图单元格条目.他/她做出的改变是什么,只需点击完成/保存它是什么. .!

更多我有一个选择器视图作为包含文本字段的最后一个单元格的输入视图作为subview.Hence在我的选择器视图中选择行方法我面临崩溃问题与最后一个单元格/部分(textField as subview)包含一个选择器视图这里是我的方法代码片段:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

    switch (tagValues) 
    {

        case 105:
        {
            self.textField = [self.fields objectAtIndex:3];
            self.textField.text = [self.reminder objectAtIndex:row];
        }
            break;

        case 107:
        {
            //Crash occurring in this case,sometimes its working wonder why
            self.textField = [self.fields objectAtIndex:5];
            self.textField.text = [self.group objectAtIndex:row];
        }
            break;

    }

}

**Note**:I am using the same textField for adding as subview to all 6 cells,but each one with unique tags and own purpose,i.e. one for editing text using key board,one with date picker,one with general picker etc...
Run Code Online (Sandbox Code Playgroud)

我想知道为什么有一些时候从选择器视图中选择值(最后一个单元格)崩溃了一些时间并且有些时候工作正常.有些时候它崩溃在上面一行注释的情况107,有时在主自动释放池中.有些时候线程如图所示在下面的快照:

在此输入图像描述

那么有没有什么方法可以一次加载所有单元格,以便在滚动之前将所有文本字段添加到数组中.我相信不会有任何问题

更多详细的编辑代码以便理解:

- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

     //NSString *identifier = @"UITableViewCell";

    NSString *cellIdentifierF = nil;

    static NSString *firstCellIdentifier = @"FirstCell";
    static NSString *secondCellIdentifier = @"SecondCell";
    static NSString *thirdCellIdentifier = @"ThirdCell";
    static NSString *fourthCellIdentifier = @"FourthCell";
    static NSString *fifthCellIdentifier = @"FifthCell";
    static NSString *sixthCellIdentifier = @"SixthCell";
    static NSString *seventhCellIdentifier = @"SeventhCell";
    static NSString *eightCellIdentifier = @"EightCell";

    if(indexPath.section == 0 && indexPath.row == 0) 
    {
        cellIdentifierF = firstCellIdentifier;
    } 
    else if(indexPath.section == 1 && indexPath.row == 0)
    {
        cellIdentifierF = secondCellIdentifier;
    }
    else if(indexPath.section == 2 && indexPath.row == 0)
    {
        cellIdentifierF = thirdCellIdentifier;
    }
    else if(indexPath.section == 3 && indexPath.row == 0)
    {
        cellIdentifierF = fourthCellIdentifier;
    }
    else if(indexPath.section == 4 && indexPath.row == 0)
    {
        cellIdentifierF = fifthCellIdentifier;
    }
    else if(indexPath.section == 5 && indexPath.row == 0)
    {
        cellIdentifierF = sixthCellIdentifier;
    }
    else if(indexPath.section == 6 && indexPath.row == 0)
    {
        cellIdentifierF = seventhCellIdentifier;
    }
    else if(indexPath.section == 7 && indexPath.row == 0)
    {
        cellIdentifierF = eightCellIdentifier;
    }

    UITableViewCell *cell = (UITableViewCell *)[atableView dequeueReusableCellWithIdentifier:cellIdentifierF];

        atableView.backgroundColor = [UIColor clearColor];

        textField = [[[UITextField alloc]initWithFrame:CGRectMake(15, 12, 300, 24)]autorelease];
        textField.textColor = [UIColor whiteColor];
        textField.delegate = self;
        tagValues = textField.tag;

    switch (indexPath.section) 
    {
        case 0:
        {
            if (cell == nil) 
            {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifierF] autorelease];
                cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"buttonbg-1.png"]];

                //Only add content to cell if it is new
                if([cellIdentifierF isEqualToString: firstCellIdentifier])
                {
                    textField.placeholder = @"Enter name";
                    [self.textField setValue:[UIColor purpleColor] 
                                  forKeyPath:@"_placeholderLabel.textColor"];
                    textField.tag = 101;
                    textField.text = reminderInstance.Name;
                    textField.autocorrectionType = UITextAutocorrectionTypeNo;
                    NSLog(@"Value = %@",textField.text);
                    [cell.contentView addSubview:textField];
                    [self.fields addObject:textField];
                }
            }
        }
            break;

        case 1:
        {
            if (cell == nil) 
            {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifierF] autorelease];
                cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"buttonbg-1.png"]];

                //Only add content to cell if it is new
                if([cellIdentifierF isEqualToString: secondCellIdentifier])
                {
                    textField.tag = 102;
                    textField.text = reminderInstance.Event;
                    NSLog(@"Value = %@",textField.text);
                    [cell.contentView addSubview:textField];
                    [self.fields addObject:textField];
                }
            }
        }
            break;

        case 2:
        {
            if (cell == nil) 
            {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifierF] autorelease];
                cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"buttonbg-1.png"]];

                //Only add content to cell if it is new
                if([cellIdentifierF isEqualToString: thirdCellIdentifier])
                {
                    textField.placeholder = @"Click here to set date and time";
                    [self.textField setValue:[UIColor purpleColor] 
                                  forKeyPath:@"_placeholderLabel.textColor"];
                    textField.inputView = self.datePicker;
                    textField.text = reminderInstance.Date;
                    textField.tag = 103;
                    NSLog(@"Value = %@",textField.text);
                    [cell.contentView addSubview:textField];
                    [self.fields addObject:textField];
                }
            }
        }   
            break;

        case 3:
        {
            if (cell == nil) 
            {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifierF] autorelease];
                cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"buttonbg-1.png"]];

                //Only add content to cell if it is new
                if([cellIdentifierF isEqualToString: fourthCellIdentifier])
                {
                    textField.tag = 105;
                    textField.text = reminderInstance.numDays;
                    textField.inputView = self.reminderPicker;
                    NSLog(@"Value = %@",textField.text);
                    [cell.contentView addSubview:textField];
                    [self.fields addObject:textField];
                }
            }
        }
            break;

        case 4:
        {   
            if (cell == nil) 
            {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifierF] autorelease];
                cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"buttonbg-1.png"]];

                //Only add content to cell if it is new
                if([cellIdentifierF isEqualToString: fifthCellIdentifier])
                {
                    checkboxButton = [[[UIButton alloc] initWithFrame:CGRectMake(16,1,120, 44)]autorelease];
                    [checkboxButton setImage:[UIImage imageNamed:@"ewee.png"] forState:UIControlStateNormal];
                    [checkboxButton addTarget:self action:@selector(toggleButton:) forControlEvents:UIControlEventTouchUpInside];

                    NSString *one = reminderInstance.selString;
                    NSNumber* i = [NSNumber numberWithInt:[one intValue]];
                    BOOL isOn = [i boolValue];

                    if(isOn)
                    {
                        [checkboxButton setImage:[UIImage imageNamed:@"checkarrow.png"] forState:UIControlStateNormal];
                    }
                    else
                    {
                        [checkboxButton setImage:[UIImage imageNamed:@"ewee.png"] forState:UIControlStateNormal];
                    }            
                    NSLog(@"String Val = %@",one);

            [checkboxButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
            [checkboxButton setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0)];
            [cell addSubview:checkboxButton];
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(55, 10, 225, 24)];
            label.text = @"Every Year";
            label.textColor = [UIColor whiteColor];
            label.backgroundColor = [UIColor clearColor];
            [cell addSubview:label];
            cell.textLabel.textColor = [UIColor whiteColor];
            [label release];
                }
            }

        }
            break;

        case 5:
        {
            if (cell == nil) 
            {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifierF] autorelease];
                cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"buttonbg-1.png"]];

                //Only add content to cell if it is new
                if([cellIdentifierF isEqualToString: sixthCellIdentifier])
                {
                    textField.placeholder = @"Enter the number here";
                    [self.textField setValue:[UIColor purpleColor] 
                                    forKeyPath:@"_placeholderLabel.textColor"];
                    textField.text = num;
                    textField.text = reminderInstance.number;
                    textField.tag = 106; 
                    textField.userInteractionEnabled = YES;
                    textField.keyboardType = UIKeyboardTypeNumberPad;
                    NSLog(@"Value = %@",textField.text);
                    [cell.contentView addSubview:textField];
                    [self.fields addObject:textField];
                }
            }
        }
            break;

        case 6:
        {
            if (cell == nil) 
            {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifierF] autorelease];
                cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"reminderbuttonxl.png"]];

                //Only add content to cell if it is new
                if([cellIdentifierF isEqualToString: seventhCellIdentifier])
                {
                    cell.backgroundColor = [UIColor clearColor];
                    textView.clipsToBounds = YES;
                    textView = [[UITextView alloc]initWithFrame: CGRectMake(-2, -3, 307, 154)];
                    UIImageView *imgView = [[[UIImageView alloc]initWithFrame: textView.frame]autorelease];
                    imgView.image = [UIImage imageNamed: @"buttonbg_text.png"];
                    [textView addSubview: imgView];
                    [textView sendSubviewToBack: imgView];
                    textView.backgroundColor = [UIColor clearColor];
                    textView.delegate = self;
                    textView.tag = 11;
                    tagValues = textView.tag;
                    textView.textColor = [UIColor purpleColor];
                    [cell.contentView addSubview:textView];
                    textView.text = @"Your birthday wishes/other reminder body here";
                    NSLog(@"Value = %@",textView.text);
                }
            }
        }
            break;

        case 7:
        {
            if (cell == nil) 
            {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifierF] autorelease];
                cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"buttonbg-1.png"]];

                //Only add content to cell if it is new
                if([cellIdentifierF isEqualToString: eightCellIdentifier])
                {
                    textField.tag = 107;
                    textField.inputView = self.groupPicker;
                    tagValues = textField.tag;
                    textField.text = reminderInstance.remGroup;
                    NSLog(@"Value = %@",textField.text);
                    [cell.contentView addSubview:textField];
                    [self.fields addObject:textField];
                }
            }
        }
            break;

        default:
            break;
    }

    int size = [fields count];
    NSLog(@"there are %d objects in the array", size);

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

我已经多次尝试找出究竟出了什么问题,我也在这里那里检查了一些问题.但是找不到任何有效/ appr.回答或解决我的问题.

所以任何人都可以帮助我提供有价值的建议或示例代码片段.

在此先感谢所有:)

Eha*_*mer 5

有道理的是,您构建表单的方式(实际上就是这种情况)会出现很多问题,当我需要构建这样的表单时,我自己一直在做的是使用构建正常查看所有控件,无论它有多长,并在滚动视图中使用该表单,并使用其中的视图大小设置其内容大小。这将使您可以更好地控制该表单的外观。而且我认为我没有看到使用 tableview 作为表单条目的应用程序。

您可以使用界面构建器来构建表单(只需在 Xcode 上作弊,使主视图和滚动视图足够大,以便您可以看到构建器上的所有内容,然后在完成后将其调整为适当的大小)