2014-12-28 86 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

相關問題