Objective c uitableview在滚动时创建单元格

Objective c uitableview在滚动时创建单元格,objective-c,ios5,ios4,uitableview,Objective C,Ios5,Ios4,Uitableview,我正在使用uitableview,我在cellforrow委托中遇到了一个问题,我使用了此代码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"mycell"; CustomCell *cell = (CustomCell *)[tableView dequeueR

我正在使用uitableview,我在cellforrow委托中遇到了一个问题,我使用了此代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"mycell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    cell =  (CustomCell *)[topLevelObjects objectAtIndex:2];
    cell.btn_Selected.hidden=YES;
    cell.btn_Selected.tag=indexPath.row+1;

    [array_btnContainer addObject:cell.btn_Selected];

}
我的问题是,当我运行应用程序时,它会加载我的表视图并创建单元格,但当我滚动我的表视图时,它会再创建一个单元格。为什么????
它必须重用已经创建的单元格,不能进入(cell==nil)块,但当我滚动时,它会创建一个单元格并重用另一个单元格,为什么????我卡在CustomCell.m中

,您必须按如下方式重写以下方法

-(NSString*) reuseIdentifier{
       return @"mycell";
 }

这可以通过以下步骤实现

  • 在.h文件中创建CustomCell的引用,您将使用该引用来显示tableview,不管它叫什么ShowTableView.h。 IbCell*单元

  • 转到CustomCell.xib并选择文件所有者,然后将类属性设置为ShowTableView

  • 将单元格引用附加到CustomCell

  • 选择CustomCell,然后使用mycell设置属性标识符值

  • 现在转到ShowTableView.m文件和cellForRowAtIndexPath方法,放置以下代码:

     -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:      (NSIndexPath *)indexPath
    {
    
    static NSString *kCellIdentifier = @"mycell";
    cell =(CustomCell *)[tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
    if (!cell)
    {
    [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];      
    }  
    }
    

  • 现在它将重用您以前的单元格

    是否可以在滚动后停止创建更多单元格并强制使用旧单元格?您正在创建多少单元格?请告诉我,有没有什么方法可以让我先在自己的函数中创建单元格,然后在cellforrow方法中使用它?当应用程序启动时,有多少行是可见的,当你滚动时,有多少行离开屏幕?你能告诉我们实际的问题吗?