tableView 自动滚动到底部

- (void)addData:(id)object{

NSInteger count = self.dataArray.count;

_index ++;

NSString * str = [NSString stringWithFormat:@"当前第%zd行",_index];

[self.dataArray addObject:str];

if (count * 44.0f > self.tableView.height) {

NSMutableArray *insert = [[NSMutableArray alloc] init];

for (NSInteger index = count; index < count+1; index++) {

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];

[insert addObject:indexPath];

}

[self.tableView beginUpdates];

[self.tableView insertRowsAtIndexPaths:insert withRowAnimation:UITableViewRowAnimationFade];

[self.tableView endUpdates];

[self.tableView layoutIfNeeded];

[self changeInsets:object];

[self scrollToBottom];

} else {

[self.tableView reloadData];

}

}

- (void)changeInsets:(id)object

{

CGFloat height = 0;

height += 44.0f;

//    for (NTESMessageModel *model in newModels) {

//        height += 44.0f;

//    }

UIEdgeInsets insets = self.tableView.contentInset;

CGFloat contentHeight = self.tableView.contentSize.height - insets.top;

contentHeight += height;

CGFloat top = contentHeight > self.tableView.height? 0 : self.tableView.height - contentHeight;

insets.top = top;

//    MyLog(@"tableViewHeight == %f",self.tableView.height);

//    MyLog(@"top == %f",top);

self.tableView.contentInset = insets;

}

- (void)scrollToBottom

{

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

CGFloat offset = self.tableView.contentSize.height - self.tableView.height;

[self.tableView scrollRectToVisible:CGRectMake(0, offset, self.tableView.width, self.tableView.height) animated:YES];

});

}

原文地址:https://www.cnblogs.com/Jackie-subDai/p/9290099.html

时间: 2024-08-09 06:39:46

tableView 自动滚动到底部的相关文章

C# ListBox 自动滚动到底部 方法:

在ListBox中添加一条记录(ListBox.Items.Add方法)后,滚动条会自动回到顶部.我们可能更希望它自动滚动到底部,简要介绍几种方法. 方法一: 1 this.listBox1.Items.Add("new line"); 2 this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1; 3 this.listBox1.SelectedIndex = -1; 在添加记录后,先选择最后一条记录,滚动条会自动到底部,

数据更新后让ListView自动滚动到底部

在做聊天界面的时候想要发送新的数据后,listview自动滚动到底部,显示出最新的数据.网上找了两个方法,觉得不错,记录一下. 方法一: 给listview添加下面两个属性 android:stackFromBottom="true"android:transcriptMode="alwaysScroll" or mListView.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL); 方法二: //

JS + jQuery 实现元素自动滚动到底部,兼容IE、FF、Chrome

HTML代码: <ul class="tasklog-dialog-ul" id="auto_to_bottom"> <li>删除虚拟机快照成功</li> <li>删除虚拟机快照成功</li> <li>删除虚拟机快照成功</li> <li>删除虚拟机快照成功</li> <li>删除虚拟机快照成功</li> <li>删除虚拟机

隐藏状态栏后tableview自动滚动的问题

最近在开发过程中碰到一个很奇怪的问题,在将状态栏隐藏掉之后,页面上的tableView会自动向上滚20个像素. 状态如下: 这是因为在iOS7.0之后,系统会自动调整scrollView的layout 和 contentInsets .从而使其自动地适应iOS7之后那种整个屏幕都可以放入自定义控件的方法.而我们大多数在开发过程中并不希望收到系统自动的影响而是完全掌握开发过程中的每一个细节.还有一个愿意是因为目前我们大多数的应用都是要向下适配到iOS6,而在6中并未提供这样的特性,所以在代码级别依

c#编程之文件IO 自动滚动至底部(补充)

这里对上一文章做一个补充,因为很多时候,我们需要分步计算多个文件的crc值,而往往最后显示的值都需要在第一时间内进入眼帘.所以我们需要确保最后文件的计算结果必须显示到UI的最前端. 同样,我们依然使用格式:控件名称+ScrollToEnd()函数实现: 代码如下: 1 UserInput.ScrollToEnd(); 2 if(UserInput.IsFocused==true) 3 { 4 UserInput.SelectionStart = UserInput.Text.Length; 5

如何让listview滚动到底部

方法一: // msgListView是ListView控件 // adapter是ListView绑定的Adapter,如果不方便直接使用,也可以通过ListView的getAdapter()方法获取到,前提是你已经绑定了适配器哦 // 里面的参数就很熟悉了吧,其实这个方法的主要作用是选中listview的指定列,选中了,自然就得让这个item可见,自然就滚动咯 msgListView.setSelection(adapter.getCount()-1);  方法二: // 这个就比较直接了

iOS scrollView/tableView滚动到底部

//项目要求tableView滚动到底部就自动加载下一页,UITableView继承自UIScrollView 所以可以在//scrollViewDidEndDecelerating这个方法中进行判断操作 1 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ 2 if (scrollView == myScrollView) { 3 CGPoint offset = myScrollView.contentOffset

133自动滚动到被选中单元格(扩展知识:滚动到头部和底部)

效果如下: ViewController.h 1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UITableViewController 4 @property (strong, nonatomic) NSMutableArray *mArrDataSource; 5 6 @end ViewController.m 1 #import "ViewController.h" 2 3 @interface Vi

让DIV的滚动条自动滚动到最底部 - 3种方法

要制作一个在线聊天的程序,在做最后的修饰时,需要对获得的信息即时滚动以保证用户总能看到最新消息. 聊天程序是基于AJAX设计的,没有用框架,消息容器是一个DIV,所以问题就在于如何控制DIV的滚动条. 但同样的代码拿到我这里却完全失效,又仔细查了下资料说XHTML标准下scrollTop的值恒为0,解决办法是使用 document.documentElement.scrollTop代替document.body.scrollTop,讲了半天所解决的是整个页 面的滚动条.这个方法我是用不了了,因为