2014-08-29 63 views
-1

我正在創建一個食譜應用程序,並在我的表視圖的節方法中的行數中遇到了此語義問題。這是我第一次真正與桌面視圖合作,我想知道如果有人能夠看到我做錯了什麼,並指出我在正確的方向。謝謝!語義問題控制可能會達到非無效函數的末尾

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    // Return the number of rows in the section. 
    if (menuInt == 0) 
     return [soupsArray count]; 
    if (menuInt == 1) 
     return [saladsArray count]; 
    if (menuInt == 2) 
     return [appetizersArray count]; 
    if (menuInt == 3) 
     return [entreeArray count]; 
    if (menuInt == 4) 
     return [dissertsArray count]; 

    [self.tableView reloadData]; 
} 
+3

你不應該調用從numberOfRowsInSection中重新加載數據。 – 2014-08-29 17:56:48

回答

3

如果所有條件都失敗會發生什麼?你需要確保你至少能夠返回一個NSInteger,即使你確定其中的一個條件肯定會成功。就是這樣。

此外,正如Martin R指出的那樣,您不應該在函數中使用reloadData。

0

嘗試這2個解決方案之一(重載數據必須return語句之前也發生)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
int counter = 0; 
// Return the number of rows in the section. 
if (menuInt == 0) 
    counter = [soupsArray count]; 
if (menuInt == 1) 
    counter = [saladsArray count]; 
if (menuInt == 2) 
    counter = [appetizersArray count]; 
if (menuInt == 3) 
    counter = [entreeArray count]; 
if (menuInt == 4) 
    counter = [dissertsArray count]; 

[self.tableView reloadData]; 
return counter; 

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

// Return the number of rows in the section. 
if (menuInt == 0) { 
    [self.tableView reloadData]; 
    return [soupsArray count]; 
} 
else if (menuInt == 1) { 
[self.tableView reloadData]; 
    return [saladsArray count]; 
} 
else if (menuInt == 2) { 
    [self.tableView reloadData]; 
    return [appetizersArray count]; 
} 
else if (menuInt == 3) { 
    [self.tableView reloadData]; 
    return [entreeArray count]; 
} 
else if (menuInt == 4) { 
    [self.tableView reloadData]; 
    return [dissertsArray count]; 
else { 
    [self.tableview reloadData]; 
    return 0; 
} 

}

+1

如果所有'if'失敗,第一個解決方案將返回未初始化的內存。 'int counter = 0;'會修復它並使其等同於您的第二個解決方案。 – 2014-08-30 03:05:09

相關問題