簡體   English   中英

我的UITableView不會向下滾動數據的末尾! 為什么?

[英]My UITableView won't scroll down through the end of the data! Why?

好吧我不知道為什么這不起作用,但我已經連接了一個tableView,我想要為每個單元格設置19項文本。

單元格填充得很好,但是當我嘗試滾動時,它會向下移動,我可以看到在初始視圖中看不到的單元格,它會掛起,並且不會向下滾動。 它只是快速回復。 真的很奇怪!

我究竟做錯了什么?

- (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 19;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

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

// Configure the cell...
if(indexPath.row == 0){
    cell.textLabel.text = @"";
}else if(indexPath.row == 1){
    cell.textLabel.text = @"";
}else if(indexPath.row == 2){
    cell.textLabel.text = @"";
}else if(indexPath.row == 3){
    cell.textLabel.text = @"";
}else if(indexPath.row == 4){
    cell.indentationLevel = 2;
    cell.textLabel.text = @"";
}else if(indexPath.row == 5){
    cell.indentationLevel = 2;
    cell.textLabel.text = @"";
}else if(indexPath.row == 6){
    cell.indentationLevel = 2;      
    cell.textLabel.text = @"";
}else if(indexPath.row == 7){
    cell.indentationLevel = 2;      
    cell.textLabel.text = @"";
}else if(indexPath.row == 8){
    cell.indentationLevel = 2;      
    cell.textLabel.text = @"";
}else if(indexPath.row == 9){
    cell.textLabel.text = @"";
}else if(indexPath.row == 10){
    cell.textLabel.text = @"";
}else if(indexPath.row == 11){
    cell.textLabel.text = @"";
}else if(indexPath.row == 12){
    cell.textLabel.text = @"";
}else if(indexPath.row == 13){
    cell.textLabel.text = @"";
}else if(indexPath.row == 14){
    cell.textLabel.text = @"";
}else if(indexPath.row == 15){
    cell.textLabel.text = @"";
}else if(indexPath.row == 16){
    cell.textLabel.text = @"";
}else if(indexPath.row == 17){
    cell.textLabel.text = @"";
}else if(indexPath.row == 18){
    cell.textLabel.text = @"";
}

return cell;
}

降低 tableView的高度

我知道這是一個老問題,這不是你的問題的答案 ,而是改善你的代碼以便其他人可以受益的建議。

你可以這樣寫:

if (indexPath.row >= 4 && indexPath.row <= 8) { // 4 and 8 inclusive
    cell.indentationLevel = 2;
}
cell.textLabel.text = @"";


而不是這個:

if(indexPath.row == 0){
    cell.textLabel.text = @"";
}else if(indexPath.row == 1){
    cell.textLabel.text = @"";
}else if(indexPath.row == 2){
    cell.textLabel.text = @"";
}else if(indexPath.row == 3){
    cell.textLabel.text = @"";
}else if(indexPath.row == 4){
    cell.indentationLevel = 2;
    cell.textLabel.text = @"";
}else if(indexPath.row == 5){
    cell.indentationLevel = 2;
    cell.textLabel.text = @"";
}else if(indexPath.row == 6){
    cell.indentationLevel = 2;      
    cell.textLabel.text = @"";
}else if(indexPath.row == 7){
    cell.indentationLevel = 2;      
    cell.textLabel.text = @"";
}else if(indexPath.row == 8){
    cell.indentationLevel = 2;      
    cell.textLabel.text = @"";
}else if(indexPath.row == 9){
    cell.textLabel.text = @"";
}else if(indexPath.row == 10){
    cell.textLabel.text = @"";
}else if(indexPath.row == 11){
    cell.textLabel.text = @"";
}else if(indexPath.row == 12){
    cell.textLabel.text = @"";
}else if(indexPath.row == 13){
    cell.textLabel.text = @"";
}else if(indexPath.row == 14){
    cell.textLabel.text = @"";
}else if(indexPath.row == 15){
    cell.textLabel.text = @"";
}else if(indexPath.row == 16){
    cell.textLabel.text = @"";
}else if(indexPath.row == 17){
    cell.textLabel.text = @"";
}else if(indexPath.row == 18){
    cell.textLabel.text = @"";
}


如果您碰巧為每個案例都有不同的文本,您可以使用:

switch (indexPath.row) {
    case 0:
        cell.textLabel.text = @"";
        break;
        ...
    case 18:
        cell.textLabel.text = @"";
    default:
        break;
}

或者甚至更好地將所有內容放在NSArray (數據庫......)中,然后通過索引循環遍歷它們。

這對我有所幫助:

[self.tableView setContentInset:UIEdgeInsetsMake(0,0,65,0)];

@EmptyStack答案是對的。那是替代但不是解決方案。

即使我們可以在故事板中實現相同的目標。

從storyboard控件中選擇tableView - >單擊“添加缺失約束”。 這將為tableView和View添加約束。

這幫助我解決了這個問題。 附上screen_shot供參考。 解

您分享的代碼對我來說很好。 您是否在導航控制器堆棧上使用此tableview推送視圖控制器? 我以前見過人們這樣做過。 單元格的默認高度為44px,導航欄的高度為44px。 因此,如果您在導航控制器堆棧上按此按鈕,則滾動視圖會像您所描述的那樣快速向后捕捉。 如果是這種情況,請將TableView的高度降低44 px(在“接口”構建器中或以編程方式編寫,如果以編程方式繪制)並且應該修復它。

您需要調整UITableView的高度,或者您可以嘗試將[tableView reloadData]放在viewDidAppear方法中(如果放在viewWillAppear或viewDidLoad中,它似乎不起作用)。

在大多數情況下,前者適合我。

如果你有故事板或筆尖試試這個(注意,如果你想支持橫向,你可能需要添加一些約束)。

在此輸入圖像描述

我有同樣的問題。

檢查您看不到的行號。

在viewDidLoad之后,添加以下方法:

myTableView.contentInset = UIEdgeInsets(top: 0,left: 0,bottom: yourCellHeight * rowHeight,right: 0)

我希望我一直很有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM