2017-02-13 67 views
0

我試圖讓一個UITableView呈現一個段的確定數量的行,但即使當我驗證其數據源正在爲numberOfRowsInSection返回x行時,表視圖顯示x-1。UITableView顯示與數據源相比不一致的行數

的例外意外的行爲是,如果numberOfRowsInSection小於3

我甚至放的cellForRowAtIndexPath一個斷點,我證實了它被稱爲針對未出現該行。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (section == SectionNumber_One) { 
     return 6; 
    } else { 
     return self.numberOfProjectRows; // This returns x, but x-1 rows are being shown 
    } 
} 

例如,如果self.numberOfProjectRows爲5,則第二部分只顯示4行。

如果我手動將它增加到6,它顯示5行,但應該在第5個位置的數據不在那裏。

它似乎與屏幕尺寸無關,因爲我在iPad上測試了它,結果相同。

這是怎麼發生的?有一些其他可能的修改器的行數在一節中?

我附上截圖,如果它有任何幫助。

UITableView screenshot

編輯 -這裏是我的委託方法:

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

    // Cell with reuse identifier setup 

    if (section == SectionNumber_One) { 
     // cell setup for section one that's showing up ok 
    } else if (section == SectionNumber_Two) { 
     UITextField *projectField = cell.projectTextField; 
     if ([self.userProjectKeys count] > row) { 
      projectField.text = self.availableProjects[self.userProjectKeys[row]]; 
     } 
    } 
    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Hide the password row for existing users 
    if (indexPath.row == FieldTag_Password && ![self.user.key vol_isStringEmpty]) { 
     return 0.0f; 
    } else { 
     return UITableViewAutomaticDimension; 
    } 
} 
+1

你能展示一些你的viewcontroller代碼嗎? –

+1

問題可能不在於您的數據源方法,而是您的委託方法。如果你沒有爲單元格返回正確的高度,它將不會顯示出來。 **您需要將代碼添加到您的問題的實際委託方法中**如果您需要進一步幫助,請在其中設置單元格的高度。如果您在數據源中寫入1000個單元,則無關緊要。如果你不返回高度,他們不會出現。您還應該將代碼添加到您的cellForRow方法中。 – 2017-02-14 01:05:39

+0

@Sneak非常感謝你!你釘了它。我改變了只考慮第0部分的行的高度,並忘記它也會修改第1部分。 –

回答

1

該問題可能不在您的數據源方法中,而是您的委託方方法tableView(_:heightForRowAt:)

如果您沒有爲單元返回正確的高度,它不會顯示出來。

如果在數據源中寫入1000個單元格,則無關緊要。如果你不返回高度,他們不會出現。

0

你是不是comforming MVC模式在實施表。您必須返回tableView的數據源的計數,而不是它的變量。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (section == SectionNumber_One) { 
     return 6; 
    } else { 
     return [_displayingArray count]; // <-- here 
    } 
} 

如果你想爲每個部分不同的項目,然後聲明爲他們每個人不同的數據源(即陣列)。

編輯:即使返回常量6在其他部分中也是危險的 - 您應該將項添加到另一個固定數組並在此代理中返回該數組的數量。

+0

self.numberOfProjectRows等於[self.displayingArray count] + 1,因爲我需要它顯示一個更多的空行。返回常數6僅用於測試。謝謝! –

+1

這是你誤解的地方。如果你想添加一行,那麼你必須在self.displayingArray上添加一個空項目。這是更好的方式。始終在數據源上進行更改,而不是在代表上進行更改。 – GeneCode