个人小摘录

UIScrollView

滚动视图, 拥有滚动功能, 主要出现在一些界面,一个屏幕无法显示全的情况

常用属性和方法

//当前滚动偏移量

@property(nonatomic) CGPoint contentOffset;

//内容大小, 必须设置了大小才能滚动

@property(nonatomic) CGSize contentSize; 

//内容边距

@property(nonatomic) UIEdgeInsets contentInset;

//代理方法

@property(nullable,nonatomic,weak) id<UIScrollViewDelegate> delegate; 

//边缘是否可以弹簧效果

@property(nonatomic) BOOL bounces;

//是否翻页模式

@property(nonatomic,getter=isPagingEnabled) BOOL pagingEnabled

//是否可以滚动

@property(nonatomic,getter=isScrollEnabled) BOOL scrollEnabled; 

//是否显示横向滚动条

@property(nonatomic) BOOL showsHorizontalScrollIndicator;

//是否显示竖向滚动条

@property(nonatomic) BOOL showsVerticalScrollIndicator;

//滚动条风格

@property(nonatomic) UIScrollViewIndicatorStyle indicatorStyle; 

//最小缩放值

@property(nonatomic) CGFloat minimumZoomScale; 

//最大缩放值

@property(nonatomic) CGFloat maximumZoomScale; 

//当前缩放值

@property(nonatomic) CGFloat zoomScale 

//是否允许点击状态栏, 滚动到顶部模式.(只有当控制器中只有一个视图时生效)

@property(nonatomic) BOOL scrollsToTop

//键盘消失方式, 默认值UIScrollViewKeyboardDismissModeNone

@property(nonatomic) UIScrollViewKeyboardDismissMode keyboardDismissMode;


typedef NS_ENUM(NSInteger, UIScrollViewKeyboardDismissMode) {

    UIScrollViewKeyboardDismissModeNone,

    UIScrollViewKeyboardDismissModeOnDrag,      //当滚动时,让键盘消失        

UIScrollViewKeyboardDismissModeInteractive, //键盘随着向下滚动, 移动消失

}

//设置当前滚动到的位置

- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;

//设置当前缩放值

- (void)setZoomScale:(CGFloat)scale animated:(BOOL)animated 

代理方法

//发生滚动操作时

- (void)scrollViewDidScroll:(UIScrollView *)scrollView; 

//用户将要开始拖拽

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;

//用户将要结束拖拽操作

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset; 

//用户已经结束了拖拽操作(手指从屏幕离开), decelerate:表示是否减速

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;

//将要开始减速动画

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView;

//减速动画完成

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;

//方法setContentOffset/scrollRectVisible:animated: 动画结束时

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView;

//拥有缩放功能的视图, 一个滚动视图只能返回一个拥有缩放功能子视图

- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView; 

//开始缩放某个视图

- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view

//结束对某个视图的缩放操作

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale;

//默认返回YES, 表示是否允许滚动到头部

- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;

//滚动到头部

- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView;

循环滚动原理

加入有4张图片, 普通排序为 1 2 3 4 , 循环滚动的排序是4 1 2 3 4 1,
通过代理方法实时监听用户当前滚动到的图片位置, 当滚动到左4, 使用非动画效果切换到右4
同理 图片1.

主要使用的协议方法为

//发生滚动操作时

- (void)scrollViewDidScroll:(UIScrollView *)scrollView; 

//减速动画完成

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;

UITableView

表格视图, iOS程序的80%界面都跟这个视图有关系, 是整个iOS开发中最重要的控件
表格的样式

常用属性

@property (nonatomic, readonly) UITableViewStyle style;

