从底部加载UITableView
我试图用UITableView
来模仿iMessage泡泡文本行为。 为了始终滚动到底部我使用scrollToRowAtIndexPath
当viewDidLoad
和viewDidAppear
。 这是因为当调用viewDidLoad
方法时,表没有被完全加载,所以我需要在viewDidAppear
中额外的滚动。 这个代码使窍门。 但是,我想要的不是一个animation滚动(设置为NO
animated
并不能解决这个问题),我希望表格总是从底部显示,不加载表,然后到最后一行。
这可能吗? 我找不到任何完全符合所需行为的解决scheme。
您可以避免来自viewDidLoad的调用,因为从viewDidAppear内滚动使第一次调用成为冗余。 viewDidAppear每次回到视图时被调用,但viewDidLoad只在初始化视图时被调用一次。
我同意以前的build议隐藏用户的滚动,而不是改变UITableView加载数据的方式。 我的build议是在viewWillAppear方法中使用scrollToRowAtIndexPath方法,将animation设置为NO。 之后,如果在表格对用户可见时必须添加新行,请使用insertRowsAtIndexPaths:withRowAnimation:在表格视图底部添加一行。 请确保在数据模型的末尾添加数据,以便当用户导航离开并返回时,他/她会回到相同的布局。
希望这可以帮助。
编辑:刚刚看到你不接受以前的答案的原因,并认为我会详细阐述一点。 我build议的解决scheme将需要最小的努力,避免一次又一次地调用reloadData,从而避免一次又一次地调用scrollToRowAtIndexPath方法。 您只需要在viewWillAppear中对scrollToRowAtIndexPath进行一次调用即可滚动到表视图的底部(这样做时隐藏了用户的转换),您不需要再次这样做。
这是最好的解决scheme!
只是扭转一切!
tableView.transform = CGAffineTransformMakeRotation(-M_PI); cell.transform = CGAffineTransformMakeRotation(M_PI);
请小心,因为现在headerView和footerView的位置相反。
我在我制作的RPN计算器中做类似的事情。 我有一个表格中的所有数字,当一个数字被添加到堆栈时,一切都popup一个单元格。 当我加载我呼吁的视图:
[self.myTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:NumOfStackItems - 1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
在我看来,将会出现。 这样我的表视图开始显示在堆栈的底部,并没有看到animation。 通过把这个放在viewWillAppear中,每次我浏览到视图,它都出现在表的底部。
当我将数字添加到堆栈中时,我只是将其添加到包含所有数字的数组中,然后将文本放在适当的行中,如下所示:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // Cell initialization here... NSUInteger row_num = [indexPath row]; cell.rowNumber.text = [NSString stringWithFormat:@"%g", [DataArray objectAtIndex:NumberOfStackItems-row_num-1];// subtract the row number off to get the correct array index return cell }
我也确保每当我用新的值更新tableview时,首先调用reloadData函数,然后调用上面引用的scrollToRowAtIndexPath函数,这样我就停留在表的底部。
解决方法是重写viewWillAppear
并让它滚动(非animation)到底部:
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self goToBottom]; } -(void)goToBottom { NSIndexPath *lastIndexPath = [self lastIndexPath]; [self.tableView scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO]; } -(NSIndexPath *)lastIndexPath { NSInteger lastSectionIndex = MAX(0, [self.tableView numberOfSections] - 1); NSInteger lastRowIndex = MAX(0, [self.tableView numberOfRowsInSection:lastSectionIndex] - 1); return [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex]; }
通过在viewWillAppear
执行此操作将在用户查看表之前完成。
您可以将您的UITableView
隐藏在viewDidLoad
,然后在将表格滚动到底部之后将其更改为在viewDidAppear
上可见。 这样用户将看不到滚动animation。
你可以通过制作一个隐藏的页脚并在那里进行计算来修复它。 当页脚被加载时,contentSize被更新。 为了让它滚动我检查设置tableview的contentOffset。
我已经评论了animation的一部分,因为你想没有,但它也有效。
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 1; } -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { if( tableView.contentOffset.y != tableView.contentSize.height - tableView.frame.size.height && automaticScroll ){ //[UIView animateWithDuration:0.0 animations:^{ self.contentTableView.contentOffset = CGPointMake(0, tableView.contentSize.height - self.contentTableView.frame.size.height); //} completion:^(BOOL finished) { [tableView reloadData]; //}]; automaticScroll = NO; } UIView *emptyFooter = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)]; emptyFooter.backgroundColor = [UIColor clearColor]; return emptyFooter; }
我创build了一个BOOL自动滚动触发滚动到底部。 这应该在viewWillAppear方法中设置,或者当您加载数据并重新加载tableView时。
如果你想添加行,你还需要设置BOOL,如:
-(void)addItemButtonClicked:(id)sender { automaticScroll = YES; //Add object to data [self.contentTableView reloadData]; }
如果您需要更多帮助,请告诉我。
scrollToRowAtIndexPath
用于将tableview中的行滚动到特定的位置
如果高度小于父视图,只需在加载数据后更改内容embedded来移动表视图的内容视图。
[self.tableView reloadData]; [self.tableView setContentInset:UIEdgeInsetsMake(self.view.frame.size.height - self.tableView.contentSize.height < 0 ? 0 : self.view.frame.size.height - self.tableView.contentSize.height, 0, 0, 0)];
Swift 3.1
tableView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi)) cell.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi))
积分:@Christos Hadjikyriacou
Swift 3.0
// Rotate let rotate = CGAffineTransform(rotationAngle: -(CGFloat)(M_PI)) // Move to the left let translate = CGAffineTransform(scaleX: -1, y: 1) self.tableView.transform = rotate.concatenating(translate)