2014-02-14 73 views
0

一節中的默認文本我在TableView中有兩個部分,有它們各自的sectionHeaders。 numberOfRowsInSection是動態計數的&它可能也會出現爲0.所以我想在0行的情況下在該部分的某處顯示默認文本。 我該怎麼做? (使用iOS 6,XCode-4.2)如果numberOfRowsInSection返回計數0

+0

正是你在哪裏想顯示默認的文本? – Nick

+0

而不是加載自定義單元格,我想顯示文本。現在我的問題是,儘管numberOfRowsInSection函數返回零;我得到一個空白的定製單元格加載。請幫忙。 – MixCoded

回答

1

爲什麼不在「空白部分」的單元格中顯示默認文本? 而不是返回0行返回1並將默認文本放在那裏。它可以是這樣的:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Verify is the section should be empty or not 
    if(emptySection == NO) { 
     return numberOfRowsInSection; 
    } 
    else { 
     return 1; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"Cell Identifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if(!cell) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    if(emptySection && indexPath.row == 0) { 
     cell.textLabel.text = @"This is the default text"; 
    } 
    else { 
     // Display the normal data 
    }  

    return cell; 
} 

更新

輕敲包含默認文本的單元格時,下面的代碼將避免任何行動。

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(emptySection) { 
     return; 
    } 

    // Perform desired action here 
} 

另一種解決方案是完全防止細胞選自:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)path 
{ 
    if(emptySection) { 
     retur nil; 
    } 

    return path; 
} 
+0

這是我嘗試過的一個選項,但是我的問題在於如果我加載一個單元格,它可以通過segue重定向到一個新的ViewController,並且需要將一些數據傳遞給該ViewController。所以它不會工作。無論如何,我找到了解決方案。我只是返回零行數,並更改了節標題!不管怎麼說,多謝拉。 – MixCoded

+0

看看更新 –

+0

非常感謝... – MixCoded