Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.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
Ios 对表视图单元格进行优先级排序_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 对表视图单元格进行优先级排序

Ios 对表视图单元格进行优先级排序,ios,objective-c,uitableview,Ios,Objective C,Uitableview,如何对tableView单元格进行优先级设置,以便在MethodcellForRowAtIndexPath中的tableView顶部显示? 我想在tableView单元格顶部显示优先级单元格,检查它们是否具有优先级,这取决于havePriority属性的bool值,这样,如果havePriority为1,则应为真,否则应在优先级列表之后。 我尝试使用不同的单元格标识符,但无法获得顶部的列表 我的代码: static NSString *CellIdentifier = @"Cell";

如何对tableView单元格进行优先级设置,以便在Method
cellForRowAtIndexPath
中的tableView顶部显示? 我想在tableView单元格顶部显示优先级单元格,检查它们是否具有优先级,这取决于
havePriority
属性的
bool
值,这样,如果
havePriority
为1,则应为真,否则应在优先级列表之后。 我尝试使用不同的单元格标识符,但无法获得顶部的列表

我的代码:

static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
 if (havePriority) // if the cell wants priority to be at top, have a value in bool
    {
        cell.textLabel = sOmeText; // 
        NSLog(@"%@",sOmeText);
    }
    return cell;

您不应该在该方法中设置单元格的顺序。到那时已经太晚了

这确实是应该在模型层中完成的事情。要显示的项目列表应该已经按照它们的优先级进行了排序,这样它们就会显示在顶部

其次,这种类型的代码实际上不再必要:

 UITableViewCell *cell = [self.tableView  dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
您应该使用更现代的方法:

 UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier 
                                                              forIndexPath:indexPath];

这将从重用池返回一个单元格或创建一个新的单元格,因此它总是返回一个单元格,而无需自己创建一个。

为数据源排定优先级,然后重新加载数据。它会出现以下错误:无法将标识符为cell的单元格出列-必须为标识符注册nib或类,或者在情节提要中连接原型单元格,这是您应该做的。要么使用故事板或xib文件中的单元标识符,要么使用方法`Registernb:forCellReuseIdentifier:`或`registerClass:forCellReuseIdentifier:`我已经在storyborad中声明并完成了,你能再解释一下旧方法和现代方法的区别吗?它在文档中: