Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c UITableViewDataSource方法_Objective C_Xcode_Cocoa Touch_Methods_Protocols - Fatal编程技术网

Objective c UITableViewDataSource方法

Objective c UITableViewDataSource方法,objective-c,xcode,cocoa-touch,methods,protocols,Objective C,Xcode,Cocoa Touch,Methods,Protocols,以下是UITableViewDataSource协议的cellforrowatinexpath方法。我在一个网站上看到了这个代码 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *TableIdentifier = @"SimpleTableItem"; UITab

以下是UITableViewDataSource协议的cellforrowatinexpath方法。我在一个网站上看到了这个代码

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

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableIdentifier];

        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableIdentifier];
        }

        cell.textLabel.text = [playersReady objectAtIndex:indexPath.row];
        return cell;
}
我的问题是:

  • 为什么在这里定义
    单元格时
    写入
    =[tableView dequeueReusableCellWithIdentifier:TableIdentifier]?这意味着什么?如果我对代码进行了注释,一切都正常那是什么代码?

  • 如果
    if
    语句中的
    cell
    可以等于
    nil
    如果
    cell
    等于
    TableIdentifier
    SimpleTableItem是什么编写了这些代码?

  • 为什么
    TableIdentifier
    等于SimpleTableItem为了什么?


  • iPhone没有很多内存。但即使在现代计算机上,您也不希望为表中的每个单元格初始化一个新单元格。那只是浪费记忆。因此,苹果提出了可重复使用细胞的想法。您只需初始化填充屏幕的几个单元格(表视图)。然后,当用户向下滚动时,一些新单元格将出现在屏幕底部,但同时其他单元格将消失在屏幕顶部。所以你可以简单地把这些细胞再利用

    幸运的是,UtableView为您管理此功能。当您需要在该方法中设置一个新单元时,您所要做的就是询问表视图是否有任何可用的单元可以重用。如果存在可重用单元格,则dequeueReusableCellWithIdentifier:将返回其中一个单元格。但如果还有不可用的数据(通常在您第一次用初始单元格填充表视图时),它将返回nil。因此,您必须测试cell是否为nil,如果是这种情况,则从头创建一个新的cell


    在iOS 6.0上,有一个新的方法
    dequeueReusableCellWithIdentifier:forIndexPath:
    ,它总是返回一个有效的单元格(如果还没有可重用的单元格,它会为您创建单元格)。

    iPhone没有很多内存。但即使在现代计算机上,您也不希望为表中的每个单元格初始化一个新单元格。那只是浪费记忆。因此,苹果提出了可重复使用细胞的想法。您只需初始化填充屏幕的几个单元格(表视图)。然后,当用户向下滚动时,一些新单元格将出现在屏幕底部,但同时其他单元格将消失在屏幕顶部。所以你可以简单地把这些细胞再利用

    幸运的是,UtableView为您管理此功能。当您需要在该方法中设置一个新单元时,您所要做的就是询问表视图是否有任何可用的单元可以重用。如果存在可重用单元格,则dequeueReusableCellWithIdentifier:将返回其中一个单元格。但如果还有不可用的数据(通常在您第一次用初始单元格填充表视图时),它将返回nil。因此,您必须测试cell是否为nil,如果是这种情况,则从头创建一个新的cell


    在iOS 6.0上,有一种新方法
    dequeueReusableCellWithIdentifier:forIndexPath:
    ,它总是返回一个有效的单元格(如果还没有可重用的单元格,它会为您创建该单元格)。

    表视图只创建那些可以在屏幕上一次显示的单元格。在此之后,系统会重用单元格以节省内存

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    return 20;
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableViewL cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"Cell";
    
    
    UITableViewCell *cell = [tableViewL dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
        NSLog(@"Cell == nil so create a new cell....");
    
    }else {
         NSLog(@"Reuse Cell ");
    }
     return cell;
    
    }
    

    CellIdentifier用于标识单元格。例如,如果您在12个单元格的前10个表格上添加标签,则添加一个按钮。当您重新使用单元格时,它会给您带来一个问题。因此,我们需要为单元格上的add button创建一个不同的单元格,并为其提供一个标识符字符串。

    表格视图仅创建可在屏幕上一次性显示的单元格。之后系统重用单元以节省内存

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    return 20;
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableViewL cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"Cell";
    
    
    UITableViewCell *cell = [tableViewL dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
        NSLog(@"Cell == nil so create a new cell....");
    
    }else {
         NSLog(@"Reuse Cell ");
    }
     return cell;
    
    }
    

    CellIdentifier用于标识单元格。例如,如果在12个单元格的前10个表上添加标签,则在重复使用单元格时会出现问题。因此,我们需要为单元格上的add button创建一个不同的单元格,并为其提供一个标识符字符串。

    您可以在此处找到所有信息::您可以在此处找到所有信息::