UITableView && UIScrollview 实现刷新加载

UITableView:
UITableView有两种风格:
UITableViewStylePlain
UITableViewStyleGrouped
UITableView中每行数据都是一个UITableViewCell
UITableViewCell自带有
一个UIView控件(contentView,作为其他元素的父控件)
两个UILable控件(textLabel、detailTextLabel)
一个UIImage控件(imageView)
分别用于容器、显示内容、详情和图片。
UITableViewCell的UITableViewCellStyle成员变量决定了Cell中存在哪些控件
typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
UITableViewCellStyleDefault, // 左侧显示textLabel(不显示detailTextLabel),imageView可选(显示在最左边)
UITableViewCellStyleValue1, // 左侧显示textLabel、右侧显示detailTextLabel(默认蓝色),imageView可选(显示在最左边)
UITableViewCellStyleValue2, // 左侧依次显示textLabel(默认蓝色)和detailTextLabel,imageView可选(显示在最左边)
UITableViewCellStyleSubtitle // 左上方显示textLabel,左下方显示detailTextLabel(默认灰色),imageView可选(显示在最左边)
};
主要成员变量:
1.id <UITableViewDataSource> dataSource;//数据源
UITableViewDataSource协议描述了UITableView的数据源
必须实现的协议方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
可选实现的协议方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
//实现对UITableView编辑的协议方法
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
//
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;

2.id <UITableViewDelegate> delegate;//代理
····主要协议方法有:
有关显示的回调:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
设置高度的回调取值:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
设置表头表尾的自定义视图的回调取值:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; // custom view for header. will be adjusted to default or specified header height
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
点击Cell的回调:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

 

UIScrollView:
成员变量:
CGPoint contentOffSet 监控目前滚动的位置
CGSize contentSize 滚动范围的大小
UIEdgeInsets contentInset 视图在scrollView中的位置
id<UIScrollerViewDelegate> delegate 设置协议
BOOL directionalLockEnabled 指定控件是否只能在一个方向上滚动
BOOL bounces 控制控件遇到边框是否反弹
BOOL alwaysBounceVertical 控制垂直方向遇到边框是否反弹
BOOL alwaysBounceHorizontal 控制水平方向遇到边框是否反弹
BOOL pagingEnabled 控制控件是否整页翻动
BOOL scrollEnabled 控制控件是否能滚动
BOOL showsHorizontalScrollIndicator 控制是否显示水平方向的滚动条
BOOL showsVerticalScrollIndicator 控制是否显示垂直方向的滚动条
UIEdgeInsets scrollIndicatorInsets 指定滚动条在scrollerView中的位置
UIScrollViewIndicatorStyleindicatorStyle 设定滚动条的样式
float decelerationRate 改变scrollerView的减速点位置
BOOL tracking 监控当前目标是否正在被跟踪
BOOL dragging 监控当前目标是否正在被拖拽
BOOL decelerating 监控当前目标是否正在减速
BOOL delaysContentTouches 控制视图是否延时调用开始滚动的方法
BOOL canCancelContentTouches 控制控件是否接触取消touch的事件
float minimumZoomScale 缩小的最小比例
float maximumZoomScale 放大的最大比例
float zoomScale 设置变化比例
BOOL bouncesZoom 控制缩放的时候是否会反弹
BOOL zooming 判断控件的大小是否正在改变
BOOL zoomBouncing 判断是否正在进行缩放反弹
BOOL scrollsToTop 控制控件滚动到顶部

主要成员方法:
[myScrollView setContentOffset:CGPointMake(0,200) animated:YES];//滚动到指定位置

id<UIScrollerViewDelegate> delegate
// 返回一个放大或者缩小的视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{

}
// 开始放大或者缩小
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view{

}
// 缩放结束时
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale{

}
// 视图已经放大或缩小
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
NSLog(@"scrollViewDidScrollToTop");
}
// 是否支持滑动至顶部
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView{
return YES;
}
// 滑动到顶部时调用该方法
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{
NSLog(@"scrollViewDidScrollToTop");
}
// scrollView 已经滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(@"scrollViewDidScroll");
}
// scrollView 开始拖动
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"scrollViewWillBeginDragging");
}
// scrollView 结束拖动
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
NSLog(@"scrollViewDidEndDragging");
}
// scrollView 开始减速(以下两个方法注意与以上两个方法加以区别)
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
NSLog(@"scrollViewWillBeginDecelerating");
}
// scrollview 减速停止
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSLog(@"scrollViewDidEndDecelerating");
}

 

 

结合动画缓等待

// 下拉刷新的原理
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
NSLog(@"BeginDecelerating");
if (scrollView.contentOffset.y < - 100) {
[UIView animateWithDuration:2.0 animations:^{
// frame发生偏移,距离顶部150的距离(可自行设定)
myTableView.contentInset = UIEdgeInsetsMake(150.0f, 0.0f, 0.0f, 0.0f);
} completion:^(BOOL finished) {
/**
* 发起网络请求,请求刷新数据
*/
}];
}
}

// 上拉加载的原理
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
NSLog(@"%f",scrollView.contentOffset.y);
NSLog(@"%f",scrollView.frame.size.height);
NSLog(@"%f",scrollView.contentSize.height);
/**
* 关键-->
* scrollView一开始并不存在偏移量,但是会设定contentSize的大小,所以contentSize.height永远都会比contentOffset.y高一个手机屏幕的
* 高度;上拉加载的效果就是每次滑动到底部时,再往上拉的时候请求更多,那个时候产生的偏移量,就能让contentOffset.y + 手机屏幕尺寸高大于这
* 个滚动视图的contentSize.height
*/
if (scrollView.contentOffset.y + scrollView.frame.size.height >= scrollView.contentSize.height) {
NSLog(@"%d %s",__LINE__,__FUNCTION__);
[UIView commitAnimations];
[UIView animateWithDuration:2.0 animations:^{
// frame发生的偏移量,距离底部往上提高60(可自行设定)
myTableView.contentInset = UIEdgeInsetsMake(0, 0, 150, 0);
} completion:^(BOOL finished) {
/**
* 发起网络请求,请求加载更多数据
* 然后在数据请求回来的时候,将contentInset改为(0,0,0,0)
*/
}];
}
}

转载于:https://www.cnblogs.com/Jk-Chan/p/5271908.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值