【问题标题】:what should I do if I want to see the tablview from the last [duplicate]如果我想从上一个 [重复] 中查看表格视图,我该怎么办
【发布时间】:2013-07-08 05:14:09
【问题描述】:

嘿,我想制作一个聊天演示。所以当我为它打开 UITableView 时,它应该从最后一项显示,前一项应该在上侧。即,我想将我的 UITableView 自动滚动到最后一行当用户打开聊天屏幕时。我该怎么办?

我的代码是-

cell = [tv dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
}

    [lblName setFont:[UIFont fontWithName:@"Arial" size:12]];
    lblName.lineBreakMode = NSLineBreakByWordWrapping;
    lblName.numberOfLines = 0;
    lblName.text=[arrUsername objectAtIndex:indexPath.row];
    lblName.textColor = [UIColor blackColor];
    [lblName setBackgroundColor:[UIColor grayColor]];
    if ([[arrCommentUid objectAtIndex:indexPath.row] isEqualToString:@"1"]) {
        [lblName setTextAlignment:NSTextAlignmentLeft];
    }
    else

请帮忙!!它只是 UITableView 的 optional.cell 的代码

【问题讨论】:

    标签: iphone uitableview


    【解决方案1】:

    试试这个聊天应用

    SSMessagesviewcontroller
    stbubbletableviewcell
    bubblethingie
    uibubbletableview
    styledchat
    acanichat

    您也可以使用

    转到最后一行
       [tableView reloadData];
       int lastRowNumber = [tableView numberOfRowsInSection:0] - 1;
       NSIndexPath* ip = [NSIndexPath indexPathForRow:lastRowNumber inSection:0];
       [tableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:NO];
    

    【讨论】:

      【解决方案2】:

      为了做到这一点,您应该按降序对数组进行排序。因此它将从最后一个到第一个显示您的数据。

      为此,您可以使用 NSSortDescriptor

      NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:NO selector:@selector(localizedCompare:)];
      NSArray* sortedArray = [<YOURS_ARRAY> sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
      

      享受编程!

      【讨论】:

        【解决方案3】:

        您可以使用scrollToRowAtIndexPath滚动到最后一个

        [tableView scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];  
        

        您可以使用

        获取最后一行的 indexPath

        NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:([yourTableAry count] - 1) inSection:0];

        或者

        NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:([tableView numberOfRowsInSection:0] - 1) inSection:0];

        编辑

        如果你想转到行首,那么

        NSIndexPath *firstRowIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        
        [tableView scrollToRowAtIndexPath:firstRowIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
        

        【讨论】:

        • 我在哪里写这段代码?
        • 但是在使用这段代码后我不能向上走..我该怎么办?
        • 您可以向上滚动亲爱的.. 或者您可以获取更低的 indexPath 并再次使用此代码
        • 你能给我代码吗?请。我是 iOS 新手
        • 再次检查我的答案以转到顶级好友
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-02-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-25
        • 2021-07-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多