ios - UITableView: `tableView:cellForRowAtIndexPath:` 没有为所有行调用

标签 ios iphone uitableview iphone-sdk-3.0

我创建了一个包含 4 个部分和 3 行的 UITableView,总共 12 行。但

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

上面的方法只被调用了 9 次而不是 12 次。为什么会这样?
我的第 4 部分没有构建,但我的第 1 部分被复制为第 4 部分。

请引用我下面的代码
@interface MainViewController : UITableViewController<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>
 {
}
@end


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 
{
  CGRect frameRect = CGRectMake(0,0,320,460);

  UITableView *tableView = [[UITableView alloc] initWithFrame:frameRect   
  style:UITableViewStyleGrouped];
  tableView.delegate = self;
  tableView.dataSource = self;  
  tableView.backgroundColor = [UIColor purpleColor];
  tableView.scrollEnabled = YES;

  self.view = tableView;
  [tableView release];

  [super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 3;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 4;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
     NSLog(@"CELL IS NIL %i", indexPath.section);

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

      if (indexPath.section == 0)
      {
          if(indexPath.row == 0)
          {
              cell.text = @"Tmail";

              UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
              aField.placeholder = @"Mandatory";
              aField.delegate = self;
              aField.textColor = [UIColor blackColor];
              [cell addSubview:aField];
              [aField release];
          }
          else if ( indexPath.row == 1 )
          {
              cell.text = @"English";
              UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
              aField.placeholder = @"Mandatory";
              aField.delegate = self;
              aField.textColor = [UIColor blackColor];
              [cell addSubview:aField];
              [aField release];
          }
          else
          {
              cell.text = @"Hindi";
              UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
              aField.placeholder = @"Mandatory";
              aField.delegate = self;
              aField.textColor = [UIColor blackColor];
              [cell addSubview:aField];
              [aField release];
          }
      }
      else if (indexPath.section == 1)
      {
          if(indexPath.row == 0)
          {
              cell.text = @"Street";

              UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
              aField.placeholder = @"Mandatory";
              aField.delegate = self;
              aField.textColor = [UIColor blackColor];
              [cell addSubview:aField];
              [aField release];
          }
          else if ( indexPath.row == 1 )
          {
              cell.text = @"City";
              UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
              aField.placeholder = @"Mandatory";
              aField.delegate = self;
              aField.textColor = [UIColor blackColor];
              [cell addSubview:aField];
              [aField release];
          }
          else
          {
              cell.text = @"State";
              UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
              aField.placeholder = @"Mandatory";
              aField.delegate = self;
              aField.textColor = [UIColor blackColor];
              [cell addSubview:aField];
              [aField release];
          }
      }
      else if (indexPath.section == 2)
      {
          if(indexPath.row == 0)
          {
              cell.text = @"Salem";

              UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
              aField.placeholder = @"Mandatory";
              aField.delegate = self;
              aField.textColor = [UIColor blackColor];
              [cell addSubview:aField];
              [aField release];
          }
          else if ( indexPath.row == 1 )
          {
              cell.text = @"Samalpatti";
              UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
              aField.placeholder = @"Mandatory";
              aField.delegate = self;
              aField.textColor = [UIColor blackColor];
              [cell addSubview:aField];
              [aField release];
          }
          else
          {
              cell.text = @"Chennai";
              UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
              aField.placeholder = @"Mandatory";
              aField.delegate = self;
              aField.textColor = [UIColor blackColor];
              [cell addSubview:aField];
              [aField release];
          }
      }

      else if (indexPath.section == 3)
      {
          if(indexPath.row == 0)
          {
              cell.text = @"NOKIA";
              UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
              aField.placeholder = @"Mandatory";
              aField.delegate = self;
              aField.textColor = [UIColor blackColor];
              [cell addSubview:aField];
              [aField release];
          }
          else if ( indexPath.row == 1)
          {
              cell.text = @"SAMSUNG";
              UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
              aField.placeholder = @"Mandatory";
              aField.delegate = self;
              aField.textColor = [UIColor blackColor];
              [cell addSubview:aField];
              [aField release];
          }
          else
          {
              cell.text = @"SONY";
              UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
              aField.placeholder = @"Mandatory";
              aField.delegate = self;
              aField.textColor = [UIColor blackColor];
              [cell addSubview:aField];
              [aField release];
          }
      }
   }

  return cell;
}

enter image description here

最佳答案

实际上,当您滚动时,会创建其他行以防万一您有更多行,因此认为您需要像这样编写此代码,

编辑了一个

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
         NSLog(@"CELL IS NIL %i", indexPath.section);

      static NSString *CellIdentifier = @"Cell";
