簡體   English   中英

如何將 UITableView 滾動到特定位置

[英]How to scroll UITableView to specific position

如何將表格的單元格滾動到特定位置? 我有一個顯示 3 行(根據高度)的表格。 我想要的是,如果我點擊第一行而不是根據表格的高度,第一行應該滾動並獲得新的位置(中心),其他行也是如此。 我試過 contenOffset 但沒有用..

編輯:

簡而言之,當我們在選擇器中選擇任何行時,就像數據選擇器一樣,該行會滾動到中心。

謝謝..

它應該使用- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated使用它:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[yourTableView scrollToRowAtIndexPath:indexPath 
                     atScrollPosition:UITableViewScrollPositionTop 
                             animated:YES];

atScrollPosition可以采用以下任何值:

typedef enum {
UITableViewScrollPositionNone,
UITableViewScrollPositionTop,
UITableViewScrollPositionMiddle,
UITableViewScrollPositionBottom
} UITableViewScrollPosition;

我希望這可以幫助你

干杯

[tableview scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];

這會將您的表格視圖帶到第一行。

最后我發現......當表格只顯示 3 行時它會很好用......如果行更多,應該相應地改變......

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{    
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section
{  
    return 30;
}

- (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] autorelease];
    }

    // Configure the cell.
    cell.textLabel.text =[NSString stringWithFormat:@"Hello roe no. %d",[indexPath row]];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * theCell = (UITableViewCell *)[tableView     
                                              cellForRowAtIndexPath:indexPath];

    CGPoint tableViewCenter = [tableView contentOffset];
    tableViewCenter.y += myTable.frame.size.height/2;

    [tableView setContentOffset:CGPointMake(0,theCell.center.y-65) animated:YES];
    [tableView reloadData]; 
 }

使用[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:scrollPosition animated:YES]; 滾動接收器,直到由索引路徑標識的行位於屏幕上的特定位置。

scrollToNearestSelectedRowAtScrollPosition:animated:

滾動表格視圖,以便在表格視圖中最接近指定 position 的選定行位於該 position 處。

Swift 4.2版本:

let indexPath:IndexPath = IndexPath(row: 0, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .none, animated: true)

枚舉:這些是可用的 tableView 滾動位置 - 此處供參考。 您無需在代碼中包含此部分。

public enum UITableViewScrollPosition : Int {

case None
case Top
case Middle
case Bottom
}

選擇行:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let theCell:UITableViewCell? = tableView.cellForRowAtIndexPath(indexPath)

    if let theCell = theCell {
        var tableViewCenter:CGPoint = tableView.contentOffset
        tableViewCenter.y += tableView.frame.size.height/2

        tableView.contentOffset = CGPointMake(0, theCell.center.y-65)
        tableView.reloadData()
    }

}

值得注意的是,如果你使用setContentOffset方式,可能會導致你的表格視圖/集合視圖有點跳躍。 老實說,我會以另一種方式嘗試 go。 建議使用免費提供的滾動視圖委托方法。

只需一行代碼:

self.tblViewMessages.scrollToRow(at: IndexPath.init(row: arrayChat.count-1, section: 0), at: .bottom, animated: isAnimeted)

如果您的表視圖只有一行和一個部分,您也可以使用以下方法

 challengeInfoTableView.scrollRectToVisible(challengeInfoCell.challengeDescriptionTextView!.frame, animated: false)

在這種情況下,您可以直接滾動到正確的文本字段、文本視圖、標簽等。

暫無
暫無

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

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