繁体   English   中英

具有文本字段可扩展的Tableview单元格导致文本字段跳转

[英]Tableview cell expandable with text field causes textfield to jump around

因此,我已经尝试了一段时间,以创建具有可扩展部分和一个不可扩展部分的表视图。 可扩展部分之一应在其中包含3个文本字段,您可以在其中编辑文本字段中的测试。 当您折叠该部分并将其再次扩展时,我能够获得工作的bt,文本字段突然在上方重复,有时会与上方的单元格互换。 Ihavent能够弄清楚为什么或不这样做的原因。 这个想法是当用户在字段中输入文本并选择输入时,文本将存储到数组中。

编码:

    - (void)viewDidLoad{
    [super viewDidLoad];

    if (!expandedSections){
        expandedSections = [[NSMutableIndexSet alloc] init];
    }
    manualSensorName = [[NSMutableArray alloc]initWithObjects: @"Sensor",@"",@"2",@"T", nil];
}

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Expanding

- (BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section{
    if (section>0) return YES;

    return NO;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([self tableView:tableView canCollapseSection:section])
    {
        if ([expandedSections containsIndex:section])
        {
            return 5; // return rows when expanded
        }

        return 1; // only top row showing
    }

    // Return the number of rows in the section.
    return 1;
}

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

    // Configure the cell...

    if ([self tableView:tableView canCollapseSection:indexPath.section]){
        if (!indexPath.row){
            // first row
            cell.textLabel.text = @"Expandable"; // only top row showing

            if ([expandedSections containsIndex:indexPath.section])
            {
                UIImageView *arrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrowUP.png"]];
                cell.accessoryView = arrow;
            }
            else
            {
                UIImageView *arrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrowDOWN.png"]];
                cell.accessoryView = arrow;
            }
        }
        else {
            if (indexPath.row == 1){
                NSString *flightNumText = [manualSensorName objectAtIndex:indexPath.row];
                cell.textLabel.text = flightNumText;
            }
            else if (indexPath.row == 2){
                txtManualSensor = [[UITextField alloc] initWithFrame:CGRectMake(180, 5, 120, 30)];
                txtManualSensor.placeholder = @"Select";
                txtManualSensor.delegate = self;
                txtManualSensor.autocorrectionType = UITextAutocorrectionTypeNo;
                txtManualSensor.backgroundColor = [UIColor whiteColor];
                [txtManualSensor setBorderStyle:UITextBorderStyleBezel];
                [txtManualSensor setTextAlignment:NSTextAlignmentCenter];
                [txtManualSensor setReturnKeyType:UIReturnKeyDone];
//                UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(180, 5, 120, 30)];
//                playerTextField.adjustsFontSizeToFitWidth = YES;
//                playerTextField.textColor = [UIColor blackColor];
//                playerTextField.placeholder = @"SAMPLE";
//                playerTextField.tag = 200;
//                playerTextField.delegate = self;
//                [cell.contentView addSubview:playerTextField];
                cell.textLabel.text = @"Sensor Name";
                [cell addSubview:txtManualSensor];
            }
            else if (indexPath.row == 3){
                cell.textLabel.text = @"Some Detail";
                cell.accessoryView = nil;
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }

        }
    }
    else {
        cell.accessoryView = nil;
        cell.textLabel.text = @"Normal Cell";

    }

    return cell;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    [manualSensorName replaceObjectAtIndex:2 withObject:textField.text];
    return YES;
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];
    return YES;
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}


#pragma mark - Table view delegate

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

    if ([self tableView:tableView canCollapseSection:indexPath.section]){
        if (!indexPath.row){
            [tableView beginUpdates];

            // only first row toggles exapand/collapse
            [tableView deselectRowAtIndexPath:indexPath animated:YES];

            NSInteger section = indexPath.section;
            BOOL currentlyExpanded = [expandedSections containsIndex:section];
            NSInteger rows;

            NSMutableArray *tmpArray = [NSMutableArray array];

            if (currentlyExpanded) {
                rows = [self tableView:tableView numberOfRowsInSection:section];
                [expandedSections removeIndex:section];
            }
            else {
                [expandedSections addIndex:section];
                rows = [self tableView:tableView numberOfRowsInSection:section];
            }

            for (int i=1; i<rows; i++) {
                NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i inSection:section];
                [tmpArray addObject:tmpIndexPath];
            }

            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            if (currentlyExpanded) {
                UIImageView *arrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrowDOWN.png"]];
                [tableView deleteRowsAtIndexPaths:tmpArray
                                 withRowAnimation:UITableViewRowAnimationFade];
                cell.accessoryView = arrow;
            }
            else {
                UIImageView *arrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrowUP.png"]];
                [tableView insertRowsAtIndexPaths:tmpArray
                                 withRowAnimation:UITableViewRowAnimationFade];
                cell.accessoryView =  arrow;
            }
            NSLog(@"tableview row is %ld in section %ld",(long)indexPath.row,(long)indexPath.section);
            [tableView endUpdates];
        }
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        NSLog(@"selected row is %ld in section %ld",(long)indexPath.row,(long)indexPath.section);
        if (indexPath.row == 1) {
            // update text fields in cell table view
        }
    }
}

这可能是因为更换简单UITableViewRowAnimationTopUITableViewRowAnimationFade
更改didSelectRowAtIndexPath索引时, UITableViewCells更改物理位置(请记住, UITableViewUIScrollView ),并且滚动条无法跟踪您的意图

UITableViewRowAnimationTop尝试调整滚动位置,但失败。


其他设计注意事项:

  • 不要将模型(要显示的数据数组)与视图模型(显示模型的UI)混合使用。 didSelectRowAtIndexPath ,您应该首先对模型重新排序,然后将其应用于单元格

  • 考虑不要即时更改索引:您可能更喜欢一个实际反映视图结构的模型,即一棵树。

  • 您是否注意到自己不尊重- (void)tableView:(UITableView *)tableView ,有时在同一方法中使用self tableView:tableViewself.customTableView 您应该使用传递给您的tableView

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM