【问题标题】:uitableview cell creation on scrolling滚动时创建 uitableview 单元格
【发布时间】:2012-09-09 14:21:37
【问题描述】:

我正在使用 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) 块但是当我滚动它时它会创建一个单元格并重用另一个单元格为什么????我被卡住了

【问题讨论】:

  • 应用启动时有多少行可见,滚动时有多少行离开屏幕?
  • 你能告诉我们这个问题吗?

标签: objective-c ios5 ios4 uitableview


【解决方案1】:

这可以通过以下步骤来实现

  1. 在 .h 文件中创建 CustomCell 的引用,您可以使用该文件来显示 tableview,无论它是什么,都可以将其称为 ShowTableView.h。 IBOutlet CustomCell *cell;

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

  3. 使用 CustomCell 附加单元格引用

  4. 选择 CustomCell,然后使用 mycell 设置属性 Identifier 值

  5. 现在转到您的 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];      
    }  
    }
    

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

【讨论】:

    【解决方案2】:

    在 CustomCell.m 中,您必须重写以下方法,如下所示。

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

    【讨论】:

    • 是否有可能在滚动后停止创建更多单元格并强制使用旧单元格
    • 您要创建多少个单元格?即一节中的行数
    • 你们好,请告诉我有什么方法可以让我先在自己的函数中创建单元格,然后在 cellforrow 方法中使用它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多