浅谈iOS 11.0中UITableView 都更改了什么? (一)

版本记录

版本号 时间
V1.0 2018.01.25

前言

2017年iOS版本更新到了11.0的系统,新机器比如iPhone X都是预装11.0的系统,而我们的UIKit框架中的UITableView类都做了哪些更改?接下来我们就看一下iOS11.0中UITableView类的改变,共22处新增,改动的还是很大的,下面我们就详细的看一下。

属性 @property (nonatomic, weak, nullable) id <UITableViewDragDelegate> dragDelegate

这个是iOS11.0新增的一个代理属性,该代理是从一个tableview初始化拖动的接口。在iOS中新增的drag和drop,是一种以图形展现的方式把数据从一个 app 移动或拷贝到另一个 app(仅限iPad),或者在程序内部进行。

在你使用的对象中实现这个协议来初始化tableview的拖动。 这个协议的唯一required方法是tableView:itemsForBeginningDragSession:atIndexPath:方法,但是您可以根据需要实现其他方法来自定义tableview的拖动行为。

将您的自定义委托对象分配给您的tableviewdragDelegate属性。

下面我们就简单的看一下这个UITableViewDragDelegate代理的API

// Drag & Drop

API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos, watchos)
@protocol UITableViewDragDelegate <NSObject>

@required

// Provide items to begin a drag associated with a given index path.
// You can use -[session locationInView:] to do additional hit testing if desired.
// If an empty array is returned a drag session will not begin.
- (NSArray<UIDragItem *> *)tableView:(UITableView *)tableView itemsForBeginningDragSession:(id<UIDragSession>)session atIndexPath:(NSIndexPath *)indexPath;

@optional

// Called to request items to add to an existing drag session in response to the add item gesture.
// You can use the provided point (in the table view's coordinate space) to do additional hit testing if desired.
// If not implemented, or if an empty array is returned, no items will be added to the drag and the gesture
// will be handled normally.
- (NSArray<UIDragItem *> *)tableView:(UITableView *)tableView itemsForAddingToDragSession:(id<UIDragSession>)session atIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point;

// Allows customization of the preview used for the row when it is lifted or if the drag cancels.
// If not implemented or if nil is returned, the entire cell will be used for the preview.
- (nullable UIDragPreviewParameters *)tableView:(UITableView *)tableView dragPreviewParametersForRowAtIndexPath:(NSIndexPath *)indexPath;

// Called after the lift animation has completed to signal the start of a drag session.
// This call will always be balanced with a corresponding call to -tableView:dragSessionDidEnd:
- (void)tableView:(UITableView *)tableView dragSessionWillBegin:(id<UIDragSession>)session;

// Called to signal the end of the drag session.
- (void)tableView:(UITableView *)tableView dragSessionDidEnd:(id<UIDragSession>)session;

// Controls whether move operations are allowed for the drag session.
// If not implemented, defaults to YES.
- (BOOL)tableView:(UITableView *)tableView dragSessionAllowsMoveOperation:(id<UIDragSession>)session;

// Controls whether the drag session is restricted to the source application.
// If not implemented, defaults to NO.
- (BOOL)tableView:(UITableView *)tableView dragSessionIsRestrictedToDraggingApplication:(id<UIDragSession>)session;

@end

可见,这个代理方法的内容还是很多的,有1个required方法和6个optional方法。下面我们继续

Topics

1. Providing the Items to Drag

2. Tracking the Drag Session

3. Providing a Custom Preview

4. Instance Methods


属性 @property (nonatomic, weak, nullable) id <UITableViewDropDelegate> dropDelegate API_AVAILABLE

这个是iOS11.0新加入的代理属性,提供用于在TableView中处理drops的接口。

在你使用的对象中实现这个协议,把被拖放的数据合并到你的TableView中。 此协议的唯一必需实现的方法是tableView:performDropWithCoordinator:方法,但是您可以根据需要实现其他方法来自定义TableView的drop行为。

将您的自定义委托对象分配给您的TableViewdropDelegate属性。

下面我们就简单的看一下这个UITableViewDropDelegate代理的API

API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos, watchos)
@protocol UITableViewDropDelegate <NSObject>

@required

// Called when the user initiates the drop.
// Use the drop coordinator to access the items in the drop and the final destination index path and proposal for the drop,
// as well as specify how you wish to animate each item to its final position.
// If your implementation of this method does nothing, default drop animations will be supplied and the table view will
// revert back to its initial state before the drop session entered.
- (void)tableView:(UITableView *)tableView performDropWithCoordinator:(id<UITableViewDropCoordinator>)coordinator;

@optional

// If NO is returned no further delegate methods will be called for this drop session.
// If not implemented, a default value of YES is assumed.
- (BOOL)tableView:(UITableView *)tableView canHandleDropSession:(id<UIDropSession>)session;

// Called when the drop session begins tracking in the table view's coordinate space.
- (void)tableView:(UITableView *)tableView dropSessionDidEnter:(id<UIDropSession>)session;

// Called frequently while the drop session being tracked inside the table view's coordinate space.
// When the drop is at the end of a section, the destination index path passed will be for a row that does not yet exist (equal
// to the number of rows in that section), where an inserted row would append to the end of the section.
// The destination index path may be nil in some circumstances (e.g. when dragging over empty space where there are no cells).
// Note that in some cases your proposal may not be allowed and the system will enforce a different proposal.
// You may perform your own hit testing via -[session locationInView:]
- (UITableViewDropProposal *)tableView:(UITableView *)tableView dropSessionDidUpdate:(id<UIDropSession>)session withDestinationIndexPath:(nullable NSIndexPath *)destinationIndexPath;

// Called when the drop session is no longer being tracked inside the table view's coordinate space.
- (void)tableView:(UITableView *)tableView dropSessionDidExit:(id<UIDropSession>)session;

// Called when the drop session completed, regardless of outcome. Useful for performing any cleanup.
- (void)tableView:(UITableView *)tableView dropSessionDidEnd:(id<UIDropSession>)session;

// Allows customization of the preview used when dropping to a newly inserted row.
// If not implemented or if nil is returned, the entire cell will be used for the preview.
- (nullable UIDragPreviewParameters *)tableView:(UITableView *)tableView dropPreviewParametersForRowAtIndexPath:(NSIndexPath *)indexPath;

@end

可以看见,有1个必须实现的方法和6个可选方法。下面我们继续。

Topics

1. Declaring Support for Handling Drops

2. Incorporating the Dropped Data

3. Tracking the Drag Movements

4. Instance Methods


属性 @property (nonatomic) UITableViewSeparatorInsetReference separatorInsetReference

我们先看一下这个新增的属性,对于cells更改自定义的separatorInset值的解释方式。 默认值是UITableViewSeparatorInsetFromCellEdges

下面我们就看一下这个枚举

typedef NS_ENUM(NSInteger, UITableViewSeparatorInsetReference) {
    // The value set to the separatorInset property is interpreted as an offset from the edges of the cell.
    设置为separatorInset属性的值被解释为与单元格边缘的偏移量
    UITableViewSeparatorInsetFromCellEdges,
    
    // The value set to the separatorInset property is interpreted as an offset from the automatic separator insets.
    设置为separatorInset属性的值被解释为与自动分隔符的偏移量。
    UITableViewSeparatorInsetFromAutomaticInsets
} API_AVAILABLE(ios(11.0), tvos(11.0));

后记

本篇已结束,后面更精彩~~~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容