Jak przewijać UITableView do określonej pozycji

Jak mogę przewijać komórkę tabeli do określonej pozycji ? Mam tabelę, która pokazuje 3 wiersze(w zależności od wysokości). to, co chcę, to jeśli kliknę na 1. wiersz niż zgodnie z wysokością tabeli 1. wiersz powinien przewijać i uzyskać nową pozycję (środek) i to samo dla innych wierszy. Próbowałem contenOffset, ale nie działa..

Edytowano:

W skrócie coś takiego jak selektor danych kiedy zaznaczamy dowolny wiersz w selektorze wiersz przewija się do środka.

Dzięki..
Author: Code cracker, 2011-05-02

7 answers

Powinno działać używając - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated używając go w ten sposób:

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

atScrollPosition może przyjąć dowolną z tych wartości:

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

Mam nadzieję, że to ci pomoże

Cheers

 130
Author: Fran Sevillano,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-08 10:29:34
[tableview scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];

Spowoduje to przeniesienie widoku tableview do pierwszego wiersza.

 16
Author: Praveen S,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-02 11:17:42

W końcu znalazłem... będzie to działać dobrze, gdy tabela wyświetla tylko 3 wiersze... jeśli wiersze są większe, należy odpowiednio zmienić...

- (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]; 
 }
 16
Author: Maulik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-08 10:40:12

Użyj [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:scrollPosition animated:YES]; Przewija odbiornik, aż wiersz określony przez ścieżkę indeksu znajdzie się w określonym miejscu na ekranie.

I

scrollToNearestSelectedRowAtScrollPosition:animated:

Przewija widok tabeli tak, że wybrany wiersz najbliższy określonej pozycji w widoku tabeli znajduje się w tej pozycji.

 14
Author: visakh7,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-02 11:20:59

Wersja Swift:

let indexPath:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0)
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.None, animated: true)

Enum: są to dostępne pozycje przewijania tableView-tutaj w celach informacyjnych. Nie musisz umieszczać tej sekcji w swoim kodzie.

public enum UITableViewScrollPosition : Int {

case None
case Top
case Middle
case Bottom
}

Diselectrow:

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()
    }

}
 7
Author: odemolliens,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-12 01:15:22

Warto zauważyć, że jeśli zastosujesz podejście setContentOffset, może to spowodować, że widok tabeli/kolekcji trochę przeskoczy. Szczerze mówiąc, spróbowałbym zrobić to w inny sposób. Zaleca się korzystanie z metod delegowania widoku przewijania, które są dostępne za darmo.

 1
Author: Stephen Paul,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-26 23:26:04

Po prostu pojedyncza linia kodu:

self.tblViewMessages.scrollToRow(at: IndexPath.init(row: arrayChat.count-1, section: 0), at: .bottom, animated: isAnimeted)
 0
Author: Mr.Javed Multani,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-08 17:56:33