if (cell == nil) 
    {

        CGRect CellFrame = CGRectMake(0, 0, 300, 70);
        cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:CellIdentifier] autorelease];
        CGRect labLabelFrame = CGRectMake(5, 5, 250, 40);
        UILabel *lblTemp;

        lblTemp = [[UILabel alloc] initWithFrame:labLabelFrame];
        lblTemp.tag = 1;
        [cell.contentView addSubview:lblTemp];
        [lblTemp release];

        CGRect textFieldFrame= CGRectMake(200, 5, 120, 30);

        UITextField *txt=[[UITextField alloc] initWithFrame:textFieldFrame];
        text.tag=2;
        [cell.contentView addSubview:txt];
        [txt release];

    }
     UILabel *cellLabel = (UILabel *)[cell viewWithTag:1];
      UITextField *aField=(UITextField *)[cell viewWithTag:2];

    aField.text=@"";
    aField.placeholder=@"";
if (indexPath.section == 0)
              {
                  if(indexPath.row == 0)
                  {
                      cellLabel.text = @"Tmail";


                      aField.placeholder = @"Mandatory";
                      aField.delegate = self;
                      aField.textColor = [UIColor blackColor];

                  }
                  else if ( indexPath.row == 1 )
                  {
                      cellLabel.text = @"English";

                      aField.placeholder = @"Mandatory";
                      aField.delegate = self;
                      aField.textColor = [UIColor blackColor];


                  }
                  else
                  {
                      cellLabel.text = @"Hindi";

                      aField.placeholder = @"Mandatory";
                      aField.delegate = self;
                      aField.textColor = [UIColor blackColor];


                  }
              }
              else if (indexPath.section == 1)
              {
                  if(indexPath.row == 0)
                  {
                      cellLabel.text = @"Street";


                      aField.placeholder = @"Mandatory";
                      aField.delegate = self;
                      aField.textColor = [UIColor blackColor];

                  }
                  else if ( indexPath.row == 1 )
                  {
                      cellLabel.text = @"City";

                      aField.placeholder = @"Mandatory";
                      aField.delegate = self;
                      aField.textColor = [UIColor blackColor];


                  }
                  else
                  {
                      cellLabel.text = @"State";

                      aField.placeholder = @"Mandatory";
                      aField.delegate = self;
                      aField.textColor = [UIColor blackColor];

                  }
              }
              else if (indexPath.section == 2)
              {
                  if(indexPath.row == 0)
                  {
                      cellLabel.text = @"Salem";
                      aField.placeholder = @"Mandatory";
                      aField.delegate = self;
                      aField.textColor = [UIColor blackColor];

                  }
                  else if ( indexPath.row == 1 )
                  {
                      cellLabel.text = @"Samalpatti";

                      aField.placeholder = @"Mandatory";
                      aField.delegate = self;
                      aField.textColor = [UIColor blackColor];


                  }
                  else
                  {
                      cellLabel.text = @"Chennai";

                      aField.placeholder = @"Mandatory";
                      aField.delegate = self;
                      aField.textColor = [UIColor blackColor];


                  }
              }

              else if (indexPath.section == 3)
              {
                  if(indexPath.row == 0)
                  {
                      cellLabel.text = @"NOKIA";

                      aField.placeholder = @"Mandatory";
                      aField.delegate = self;
                      aField.textColor = [UIColor blackColor];

                  }
                  else if ( indexPath.row == 1)
                  {
                      cellLabel.text = @"SAMSUNG";

                      aField.placeholder = @"Mandatory";
                      aField.delegate = self;
                      aField.textColor = [UIColor blackColor];


                  }
                  else
                  {
                      cellLabel.text = @"SONY";

                      aField.placeholder = @"Mandatory";
                      aField.delegate = self;
                      aField.textColor = [UIColor blackColor];

                  }
              }


          return cell;
        }

关于ios - UITableView: `tableView:cellForRowAtIndexPath:` 没有为所有行调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5012644/

相关文章:

ios - 如何在iOS上安全存储私有(private)文件

ios - 如何检测是否支持 HTML5 自动播放属性?

ios - UINavigationController - 弹出动画错误

iPhone HTTP Streaming .m3u8 和 .ts 文件 - 如何使用 ffmpeg 创建

ios - MvvmCross 中的 CoreAnimation 和 ObservableCollection

iphone - 子类化 UITableViewCell - backgroundView 掩盖了我在 drawRect 中所做的任何事情

ios - 我已经有一个sprite sheet 图像,我怎样才能得到cocos2D 的plist 文件?

iphone - 如何分配另一个对象指向单个对象?

ios - UITableViewCells 上的虚线边框

iphone - 在 iPhone 中使用 childBrowser 和 PhoneGap 从 Facebook 注销