簡體   English   中英

在UITableViewCells中維護UITextFields中的文本

[英]Maintaining text in UITextFields in UITableViewCells

我的問題:我有一個用於創建列表的UITableView部分。 每個TableViewCell都包含UITextField。 當您開始鍵入文本字段時,將插入一個新單元格。 所有這些功能都非常有效。 但是,如果用戶創建了許多單元格,它們會離開屏幕並出現問題。 特別是該部分頂部的單元格開始被重用。 這會導致新單元格中出現不需要的文本,並刪除舊單元格中的文本。 我該如何解決這個問題?

這個問題 的圖像:(在第二張圖片中,當我開始輸入第6項時,第1項出現在它下面)

在此輸入圖像描述

在此輸入圖像描述

創建UITableViewCells的代碼:

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


    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        // Intialize TableView Cell
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];


        // Initialize TextField
         // ... code ommitted for brevity

        // Custome intializiation per each kind of TextField
        if (indexPath.section == 0) {
            playerTextField.tag = 0;
            playerTextField.placeholder = @"Title";
        }
        else if (indexPath.section == 1) {

            // Here's where the problem starts *********

            playerTextField.tag = indexPath.row + 1;
            playerTextField.placeholder = @"List Item";
        }

            [cell.contentView addSubview:playerTextField];
    }


    return cell;
}

用於添加/刪除單元格的代碼

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    NSInteger section = [self.tableView indexPathForCell:(UITableViewCell *)textField.superview.superview].section;
    NSInteger row = [self.tableView indexPathForCell:(UITableViewCell *)textField.superview.superview].row;

    if (section == 0) {

        _myList.name = textField.text;
    }
    else if (section == 1) {

        // Delete cell that is no longer used
        if ([string isEqualToString:@""]) {
            if (textField.text.length == 1) {
                if (cellCount > 1) {
                cellCount = cellCount - 1;

                [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:row inSection:1]] withRowAnimation:UITableViewRowAnimationAutomatic];

                textField.text = @"";
                }
            }
        }

        // Add a new cell
        if (self.beganEditing) {
            if (![string isEqualToString:@""]) {
            if (row == [self.tableView numberOfRowsInSection:1] - 1) {
                cellCount = cellCount + 1;
                [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:row + 1 inSection:1]] withRowAnimation:UITableViewRowAnimationAutomatic];
                self.beganEditing = NO;
                }
            }
        }
    }    

    return YES;
}

正如@Bruno所說,你需要觀察你在細胞初始化中投入的內容。 一個好的一般規則是只將通用規則放入cell == nil條件。 依賴於表視圖中單元格位置的任何內容都需要超出此if語句。

在上面的代碼中,您有一條注釋“每種文本字段的自定義初始化”。 這是一個很好的指標,這個代碼需要超出if語句。

其次, 您不能依賴表格視圖單元格來保存您的數據 如果您還沒有,則需要保留一個字符串數組,表示文本字段區域中的每個條目。 然后,當通過表視圖詢問單元格時,可以根據cell == nil 條件 之外的索引路徑設置每個文本字段的文本。

看看這個答案

希望這可以幫助!

你的bug就在

if (cell == nil)

部分代碼。 你不是在考慮else ,這是細胞被重復使用的情況。 (細胞被重復使用,這就是它出現在底部的原因)。

您應該確保處理重用案例(換句話說,在重用時相應地配置您的單元格)

我找到了你的問題。 但這有點難以說。

試着考慮一下,當表視圖要在一行中呈現一個單元格時,它將在重用隊列中選擇一個與將要顯示的單元格相同的單元格。 換句話說,表視圖只知道它選擇的重用單元的類型,並且單元的注釋被哪一行識別! 那么看看你的代碼吧。

您可以知道,只有零時,您才會使用您的文本創建一個單元格。 然而,當它將被使用時,具有相同類型的單元格不知道他在哪一行,因此它不知道它持有什么樣的注釋並且只是呈現其最后的infromations !!

這是解決方案。 嘗試將您的信息保存在一個組織良好的數組中(例如:您可以通過其中的哪一行返回您的信息),基本在其行上,嘗試更新您的單元格的注釋,其中哪一行與info-array相關。

希望它可以幫助:)

暫無
暫無

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

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