Содержимое ячейки перекрывается, когда TableView Scroll - PullRequest
0 голосов
/ 23 марта 2012

Когда я прокручиваю табличное представление вверх и вниз, содержимое в ячейках перекрывается, почему это так и как я могу это исправить?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NS

IndexPath *)indexPath
    {

        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {

            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ;

        }

            UITextField* tf = nil ;

                switch ( indexPath.row ) {

                    case 0: {

                        cell.textLabel.text = @"First Name" ;

                        tf = firstNameTextField = [self makeTextField:self. placeholder:@"F name"];

                        [cell.contentView addSubview:firstNameTextField];

                        break ;

                    }

                    case 1: {

                        cell.textLabel.text = @"Last Name" ;

                        tf = lastNameTextField = [self makeTextField:self.lastName placeholder:@"L name"];

                        [cell.contentView addSubview:lastNameTextField];

                        break ;

                    }

                    case 2: {

                        cell.textLabel.text = @"age" ;

                        tf = agetextfield = [self makeTextField:self.password placeholder:@"Age"];

                        [cell.contentView addSubview:agetextfield];


                        break ;

                    }


                }
                return cell;

Ответы [ 4 ]

1 голос
/ 23 марта 2012

Вы добавляете UITextField в ячейку каждый раз, когда ячейка используется повторно.Идея состоит в том, чтобы добавить его только при создании ячейки, а затем получить существующее представление из существующей ячейки

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
  UITextField* tf = ...;
  tf.tag = kMyTextFieldTag;
  [cell.contentView addSubview:tf];
}
UITextField* tf = (UITextField*)[cell.contentView viewWithTag:kMyTextFieldTag];
tf.text = ...;

. Еще лучше идея сделать UITextField accessoryView ячейки вместодобавив его в качестве подпредставления.Если это не дает желаемого результата, рассмотрите возможность создания пользовательской ячейки вместо добавления представлений в ячейку UITableViewCellStyleValue1.

1 голос
/ 23 марта 2012

Клетки были повторно использованы в этом случае.Таким образом, клетки перекрываются на других.Просто измените свой код, чтобы решить эту проблему.

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ;
        UITextField* tf = nil ;
        for(UIView *view in [cell subViews]){

            if([view isKindOfClass:[UITextFiel class]]){

                  [view removeFromSuperView];
                 }
        }
            switch ( indexPath.row ) {

                case 0: {

                    cell.textLabel.text = @"First Name" ;

                    tf = firstNameTextField = [self makeTextField:self. placeholder:@"F name"];

                    [cell.contentView addSubview:firstNameTextField];

                    break ;

                }

                case 1: {

                    cell.textLabel.text = @"Last Name" ;

                    tf = lastNameTextField = [self makeTextField:self.lastName placeholder:@"L name"];

                    [cell.contentView addSubview:lastNameTextField];

                    break ;

                }

                case 2: {

                    cell.textLabel.text = @"age" ;

                    tf = agetextfield = [self makeTextField:self.password placeholder:@"Age"];

                    [cell.contentView addSubview:agetextfield];


                    break ;

                }


            }
    }


            return cell;
0 голосов
/ 08 мая 2012

вам нужно добавить все подпредставления (метки и т. Д.) В свой cell.contentview только один раз, это означает, что вы можете модифицировать ваши методы cellForRowAtIndexPath следующим образом:

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ;

UITextField* tf = nil ;

            switch ( indexPath.row ) {

                case 0: {

                    cell.textLabel.text = @"First Name" ;

                    tf = firstNameTextField = [self makeTextField:self. placeholder:@"F name"];

                    [cell.contentView addSubview:firstNameTextField];

                    break ;

                }

                case 1: {

                    cell.textLabel.text = @"Last Name" ;

                    tf = lastNameTextField = [self makeTextField:self.lastName placeholder:@"L name"];

                    [cell.contentView addSubview:lastNameTextField];

                    break ;

                }

                case 2: {

                    cell.textLabel.text = @"age" ;

                    tf = agetextfield = [self makeTextField:self.password placeholder:@"Age"];

                    [cell.contentView addSubview:agetextfield];


                    break ;

                }

    }





            return cell;
}

Вам не нужно добавлять и удалять подпредставления изваша ячейка каждый раз, когда ячейка перезагружается.

0 голосов
/ 23 марта 2012

Ваш код в порядке, за исключением того, что вы никогда не удаляете «старые» подпредставления из представления содержимого ячейки. Поэтому добавьте этот код перед UITextField *tF = nil;:

for(UIView *v in cell.contentView.subviews){
    [v removeFromSuperview];
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...