2013-01-25 42 views
291

我試圖用一些數據顯示一個簡單的UITableView。我希望設置UITableView的靜態高度,以便它不會在表格末尾顯示空單元格。我怎麼做?如何刪除UITableView中的空單元格?

代碼:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    NSLog(@"%d", [arr count]); 
    return [arr count]; 
} 
+0

把你的代碼,請:) – iPatel

+0

集numberOfRows正常則不會顯示空單元格 – DJB

回答

759

設置零高度表頁腳視圖(也許在你viewDidLoad法),像這樣:

斯威夫特:

tableView.tableFooterView = UIView() 

的Objective-C :

tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; 

因爲表格認爲有一個頁腳可以顯示,所以它不會顯示超出您明確要求的單元格。

接口構建器親尖端:

如果使用一個XIB /故事板,可以只需將一個UIView(具有高度0pt)到的UITableView的底部。

+0

以何種方法應該添加的代碼行? tableView willDisplayFooterView? – Jupiter869

+2

@ Jupiter869我只是把它放在ViewDidLoad中,它的工作原理 – Jacobanks

+0

謝謝,我試着用下面的代碼。但是選擇細胞後,線條會消失。所以,不要使用這個:'if(indexPath.row!= self.menuItems.count){UIImageView * line = [[UIImageView alloc] initWithFrame:CGRectMake(0,44,320,1)]; line.backgroundColor = [UIColor lightGrayColor]; [cell addSubview:line]; }' – byJeevan

11

我現在不能添加評論,所以添加這個作爲答案。

@安迪的回答是好,相同的結果可以用下面的代碼來實現:

tableView.tableFooterView = [UIView new]; 

「新」的方法屬於NSObject類和調用alloc和對UIView的初始化方法。

+0

這可以解決,但在技術上與Andy的答案不一樣。使用new只需調用「init」方法,但不能保證[myView init]和[myView initWithFrame:CGRectZero]是相同的,即使在今天的實現中,它們似乎也是如此。 – smehmood

52

在故事板中,選擇UITableView,並將屬性Style從Plain修改爲Grouped

+0

最好和最直接的答案1 – Daniel

+0

但它增加了表格部分之上和之下的額外間距。 – Andy

13

實現與SWIFT上的Xcode 6.1

self.tableView.tableFooterView = UIView(frame: CGRectZero) 
self.tableView.tableFooterView?.hidden = true 

的第二行代碼不會對演示文稿任何影響,你可以用它來檢查是否隱藏或不。從這個鏈接 Fail to hide empty cells in UITableView Swift

+0

這對我來說使用Swift&XCode 6.1!曾試過Swift相當於上述答案的版本,但沒有成功。 – Chris

6

採取

答案我試過代碼:

tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; 

viewDidLoad中部分和xcode6顯示一個警告。我已經把一個「自我」。在它前面,現在它工作正常。所以工作代碼我用的是:

self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; 
86

夫特3語法:

tableView.tableFooterView = UIView(frame: .zero) 

夫特語法:< 2。0

tableView.tableFooterView = UIView(frame: CGRect.zeroRect) 

夫特2.0語法:

tableView.tableFooterView = UIView(frame: CGRect.zero) 
+3

它現在是tableView.tableFooterView = UIView(框架:CGRect.zero) –

+0

與ios 8+測試和工作正常。 – lifeisfoo

+3

爲了防止任何人感興趣,Swift實際上讓你更簡潔:'tableView.tableFooterView = UIView(frame:.zero)' – Stuart

1
override func viewWillAppear(animated: Bool) { 
    self.tableView.tableFooterView = UIView(frame: CGRect.zeroRect) 

/// OR 

    self.tableView.tableFooterView = UIView() 
} 
1

使用UITableViewController

接受將改變TableViewCell的高度的溶液。爲了解決這個問題,執行以下步驟:

  1. 寫入代碼段在ViewDidLoad方法如下。

    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

  2. 添加以下的TableViewClass.m文件的方法。

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        return (cell height set on storyboard); 
    } 
    

就是這樣。您可以構建並運行您的項目。

在下面方法
0

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (([array count]*65) > [UIScreen mainScreen].bounds.size.height - 66) 
    { 
     Table.frame = CGRectMake(0, 66, self.view.frame.size.width, [array count]*65)); 
    } 
    else 
    { 
     Table.frame = CGRectMake(0, 66, self.view.frame.size.width, [UIScreen mainScreen].bounds.size.height - 66); 
    } 
    return [array count]; 
} 

這裏圖65是單元格的高度和66是在UIViewController中導航欄的高度。

-2

在viewDidLoad中:

self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; 
+1

這個解決方案已經在三年前被接受的答案中提供了:http://stackoverflow.com/a/14520593/603977重複它沒有增加任何價值。 –

2

,或者你可以調用的tableView方法來設置頁腳高度在1點,這將增加一個最後一行,但可以將其隱藏太多,通過設置頁腳的背景顏色。

代碼:

func tableView(tableView: UITableView,heightForFooterInSection section: Int) -> CGFloat { 
    return 1 
} 

looks like last line

相關問題