簡體   English   中英

在UITableView中選擇時未清除UITextViews

[英]UITextViews not cleared when selected in a UITableView

我正在實現帶有文本視圖作為單元格內容視圖的UITableView。

當用戶按下返回鍵時,用戶輸入的數據將保存在設置字典中:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == _textFieldOne){
        [settingsDictionary setObject: _textFieldOne.text forKey:@"EntryOneText"];
        [settingsDictionary stringValueForKey:@"textFieldOneValue"];
        [self postNotificationSettingsUpdate:settingsDictionary];
        didTestPostNotificationSettings = YES;
}

當用戶返回屏幕時,這些保存的值應顯示在文本字段中,這是我使用以下代碼完成的:

    //Set the value in the text fields from the settings dictionary
    NSString *textFieldOneText = [self.settingsDictionary       stringValueForKey:@"RedZoneCarsPerManHrMin"];

    _textFieldOne.text = textFieldOneValue;

到目前為止,一切似乎都按預期工作:文本輸入已保存,並在用戶返回屏幕時顯示在文本字段中。 但是,如果選擇了包含文本字段的TableView的 (而不是文本字段本身 ),則文本字段將顯示以下行為:

在此處輸入圖片說明

似乎該文本字段同時顯示了新輸入的條目以及上次保存的條目。

編輯我在下面添加了更多的cellForRowAtIndexPath方法:

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

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

    cell.backgroundColor = [UIColor colorWithRed:0.922 green:0.937 blue:0.949 alpha:1];

    UISwitch *cellSwitch = nil;

    NSNumber *position = enabledRedZoneAlertRows[indexPath.row];


switch (position.integerValue) {
    case CarPerManHourRow: {
        cell.textLabel.text = NSLocalizedString(@"Label", @"Label Row");
        cellSwitch = [[UISwitch alloc] init];
        cellSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:SwitchKey];
        cellSwitch.tag = SwitchTag;
        [cellSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
        cell.accessoryView = cellSwitch;

        if ([[self.settingsDictionary valueForKey:@"RedZoneCarsPerManHrOnOff"]isEqual: @"1"]){
            [cellSwitch setOn:YES];
        }
        break;
    }
    case TextFieldRow: {
        static NSString *CellIdentifier = @"ConfigCell";
        cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        [cell.contentView addSubview:_textFieldOne];
        [cell.contentView addSubview:_textFieldTwo];
        [cell.contentView addSubview:_spacingLabel];
        }

        cell.backgroundColor = [UIColor colorWithRed:0.922 green:0.937 blue:0.949 alpha:1];

        _textFieldOne = [[UITextField alloc]initWithFrame:CGRectMake(25, 0, 50, 30)];
        _textFieldTwo = [[UITextField alloc]initWithFrame:CGRectMake(100, 0, 50, 30)];

        _textFieldOne.delegate = self;
        _textFieldTwo.delegate = self;

        _textFieldOne.placeholder = @"Auto";
        _textFieldTwo.placeholder = @"Auto";
        [_textFieldOne setTextAlignment:NSTextAlignmentCenter];
        [_textFieldTwo setTextAlignment:NSTextAlignmentCenter];

        //Set the value in the text fields from the settings dictionary
       NSString *textFieldOneText = [self.settingsDictionary stringValueForKey:@"RedZoneCarsPerManHrMin"];
       NSString *textFieldTwoText = [self.settingsDictionary stringValueForKey:@"RedZoneCarsPerManHrMax"];

        NSString *carsPerManHrMax = [self.settingsDictionary stringValueForKey:@"RedZoneCarsPerManHrMax"];
        NSString *carsPerManHrMax = [self.settingsDictionary stringValueForKey:@"RedZoneCarsPerManHrMax"];

        _textFieldOne.text = textFieldOneValue;
        _textFieldTwo.text = textFieldTwoValue

        _textFieldOne.backgroundColor = [UIColor whiteColor];
        _textFieldTwo.backgroundColor = [UIColor whiteColor];
        _textFieldOne.tintColor = [UIColor blackColor];
        _textFieldTwo.tintColor = [UIColor blackColor];

        [_textFieldOne.layer setCornerRadius:7.0f];
        [_textFieldTwo.layer setCornerRadius:7.0f];

        [_textFieldOne setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
        [_textFieldTwo setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];

        _textFieldOne.returnKeyType = UIReturnKeyNext;
        _textFieldTwo.returnKeyType = UIReturnKeyDone;

        _spacingLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 0, 35, 30)];
        _spacingLabel.text = @"–";
        _spacingLabel.textColor = [UIColor colorWithRed:0.658 green:0.658 blue:0.658 alpha:1];
        [_spacingLabel setTextAlignment:NSTextAlignmentCenter];
        _spacingLabel.backgroundColor = [UIColor clearColor];

        //[cell.contentView addSubview:_textFieldOne];
        //[cell.contentView addSubview:_textFieldTwo];
        //[cell.contentView addSubview:_spacingLabel];

        break;
    }
   return cell;
}