typedef NS_ENUM(NSInteger, UITableViewStyle) {

    UITableViewStylePlain,          // 正常模式

    UITableViewStyleGrouped         // 分组方式};

//背景视图, 自动适应大小,与表格视图一样. 出现在所有cell的后面

@property (nonatomic, strong, nullable) UIView *backgroundView

//行高

@property (nonatomic) CGFloat rowHeight; 

//组头高,最小1

@property (nonatomic) CGFloat sectionHeaderHeight;

//组脚高,最小1

@property (nonatomic) CGFloat sectionFooterHeight;

//数据代理

@property (nonatomic, weak, nullable) id <UITableViewDataSource> dataSource;

//操作代理

@property (nonatomic, weak, nullable) id <UITableViewDelegate> delegate;

常用方法

//初始化方法

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style

//某个cell的索引值

- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;

//某个索引值对应

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath; 

//重载所有数据, 刷新界面

- (void)reloadData;

数据代理UITableViewDataSource

必须实现的方法

//返回每个分区中,有多少行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;


//显示每行的视图, 需要通过重用标识'reuseIdentifier''dequeueReusableCellWithIdentifier:'方法配合使用.

- (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;

//某行是否可以开启编辑模式, 默认返回YES,表示都可以编辑

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

//某行是否可以移动, 配合代理方法'-tableView:moveRowAtIndexPath:toIndexPath:' 使用

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;

//通讯录右侧的快捷跳转标识 (例如 "ABCD...Z#")

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView; 

//某个快捷跳转标识对应的section索引值, 例如B对应索引值1

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;

// 当编辑操作被处罚时, 例如对Cell的删除,添加操作

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;

// Cell的移动操作

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;

重用机制

操作代理UITableViewDelegate

显示相关的自定义设置

- (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;


// Use the estimatedHeight methods to quickly calcuate guessed values which will allow for fast load times of the table.

// If these methods are implemented, the above -tableView:heightForXXX calls will be deferred until views are ready to be displayed, so more expensive logic can be placed there.

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(7_0);

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);

自定义分区头部和脚部视图

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;   // custom view for header. will be adjusted to default or specified header height

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;   // custom view for footer. will be adjusted to default or specified footer height

辅助视图

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;

选择Cell相关操作

// -tableView:shouldHighlightRowAtIndexPath: is called when a touch comes down on a row. 

// Returning NO to that message halts the selection process and does not cause the currently selected row to lose its selected look while the touch is down.

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);

- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);


// Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection.

- (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;

- (nullable NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);

// Called after the user changes the selection.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);

编辑相关操作

// Allows customization of the editingStyle for a particular cell located at 'indexPath'. If not implemented, all editable cells will have UITableViewCellEditingStyleDelete set for them when the table has editing property set to YES.

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;

- (nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED; // supercedes -tableView:titleForDeleteConfirmationButtonForRowAtIndexPath: if return value is non-nil


// Controls whether the background is indented while editing.  If not implemented, the default is YES.  This is unrelated to the indentation level below.  This method only applies to grouped style table views.

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;


// The willBegin/didEnd methods are called whenever the 'editing' property is automatically changed by the table (allowing insert/delete/move). This is done by a swipe activating a single row

- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath __TVOS_PROHIBITED;

- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath __TVOS_PROHIBITED;

// Moving/reordering


// Allows customization of the target row for a particular row as it is being moved/reordered

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;               

Indentation

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath; // return 'depth' of row for hierarchies

//下方三个方法都必须完成

- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(5_0);

- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender NS_AVAILABLE_IOS(5_0);

- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender NS_AVAILABLE_IOS(5_0);

3D Touch技术

- (BOOL)tableView:(UITableView *)tableView canFocusRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0);

- (BOOL)tableView:(UITableView *)tableView shouldUpdateFocusInContext:(UITableViewFocusUpdateContext *)context NS_AVAILABLE_IOS(9_0);

- (void)tableView:(UITableView *)tableView didUpdateFocusInContext:(UITableViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator NS_AVAILABLE_IOS(9_0);

- (nullable NSIndexPath *)indexPathForPreferredFocusedViewInTableView:(UITableView *)tableView NS_AVAILABLE_IOS(9_0);

编辑相关

//当前编辑状态, 非动画

@property (nonatomic, getter=isEditing) BOOL editing;

//动画开启编辑状态

- (void)setEditing:(BOOL)editing animated:(BOOL)animated;

动画操作相关

动画模式

typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {

    UITableViewRowAnimationFade, //渐变效果

    UITableViewRowAnimationRight,    //从右侧滑入或者滑出

    UITableViewRowAnimationLeft,    //从左侧滑入或者滑出

    UITableViewRowAnimationTop, //从上滑入或者滑出

    UITableViewRowAnimationBottom,  //从下侧滑入或者滑出

    UITableViewRowAnimationNone,     //无动画

    UITableViewRowAnimationMiddle,   //滚动到中间

    UITableViewRowAnimationAutomatic = 100  //自动选择动画

};

//滚动方向

typedef NS_ENUM(NSInteger, UITableViewScrollPosition) {

    UITableViewScrollPositionNone,  

    UITableViewScrollPositionTop,   //向头部方向

    UITableViewScrollPositionMiddle,     //向中间

    UITableViewScrollPositionBottom //向底部

};  

//滚动到某一行    

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

//滚动到被选中的最近一行, 因为行可以多选, 通常是单选

- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

--------------------------华丽的分割线---------------------------

重要的事情说三遍:

若要编辑表格, 必须先改数据, 再改UI.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值