2014-12-28 29 views
-1

如何创建tableView单元格以便显示在tableView的顶部在方法cellForRowAtIndexPath? 我想显示优先单元格在tableView单元格的顶部,检查它们是否有优先级,取决于havePriority属性的bool的值,例如,如果havePriority为1,则应该是true,否则应该在优先级列表之后。 我尝试过使用不同的单元格标识符,但无法获得顶部的列表。确定表格视图单元格的优先级IOS

我的代码:

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; 
+0

优先考虑您的数据源,然后重新加载数据。 –

回答

0

你不应该设置该方法中的细胞的顺序。那时已经太晚了。

这实在是应该在模型层中完成的事情。要显示的项目列表应该已经根据其优先级进行排序,并且这样他们将显示在顶部。

其次,这种类型的代码是不是真的有必要了:

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

您应该使用更现代的方法:

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

这无论是从重用池中返回一个细胞或创建一个新的,所以它总是返回一个单元格,并且不需要自己创建一个单元格。

+0

它给出了这个错误:无法使用标识符出列单元 - 必须为标识符注册一个笔尖或类,或者在故事板中连接一个原型单元格 – developer

+0

这是您应该做的。或者使用storyboard或xib文件中的单元格标识符,或者使用下面的方法:“registerNib:forCellReuseIdentifier:'或' registerClass:forCellReuseIdentifier:' – Abizern

+0

我已经在storyborad中声明过了,它可以解释多一点古代与现代的区别是什么? – developer

相关问题