編輯兩個下面是我根據jherran的答案來解決我的問題的嘗試。 但是,我仍然遇到同樣的問題。

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

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

    cell.backgroundColor = [UIColor colorWithRed:0.922 green:0.937 blue:0.949 alpha:1];

    UISwitch *cellSwitch = nil;

    NSNumber *position = enabledRedZoneAlertRows[indexPath.row];

    switch (position.integerValue) {
        case CarPerManHourRow: {
            cell.textLabel.text = NSLocalizedString(@"Label", @"Label Row");
            cellSwitch = [[UISwitch alloc] init];
            cellSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:SWSettingCarsPerManHour];
            cellSwitch.tag = SaveCarsPerManHourTag;
            [cellSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
            cell.accessoryView = cellSwitch;

            if ([[self.settingsDictionary valueForKey:@"RedZoneCarsPerManHrOnOff"]isEqual: @"1"]){
                [cellSwitch setOn:YES];
            }
            break;
        }
        case CarsPerManHourConfigRow: {

            static NSString *CellIdentifier = @"ConfigCell";
            cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];

                cell.backgroundColor = [UIColor colorWithRed:0.922 green:0.937 blue:0.949 alpha:1];

                _carsPerManHourMinTextField = [[UITextField alloc]initWithFrame:CGRectMake(25, 0, 50, 30)];
                _carsPerManHourMaxTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 0, 50, 30)];

                _textFieldOne.delegate = self;
                _textFieldTwo.delegate = self;

                _textFieldOne.placeholder = @"Auto";
                _textFieldTwo.placeholder = @"Auto";
                [_textFieldOne setTextAlignment:NSTextAlignmentCenter];
                [_textFieldTwo setTextAlignment:NSTextAlignmentCenter];




                _textFieldOne.backgroundColor = [UIColor whiteColor];
                _textFieldTwo.backgroundColor = [UIColor whiteColor];
                _textFieldOne.tintColor = [UIColor blackColor];
                _textFieldTwo.tintColor = [UIColor blackColor];


                [_textFieldOne.layer setCornerRadius:7.0f];
                [_textFieldTwo.layer setCornerRadius:7.0f];

                [_textFieldOne setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
                [_carsPerManHourMaxTextField setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];

                _textFieldOne.returnKeyType = UIReturnKeyNext;
                _textFieldTwo.returnKeyType = UIReturnKeyDone;

                _spacingLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 0, 35, 30)];
                _spacingLabel.text = @"–";
                _spacingLabel.textColor = [UIColor colorWithRed:0.658 green:0.658 blue:0.658 alpha:1];
                [_spacingLabel setTextAlignment:NSTextAlignmentCenter];
                _spacingLabel.backgroundColor = [UIColor clearColor];

                NSLog(@"%@", _textFieldOne.text);
                NSLog(@"%@",_carsPerManHourMaxTextField.text);

                [cell.contentView addSubview: _textFieldOne];
                [cell.contentView addSubview: _textFieldTwo];
                [cell.contentView addSubview:_spacingLabel];

                cell.tag = 1;
            }

            else {
                _textFieldOne = (UITextField *)[cell.contentView viewWithTag:1];
                _textFieldTwo = (UITextField *)[cell.contentView viewWithTag:1];
                _spacingLabel = (UILabel *)[cell.contentView viewWithTag:1];
            }

            NSString * _textFieldOne = [self.settingsDictionary stringValueForKey:@"RedZoneCarsPerManHrMin"];
            NSString *carsPerManHrMax = [self.settingsDictionary stringValueForKey:@"RedZoneCarsPerManHrMax"];

            _textFieldOne.text = carsPerManHrMin;
            _textFieldTwo.text = carsPerManHrMax;

如何修改我的代碼,以便不會發生此現象?

檢查cellForRowAtIndexPath ,因為單元被重用,您可能未加載單元屬性。

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

    /*
     *   This is an important bit, it asks the table view if it has any available cells
     *   already created which it is not using (if they are offscreen), so that it can
     *   reuse them (saving the time of alloc/init/load from xib a new cell ).
     *   The identifier is there to differentiate between different types of cells
     *   (you can display different types of cells in the same table view)
     */

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

    /*
     *   If the cell is nil it means no cell was available for reuse and that we should
     *   create a new one.
     */
    UILabel *label;
    if (cell == nil) {

        /* 
         *   Actually create a new cell (with an identifier so that it can be dequeued). 
         */

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease];

        label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
        cell.tag = 1;
        [cell.contentView addSubview:label];

    } else {

        label = (UILabel *)[cell.contentView viewWithTag:1];

    }

    /*
     *   Now that we have a cell we can configure it to display the data corresponding to
     *   this row/section
     */

     label.text = @"whatever";
}

編輯:當單元格退出並被重用時,每次單元格被重用時,您都將添加一個視圖( [cell.contentView addSubview:_textFieldOne] )。

好的,這是我所懷疑的。 每次將單元格從重用隊列中取出時,您都將創建一個新的文本字段實例,並將其添加到內容視圖中。 因此,基本上,內容視圖會堆積新的文本字段。 你應該做的是

1)具有一個自定義單元,並使該單元成為負責其內部的唯一類

2)僅公開一種配置方法以將原始文本傳遞給單元格

3)當單元被重用時,調用該配置方法。

4)保留對自定義單元格內文本字段的引用,以便能夠在任何時間(包括時間)更新文本值。 在cellforrowatindexpath中回收時

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM