【问题标题】:Why we are checking if (cell == nil) in UITableViewController?为什么我们在 UITableViewController 中检查 if (cell == nil)?
【发布时间】:2012-03-30 20:53:49
【问题描述】:

我正在尝试实现基于 UITableView 的应用程序。为此,我选择 UITableViewStyle 是 Group。在我的 TableView 中,它们是 15 部分,每个部分有 1 行。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 15;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==12) 
    {
        return 120;
    }
    else
    {
        return 60;
    }

}

我想在 12

部分添加一个 UITextView

为此我做了以下代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

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



    }
    if ([indexPath section] == 12) 
    {
        if([indexPath row]==0)
        {
            descriptionTextField=[[UITextView alloc] initWithFrame:CGRectMake(5, 8, 290, 106)];
            descriptionTextField.font = [UIFont systemFontOfSize:15.0];   
            descriptionTextField.backgroundColor=[UIColor scrollViewTexturedBackgroundColor];

            [descriptionTextField setDelegate:self];
            [descriptionTextField setTag:2];
            [descriptionTextField setText:@"Enter Location Description."];

            descriptionTextField.keyboardType=UIKeyboardTypeDefault;
            descriptionTextField.returnKeyType=UIReturnKeyNext;


            descriptionTextField.textColor=[UIColor blackColor];
            descriptionTextField.editable=YES;
            descriptionTextField.autocapitalizationType=UITextAutocapitalizationTypeWords;
            descriptionTextField.autocorrectionType=UITextAutocorrectionTypeDefault;
            descriptionTextField.textAlignment=UITextAlignmentLeft;

            UIToolbar*  keboardToolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 32)];

            UIBarButtonItem *extra=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
            UIBarButtonItem *Done=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(keyboardDoneButtonActin:)];
            [Done setWidth:65.0f];
            [keboardToolBar setItems:[[[NSArray alloc]initWithObjects:extra,Done, nil]autorelease] ];
            [extra release];
            [Done release];

            [keboardToolBar setTintColor:[UIColor blackColor]];
            [keboardToolBar setAlpha:.70];
            [descriptionTextField setInputAccessoryView:keboardToolBar];
            [descriptionTextField setTag:101];
            [cell.contentView addSubview:descriptionTextField];
            [descriptionTextField release];
        }
    }

    return cell;
}

在初始阶段,表格视图是这样的

如果我上下滚动表格视图,那么 uitextView 部分发生了变化,它将显示多个位置。

我无法理解我的错,为什么会这样?

如果我在 if(cell==nil) 中实现上述代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

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

    if ([indexPath section] == 12) 
    {
        if([indexPath row]==0)
        {
           **/* implemention  all code here*/**

             [cell.contentView addSubview:descriptionTextField];
            [descriptionTextField release];
        }
    }

    return cell;

}

UITextView 不显示,我认为它没有分配。

那么在实现的代码之间有什么区别 如果(单元格 == 零) { 里面 }

如果(单元格 == 无) { } 外面

【问题讨论】:

  • 唯一的区别是,当你在 cell==nil 中编写代码时,它只会在开始时工作一次,如果你写出来,它会在每次视图加载时创建
  • 如果滚动 tableview ,为什么部分值会改变。如果我在外面写代码?
  • 您可以在代码中做的另一件事为 textview 设置一个标记值,然后编写此代码 [[cell.contentView viewWithTag:100+indexPath.row] removeFromSuperview]; 100+index.row 为标签值
  • 因为它再次使用相同的单元格视图,并且您不需要初始化表格视图中的所有单元格。可见的单元格被初始化。您的记忆也将受到控制@Mustafa
  • 如何解决这个问题

标签: objective-c ios uitableview cell


【解决方案1】:
NSString *CellIdentifier = [NSString stringWithFormat:@"%i",indexPath.row];

// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

这个可以代替

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

这是在单元格中编写的简单示例 ==nill

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

static NSString *CellIdentifier = @"CellIdentifier";
// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
cell= nil;
if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    [[cell.contentView viewWithTag:100+indexPath.row] removeFromSuperview]; 

    UIView *selectview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 30)];
    [selectview setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"strip_s12A-1_h.png"]]];
    cell.selectedBackgroundView = selectview;
    [selectview release];

    UILabel *cellTitle=[[UILabel alloc]initWithFrame:CGRectMake(40, 0, 300, 30)];
    //cellTitle.adjustsFontSizeToFitWidth=YES;
    [cellTitle setBackgroundColor:[UIColor clearColor]];
    [cellTitle setFont:[UIFont fontWithName:@"Arial Rounded MT Bold" size:17]];
    [cellTitle setTextColor:[UIColor blackColor]];
    cellTitle.tag = 100+indexPath.row;

        cellTitle.text= [[[cellArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row] valueForKey:@"Des2"];

    [cell.contentView addSubview:cellTitle];

    [cellTitle release];
}
return cell;
     }

我觉得就够了

【讨论】:

  • 是否可以在 if(cell==nil) 里面创建 UITextView
  • 如果我们滚动表格视图,我不明白为什么部分值会发生变化。
  • 如果我遇到问题,我可以连接到你吗?因为,我现在想离开。我想再检查一下。
  • 在 UITabBarController 中的 tabBarItem 之一是带有 RootViewController(UITableVIewController) 的 UINavigationController。tableView 的滚动属性已启用。当为编辑目的选择一个单元格时,tableview 不会滚动。键盘将隐藏单元格。在正常情况下(没有 UITabBarController)它工作正常。此错误与 UITababrController 有关吗?
【解决方案2】:

测试if (cell == nil) 处理没有可重复使用的单元格出列的情况,在这种情况下,您必须创建一个新单元格。创建新单元格时,您负责构建其视图层次结构。

【讨论】:

  • @MusthafaPP 单元被回收,而不是实例化新单元。原因:性能、速度。大多数情况下,在OOP 中,我们会根据需要创建新实例(对象)。但是实例化一个新对象有一些开销,需要一些时间。在极少数情况下,例如 UITableView,我们需要最大限度的性能。回收现有对象,同时重置其适合新使用的关键属性/变量,可以节省足够的时间来保证麻烦。因此,如果您有可用的单元格,请重新使用它。否则,创建一个新实例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-19
  • 1970-01-01
相关资源
最近更新 更多