2012-05-27 30 views
31

我希望我的tableView能顯示6行文本,本例中爲「Example」。據我所知,我有我的numberOfSectionsInTableView:numberOfRowsInSection:正確設置。請參閱下面的示例代碼:UITableView顯示的行數多於numberOfRowsInSection中指定的行數:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    // Return the number of rows in the section. 
    return 6; 
} 

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    cell.textLabel.text = @"Example"; 

    return cell; 
} 

問題是當您看到下面的圖像顯示行不應該/不存在的行時。

enter image description here

如何擺脫過去顯示6行的行嗎?

+3

這是比需要滾動更少的行所有普通表視圖的默認行爲,所以只需設置行數和部分的數量將無濟於事。你需要找到另一種方式... – BoltClock

回答

55

這樣做的generally accepted way是添加一個頁腳視圖與CGRectZero的幀大小,因爲這樣的:

[tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]] 

這樣做是告訴表格有一個頁腳,所以它停止顯示分隔線。但是,由於頁腳有一個CGRectZero作爲框架,所以沒有顯示任何內容,所以視覺效果就是分隔符停止。

+0

如何擺脫多餘的細胞?我不想向他們展示 - 我寧願顯示部分標題中顯示的灰色背景。 –

0

你可以做線沿線的東西:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:7 inSection:0]; 
[self.mytableView cellForRowAtIndexPath:indexPath].hidden = YES; 

我肯定有一些更好的方法,但是這是浮現在腦海的第一件事。

+0

原諒我,如果這是一個愚蠢的問題,但你在哪裏建議我把這個代碼? (在什麼方法裏面?) – tarheel

+0

似乎適合的任何地方 –

0

如果您指的是顯示在最後一行下面的淺灰線,那麼這只是UITableView繪製行分隔符的默認方式。

您可以嘗試更改Interface Builder中的分隔符樣式(請參見下面的圖像),以查看其中的一個可能更符合您的喜好。

enter image description here enter image description here

+0

感謝您的建議,但我希望那裏的分隔符存在的行,但沒有其他顯示。 – tarheel

0

你沒有說你想看過去的最後一行。如果你只是想看到窗口背景,那麼只需將你的表格視圖嵌入一個UIView中,該UIView的高度足以顯示你想要查看的行數。如果你想看到更多的行而不滾動,那麼你將不得不根據行數調整包含視圖的大小。

7

這是由於您的表視圖高度。你有寫的天氣

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

//返回段中的行數。 return 6; }

但它的顯示行根據Table-view Size。如果你不想顯示這個額外的線條,那麼請將UITableView樣式平鋪爲分組。

+0

對於UITableViewStyleGrouped,UITableViewStyleGrouped –

+0

-1爲+1。這影響了整個表格的顯示,而不是詢問者詢問的內容。刪除分隔符是改變樣式的副作用。如果在更高版本中將分組樣式更改爲包含其他分隔符,則此樣式更改是沒有意義的。 – Tim

+0

不錯的一個。好,簡單,快速修復! –

-1

以編程方式刪除它,使用: [yourTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

+0

這將刪除所有單元格之間的分隔線 –

5

短期和簡單的答案..

self.tableView.tableFooterView = [UIView new]; 
20

斯威夫特版本

最簡單的方法是設置tableFooterView屬性:

override func viewDidLoad() { 
    super.viewDidLoad() 
    // This will remove extra separators from tableview 
    self.tableView.tableFooterView = UIView(frame: CGRect.zero) 
} 
+2

Swift 3:'= UIView(frame:CGRect.zero)' – fbynite

相關問題