簡體   English   中英

滾動UITableView時UITableViewCell的位置更改

[英]UITableViewCell position change while scroll UITableView

滾動UITableView我的UITableViewCell位置發生了變化。 在這里,我有更多的UITableViewCell 因此它涵蓋了不止一頁的屏幕。 當我從上到下滾動時, UITableViewCell的位置會發生變化,例如第一個單元格排在第五位。 第二單元到達第一單元,依此類推。

我的代碼:

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

    static NSString *CellIdentifier = @"SignUpWithEmailCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    switch (indexPath.row) {

        case 0: {

            UILabel *lblFirstName = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
            lblFirstName.text = @"First Name";
            [cell.contentView addSubview:lblFirstName];

            self.txtFirstName = [[UITextField alloc]initWithFrame:CGRectMake(lblFirstName.frame.origin.x + lblFirstName.frame.size.width + 5, 0, 190, 40)];
            self.txtFirstName.delegate = self;
            self.txtFirstName.text = strFirstName;
            self.txtFirstName.tag = 0;
            self.txtFirstName.userInteractionEnabled = YES;
            self.txtFirstName.font = [UIFont systemFontOfSize:15.0];
            self.txtFirstName.clearButtonMode = UITextFieldViewModeWhileEditing;
            self.txtFirstName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
            [cell.contentView addSubview:self.txtFirstName];
            break;
        } case 1: {

            UILabel *lblLastName = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
            lblLastName.text = @"Last Name";
            [cell.contentView addSubview:lblLastName];

            self.txtLastName = [[UITextField alloc]initWithFrame:CGRectMake(lblLastName.frame.origin.x + lblLastName.frame.size.width + 5, 0, 190, 40)];
            self.txtLastName.backgroundColor = [UIColor clearColor];
            self.txtLastName.delegate = self;
            self.txtLastName.tag = 1;
            self.txtLastName.text = strLastName;
            self.txtFirstName.userInteractionEnabled = YES;
            self.txtLastName.returnKeyType = UIReturnKeyDone;
            self.txtLastName.clearButtonMode = UITextFieldViewModeWhileEditing;
            self.txtLastName.font = [UIFont systemFontOfSize:15.0];
            self.txtLastName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
            [cell.contentView addSubview:self.txtLastName];
            break;
        }
        default:
            break;
    }
}

 return cell;
}

誰能指導我正確存檔。

提前致謝。

即使單元格不是nil,也必須設置單元格的視覺元素。 如果閱讀文檔,您會注意到Apple表示必須始終使用此方法更新單元格的內容。 原因是因為檢查nil只是測試是否必須創建新的單元格。 如果不需要創建新的單元格,則意味着您正在重用一個單元格,因此,該單元格很可能會顯示您想要的行以外的內容。 因此,您應該每次使用此方法重置內容。

從此文件

重新使用單元格時,tableView:cellForRowAtIndexPath:的表視圖的數據源實現應始終重置所有內容。

這里有一些代碼可以幫助您入門……但是由於要在此方法中向單元格添加子視圖,因此需要對其進行改進。 只有在單元格為零時才應該發生這種情況,您應該存儲對這些子視圖的引用,以便可以在其他時間訪問它們。

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

    static NSString *CellIdentifier = @"SignUpWithEmailCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    switch (indexPath.row) {

        case 0: {

            UILabel *lblFirstName = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
            lblFirstName.text = @"First Name";
            [cell.contentView addSubview:lblFirstName]; // TODO: MUST BE MOVED

            self.txtFirstName = [[UITextField alloc]initWithFrame:CGRectMake(lblFirstName.frame.origin.x + lblFirstName.frame.size.width + 5, 0, 190, 40)];
            self.txtFirstName.delegate = self;
            self.txtFirstName.text = strFirstName;
            self.txtFirstName.tag = 0;
            self.txtFirstName.userInteractionEnabled = YES;
            self.txtFirstName.font = [UIFont systemFontOfSize:15.0];
            self.txtFirstName.clearButtonMode = UITextFieldViewModeWhileEditing;
            self.txtFirstName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
            [cell.contentView addSubview:self.txtFirstName]; // TODO: MUST BE MOVED
            break;
        } case 1: {

            UILabel *lblLastName = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
            lblLastName.text = @"Last Name";
            [cell.contentView addSubview:lblLastName]; // TODO: MUST BE MOVED

            self.txtLastName = [[UITextField alloc]initWithFrame:CGRectMake(lblLastName.frame.origin.x + lblLastName.frame.size.width + 5, 0, 190, 40)];
            self.txtLastName.backgroundColor = [UIColor clearColor];
            self.txtLastName.delegate = self;
            self.txtLastName.tag = 1;
            self.txtLastName.text = strLastName;
            self.txtFirstName.userInteractionEnabled = YES;
            self.txtLastName.returnKeyType = UIReturnKeyDone;
            self.txtLastName.clearButtonMode = UITextFieldViewModeWhileEditing;
            self.txtLastName.font = [UIFont systemFontOfSize:15.0];
            self.txtLastName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
            [cell.contentView addSubview:self.txtLastName]; // TODO: MUST BE MOVED
            break;
        }
        default:
            break;
    }


 return cell;
}

這是您的問題, if (cell == nil)第一次使該單元格為零,那么我將創建一個單元格,那么我會嘗試重用de cell單元格為非null,因此我像以前一樣添加此單元格,而沒有進行任何更改,為了解決您的問題,您必須刪除if和for no donage更好的方法是創建一個包含所有對象的自定義單元格。

為了優化內存,iOS會初始化並僅使用屏幕顯示的單元格。 滾動查看下一個內容時,它將使不可見的單元出隊,並將其用於新的可見單元。 因此,第一個單元格用於第8行,依此類推。 現在,在您的代碼中,當此出隊操作發生時,您無需重置內容。 您所有的單元格內容代碼僅在單元格不在時才存在。 從您的IF條件中取出單元初始化代碼,您應該一切順利。

暫無
暫無

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

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