开发者

values in the text field are becoming null when we scroll the page in table view

hii every one

i have created a data entry screen like this in which each row contains a text field

values in the text field are becoming null when we scroll the page in table view

here after entering data into the text field if we scroll the page which are all the text fields goesup(beyond the view) will set to null when it come back to the original position,, this is my code for each cell where CustomCellStudentData is a cell class where i have created frames for lables

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

    CustomCellStudentData *cell = (CustomCellStudentData*)[tableView dequeueReusableCellWithIdentifier:nil];

    if (cell == nil) {
      cell = [[[CustomCellStudentData alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    switch(indexPath.section)
    { 
        case 0:
            switch (indexPath.row) 
            {
                case 0:
                {
                    tfText[0] = [[UITextField alloc] initWithFrame:CGRectMake(125,22, 170, 25)];
                    cell.accessoryView = tfText[0];
                    tfText[0].delegate=self;
                    tfText[0].font = [UIFont systemFontOfSize:14];
                    tfText[0].placeholder =@"<Student Name>";

                    cell.primaryLabel.text =@"Student Name: ";

                } 
                break;

                case 1: 
                {
  开发者_如何学JAVA                  tfText[1] = [[UITextField alloc] initWithFrame:CGRectMake(125,22, 170, 25)];
                    cell.accessoryView = tfText[1];
                    tfText[1].delegate=self;
                    tfText[1].font = [UIFont systemFontOfSize:14];
                    tfText[1].placeholder =@"<Student Age>";
                    tfText[1].keyboardType = UIKeyboardTypeNumbersAndPunctuation;
                   cell.primaryLabel.text =@"Student Age: ";
                } 
                break;

                case 2:
                {
                    cell.primaryLabel.text =@"Gender: ";

                    segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
                    segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
                    segmentedControl.frame = CGRectMake(125,22, 170, 25);
                    [segmentedControl insertSegmentWithTitle:@"Male" atIndex:0 animated:YES];
                    [segmentedControl insertSegmentWithTitle:@"Female" atIndex:1 animated:YES];
                    segmentedControl.selectedSegmentIndex = 1;
                    [segmentedControl setMomentary:NO];
                    [segmentedControl addTarget:self action:@selector(segmentSwitch:) forControlEvents:UIControlEventValueChanged];
                    cell.accessoryView =segmentedControl;
                }
                break;

                case 3:
                {
                    tfText[3] = [[UITextField alloc] initWithFrame:CGRectMake(125,22, 170, 25)];
                    cell.accessoryView = tfText[3];
                    tfText[3].delegate=self;
                    tfText[3].font = [UIFont systemFontOfSize:14];
                    tfText[3].placeholder =@"<dd/mm/yyyy>";
                    tfText[3].keyboardType = UIKeyboardTypeNumbersAndPunctuation;
                    cell.primaryLabel.text =@"Date Of Birth: ";
                }
    }
}

y that data becoming null, how can i resolve this ,, can any one help me,, thanx in advance


You are creating the text fields every time cellForRowAtIndexPath: is called. I think you can solve this problem by creating the text fields elsewhere, probably in viewDidLoad or similar method and just set the accessory view when the cellForRowAtIndexPath: is called. For example,

tfText[0] = [[UITextField alloc] initWithFrame:CGRectMake(125,22, 170, 25)];
tfText[0].delegate=self;
tfText[0].font = [UIFont systemFontOfSize:14];
tfText[0].placeholder =@"<Student Name>";

Move the top four lines to viewDidLoad and create them only once. And let the line below remain as it is.

cell.accessoryView = tfText[0];


Because each time you make a cell you recreate the text field.

Take the first cell (at index 0)

                tfText[0] = [[UITextField alloc] initWithFrame:CGRectMake(125,22, 170, 25)];
                cell.accessoryView = tfText[0];
                tfText[0].delegate=self;
                tfText[0].font = [UIFont systemFontOfSize:14];
                tfText[0].placeholder =@"<Student Name>";

                cell.primaryLabel.text =@"Student Name: ";

The first time the table is created you will make a text view and add it to the cell. The user will then enter their data into the cell (in this case their name).

Then, they scroll the table down and back up.

The table view will call tableView:cellForRowAtIndexPath: again. Your code replaces the text field at index 0 with a new, empty one.

Try something like this in your switch statement instead :

if (NULL == tfText[0]) {
    tfText[0] = [[UITextField alloc] initWithFrame:CGRectMake(125,22, 170, 25)];
    tfText[0].delegate=self;
    tfText[0].font = [UIFont systemFontOfSize:14];
    tfText[0].placeholder =@"<Student Name>";
}
cell.accessoryView = tfText[0];

cell.primaryLabel.text =@"Student Name: ";


dequeueReusableCellWithIdentifier:nil

I believe this is the problem, you might be getting some other type of cell there. You should set a cell identifier for each type of cell you use in that table.


It looks like you are dealing with a finite number of cells... maybe you shouldn't bother with dequeue... Just deal with them like normal control elements... Stick them in an array in view did load and deliver them in cellForRowAtIndexPath

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