最近在做的一个项目,其中有一个场景为搜索,遇到如下问题:输入A关键词,完成搜索后,滑动到第3页;再输入B关键词,完成搜索后,仍然是在第3页,而需求是改变关键词后,页数回到第一页。那就需要在切换关键词后,将tableView滑动到顶部。
在google搜索了下资料,在代码里加了一句话,问题成功解决。
[self scrollToTop];//在reloadData后面加上
- (void)scrollToTop {
[self.tableView setContentOffset:CGPointMake(0,0) animated:YES];
}
另外文章里讲得其他滑动问题的解决方案也不错,摘录下来:
Q:[让tableview滚动到顶端] 从另一个view进入到一个tableview时,总是会自动滚动到先前的滚动条位置,我想让它每次进入这个tableview时,都滚动回最顶端,应该用哪个消息呢?
A: 方法一:使用
scrollToRowAtIndexPath
方法二:
- (void)scrollToTop {
[self.tableView setContentOffset:CGPointMake(0,0) animated:YES];
}
[self.tableView setContentOffset:CGPointMake(0,0) animated:YES];
}
- (void)scrollToBottom {
NSUInteger sectionCount = [self.tableView numberOfSections];
if (sectionCount) {
NSUInteger rowCount = [self.tableView numberOfRowsInSection:0];
if (rowCount) {
NSUInteger ii[2] = {0, rowCount - 1};
NSIndexPath* indexPath = [NSIndexPath indexPathWithIndexes:ii length:2];
[self.tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
}
}
NSUInteger sectionCount = [self.tableView numberOfSections];
if (sectionCount) {
NSUInteger rowCount = [self.tableView numberOfRowsInSection:0];
if (rowCount) {
NSUInteger ii[2] = {0, rowCount - 1};
NSIndexPath* indexPath = [NSIndexPath indexPathWithIndexes:ii length:2];
[self.tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
}
}
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:_currentRow inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
首先使用selectRowAtIndexes: 选择行数,滚动的话tableview的superview时scrollview,scrollview可以滚动到某个position 那么就要计算这个position position = table row height * index,就得到滚动的位置了。
Q:在使用SLQite3调用sqlite3_bind_text函数时需要使用char *类型的参数,在sqlite3_column_text函数中需要使用char *类型的返回值,如何将字符串对象在NSString和Char *之间进行转换?
A:
将NSString转换成char *:[NSString UTF8String]
将char *转换成NSString:[NSString stringWithUTF8String:]
例如:
//=========char * to NSString============
要得到几天几分几秒的,算算就出来了。
Q:怎么实现一个登录页面,在登录成功后跳转到另一个页面(我想实现先是一个登录界面点击一个登录按钮载跳转到UITabBarController界面怎样处理啊 )?
A:可以尝试下面的方法:
1,在MainWindow.xib里放入LoginViewController和UITabBarController。
2,Delegate里application加入下记代码。
Q:UITableViewCell 里 有个 UITextField当点击UITextField时会出现软键盘,为了返回UITextField的值,我在valueChanged事件绑定了 rootViewController 的
-(IBAction) textAction : (id) sender;
可是我同时需要知道该Cell 的 indexPath.row 该怎么做?
A:有两种方法:
方法1
先获取UITextField所在的Cell.
<img src="http://ddkangfu.blog.51cto.com/'#\'"" editer="" inblock.gif"="" align="top" style="padding: 0px; margin: 0px; vertical-align: top; border: none; ">}