UITableView和UITableViewCell

UITableView为继承与UIScrollView的一个控件.

以下是UITableView的头文件:

//

//  UITableView.h

//  UIKit

//

//  Copyright (c) 2005-2016 Apple Inc. All rights reserved.

//


#import

#import

#import

#import

#import

#import


NS_ASSUME_NONNULL_BEGIN


typedef NS_ENUM(NSInteger, UITableViewStyle) {

    UITableViewStylePlain,          // regular table view

    UITableViewStyleGrouped         // preferences style table view

};


typedef NS_ENUM(NSInteger, UITableViewScrollPosition) {

    UITableViewScrollPositionNone,

    UITableViewScrollPositionTop,    

    UITableViewScrollPositionMiddle,   

    UITableViewScrollPositionBottom

};                // scroll so row of interest is completely visible at top/center/bottom of view


typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {

    UITableViewRowAnimationFade,

    UITableViewRowAnimationRight,           // slide in from right (or out to right)

    UITableViewRowAnimationLeft,

    UITableViewRowAnimationTop,

    UITableViewRowAnimationBottom,

    UITableViewRowAnimationNone,            // available in iOS 3.0

    UITableViewRowAnimationMiddle,          // available in iOS 3.2.  attempts to keep cell centered in the space it will/did occupy

    UITableViewRowAnimationAutomatic = 100  // available in iOS 5.0.  chooses an appropriate animation style for you

};


// Including this constant string in the array of strings returned by sectionIndexTitlesForTableView: will cause a magnifying glass icon to be displayed at that location in the index.

// This should generally only be used as the first title in the index.

UIKIT_EXTERN NSString *const UITableViewIndexSearchNS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;


// Returning this value from tableView:heightForHeaderInSection: or tableView:heightForFooterInSection: results in a height that fits the value returned from

// tableView:titleForHeaderInSection: or tableView:titleForFooterInSection: if the title is not nil.

UIKIT_EXTERN constCGFloat UITableViewAutomaticDimensionNS_AVAILABLE_IOS(5_0);


@class UITableView;

@class UINib;

@protocol UITableViewDataSource;

@protocol UITableViewDataSourcePrefetching;

@class UILongPressGestureRecognizer;

@class UITableViewHeaderFooterView;

@class UIRefreshControl;

@class UIVisualEffect;


typedef NS_ENUM(NSInteger, UITableViewRowActionStyle) {

    UITableViewRowActionStyleDefault = 0,

    UITableViewRowActionStyleDestructive = UITableViewRowActionStyleDefault,

    UITableViewRowActionStyleNormal

} NS_ENUM_AVAILABLE_IOS(8_0)__TVOS_PROHIBITED;


NS_CLASS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED @interface UITableViewRowAction :NSObject <NSCopying>


+ (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(nullableNSString *)title handler:(void (^)(UITableViewRowAction *action,NSIndexPath *indexPath))handler;


@property (nonatomic,readonly) UITableViewRowActionStyle style;

@property (nonatomic,copy, nullable)NSString *title;

@property (nonatomic,copy, nullable) UIColor *backgroundColor;// default background color is dependent on style

@property (nonatomic,copy, nullable)UIVisualEffect* backgroundEffect;


@end


NS_CLASS_AVAILABLE_IOS(9_0) @interface UITableViewFocusUpdateContext :UIFocusUpdateContext


@property (nonatomic,strong, readonly,nullable) NSIndexPath *previouslyFocusedIndexPath;

@property (nonatomic,strong, readonly,nullable) NSIndexPath *nextFocusedIndexPath;


@end


//_______________________________________________________________________________________________________________

// this represents the display and behaviour of the cells.


@protocol UITableViewDelegate<NSObject,UIScrollViewDelegate>


@optional


// Display customization


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)sectionNS_AVAILABLE_IOS(6_0);

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)sectionNS_AVAILABLE_IOS(6_0);

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPathNS_AVAILABLE_IOS(6_0);

- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)sectionNS_AVAILABLE_IOS(6_0);

- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)sectionNS_AVAILABLE_IOS(6_0);


// Variable height support


- (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)sectionNS_AVAILABLE_IOS(7_0);

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


// Section header & footer information. Views are preferred over title should you decide to provide both


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

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


// Accessories (disclosures). 


- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath NS_DEPRECATED_IOS(2_0, 3_0)__TVOS_PROHIBITED;

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


// Selection


// -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.

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

- (nullableNSIndexPath *)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);


// Editing


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

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

- (nullableNSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPathNS_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:(nullableNSIndexPath *)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


// Copy/Paste.  All three methods must be implemented by the delegate.


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

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

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


// Focus


- (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 *)coordinatorNS_AVAILABLE_IOS(9_0);

- (nullableNSIndexPath *)indexPathForPreferredFocusedViewInTableView:(UITableView *)tableViewNS_AVAILABLE_IOS(9_0);


@end


UIKIT_EXTERN NSNotificationNameconst UITableViewSelectionDidChangeNotification;



//_______________________________________________________________________________________________________________


NS_CLASS_AVAILABLE_IOS(2_0) @interface UITableView : UIScrollView <NSCoding>


- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)styleNS_DESIGNATED_INITIALIZER;// must specify style at creation. -initWithFrame: calls this with UITableViewStylePlain

- (nullableinstancetype)initWithCoder:(NSCoder *)aDecoderNS_DESIGNATED_INITIALIZER;


@property (nonatomic,readonly) UITableViewStyle style;

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

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

@property (nonatomic,weak) id<UITableViewDataSourcePrefetching> prefetchDataSourceNS_AVAILABLE_IOS(10_0);

@property (nonatomic)CGFloat rowHeight;            // will return the default value if unset

@property (nonatomic)CGFloat sectionHeaderHeight;  // will return the default value if unset

@property (nonatomic)CGFloat sectionFooterHeight;  // will return the default value if unset

@property (nonatomic)CGFloat estimatedRowHeightNS_AVAILABLE_IOS(7_0);// default is 0, which means there is no estimate

@property (nonatomic)CGFloat estimatedSectionHeaderHeightNS_AVAILABLE_IOS(7_0);// default is 0, which means there is no estimate

@property (nonatomic)CGFloat estimatedSectionFooterHeightNS_AVAILABLE_IOS(7_0);// default is 0, which means there is no estimate

@property (nonatomic)UIEdgeInsets separatorInsetNS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR; // allows customization of the frame of cell separators


@property (nonatomic,strong, nullable) UIView *backgroundViewNS_AVAILABLE_IOS(3_2);// the background view will be automatically resized to track the size of the table view.  this will be placed as a subview of the table view behind all cells and headers/footers.  default may be non-nil for some devices.


// Data


- (void)reloadData;// reloads everything from scratch. redisplays visible rows. because we only keep info about visible rows, this is cheap. will adjust offset if table shrinks

- (void)reloadSectionIndexTitlesNS_AVAILABLE_IOS(3_0);  // reloads the index bar.


// Info


@property (nonatomic,readonly) NSInteger numberOfSections;

- (NSInteger)numberOfRowsInSection:(NSInteger)section;


- (CGRect)rectForSection:(NSInteger)section;                                   // includes header, footer and all rows

- (CGRect)rectForHeaderInSection:(NSInteger)section;

- (CGRect)rectForFooterInSection:(NSInteger)section;

- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;


- (nullableNSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;                        // returns nil if point is outside of any row in the table

- (nullableNSIndexPath *)indexPathForCell:(UITableViewCell *)cell;                     // returns nil if cell is not visible

- (nullableNSArray<NSIndexPath *> *)indexPathsForRowsInRect:(CGRect)rect;                              // returns nil if rect not valid


- (nullable__kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;  // returns nil if cell is not visible or index path is out of range

@property (nonatomic,readonly) NSArray<__kindofUITableViewCell *> *visibleCells;

@property (nonatomic,readonly, nullable)NSArray<NSIndexPath *> *indexPathsForVisibleRows;


- (nullableUITableViewHeaderFooterView *)headerViewForSection:(NSInteger)sectionNS_AVAILABLE_IOS(6_0);

- (nullableUITableViewHeaderFooterView *)footerViewForSection:(NSInteger)sectionNS_AVAILABLE_IOS(6_0);


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

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


// Row insertion/deletion/reloading.


- (void)beginUpdates;  // allow multiple insert/delete of rows and sections to be animated simultaneously. Nestable

- (void)endUpdates;    // only call insert/delete/reload calls or change the editing state inside an update block.  otherwise things like row count, etc. may be invalid.


- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animationNS_AVAILABLE_IOS(3_0);

- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSectionNS_AVAILABLE_IOS(5_0);


- (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animationNS_AVAILABLE_IOS(3_0);

- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath NS_AVAILABLE_IOS(5_0);


// Editing. When set, rows show insert/delete/reorder controls based on data source queries


@property (nonatomic,getter=isEditing) BOOL editing;                            // default is NO. setting is not animated.

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


@property (nonatomic)BOOL allowsSelectionNS_AVAILABLE_IOS(3_0); // default is YES. Controls whether rows can be selected when not in editing mode

@property (nonatomic)BOOL allowsSelectionDuringEditing;                                // default is NO. Controls whether rows can be selected when in editing mode

@property (nonatomic)BOOL allowsMultipleSelectionNS_AVAILABLE_IOS(5_0);               // default is NO. Controls whether multiple rows can be selected simultaneously

@property (nonatomic)BOOL allowsMultipleSelectionDuringEditingNS_AVAILABLE_IOS(5_0);  // default is NO. Controls whether multiple rows can be selected simultaneously in editing mode


// Selection


@property (nonatomic,readonly, nullable) NSIndexPath *indexPathForSelectedRow;// returns nil or index path representing section and row of selection.

@property (nonatomic,readonly, nullable) NSArray<NSIndexPath *> *indexPathsForSelectedRowsNS_AVAILABLE_IOS(5_0);// returns nil or a set of index paths representing the sections and rows of the selection.


// Selects and deselects rows. These methods will not call the delegate methods (-tableView:willSelectRowAtIndexPath: or tableView:didSelectRowAtIndexPath:), nor will it send out a notification.

- (void)selectRowAtIndexPath:(nullableNSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;

- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;


// Appearance


@property (nonatomic)NSInteger sectionIndexMinimumDisplayRowCount;                                                     // show special section index list on right when row count reaches this value. default is 0

@property (nonatomic,strong, nullable)UIColor *sectionIndexColorNS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;                  // color used for text of the section index

@property (nonatomic,strong, nullable)UIColor *sectionIndexBackgroundColorNS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;        // the background color of the section index while not being touched

@property (nonatomic,strong, nullable)UIColor *sectionIndexTrackingBackgroundColorNS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR; // the background color of the section index while it is being touched


@property (nonatomic)UITableViewCellSeparatorStyle separatorStyle__TVOS_PROHIBITED;// default is UITableViewCellSeparatorStyleSingleLine

@property (nonatomic,strong, nullable)UIColor *separatorColorUI_APPEARANCE_SELECTOR __TVOS_PROHIBITED; // default is the standard separator gray

@property (nonatomic,copy, nullable)UIVisualEffect *separatorEffectNS_AVAILABLE_IOS(8_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;// effect to apply to table separators


@property (nonatomic)BOOL cellLayoutMarginsFollowReadableWidthNS_AVAILABLE_IOS(9_0);// if cell margins are derived from the width of the readableContentGuide.


@property (nonatomic,strong, nullable) UIView *tableHeaderView;                          // accessory view for above row content. default is nil. not to be confused with section header

@property (nonatomic,strong, nullable) UIView *tableFooterView;                          // accessory view below content. default is nil. not to be confused with section footer


- (nullable__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier; // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.

- (__kindofUITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);// newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered

- (nullable__kindof UITableViewHeaderFooterView *)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0); // like dequeueReusableCellWithIdentifier:, but for headers/footers


// Beginning in iOS 6, clients can register a nib or class for each cell.

// If all reuse identifiers are registered, use the newer -dequeueReusableCellWithIdentifier:forIndexPath: to guarantee that a cell instance is returned.

// Instances returned from the new dequeue method will also be properly sized when they are returned.

- (void)registerNib:(nullableUINib *)nib forCellReuseIdentifier:(NSString *)identifierNS_AVAILABLE_IOS(5_0);

- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);


- (void)registerNib:(nullableUINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifierNS_AVAILABLE_IOS(6_0);

- (void)registerClass:(nullable Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);


// Focus


@property (nonatomic)BOOL remembersLastFocusedIndexPathNS_AVAILABLE_IOS(9_0);// defaults to NO. If YES, when focusing on a table view the last focused index path is focused automatically. If the table view has never been focused, then the preferred focused index path is used.


@end


//_______________________________________________________________________________________________________________

// this protocol represents the data model object. as such, it supplies no information about appearance (including the cells)


@protocol UITableViewDataSource<NSObject>


@required


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


// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:

// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)


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


@optional


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;             // Default is 1 if not implemented


- (nullableNSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;    // fixed font style. use custom view (UILabel) if you want something different

- (nullableNSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;


// Editing


// Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable.

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


// Moving/reordering


// Allows the reorder accessory view to optionally be shown for a particular row. By default, the reorder control will be shown only if the datasource implements -tableView:moveRowAtIndexPath:toIndexPath:

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


// Index


- (nullableNSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView__TVOS_PROHIBITED;                                                   // return list of section titles to display in section index view (e.g. "ABCD...Z#")

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index__TVOS_PROHIBITED// tell table which section corresponds to section title/index (e.g. "B",1))


// Data manipulation - insert and delete support


// After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change

// Not called for edit actions using UITableViewRowAction - the action's handler will be invoked instead

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


// Data manipulation - reorder / moving support


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


@end



// _______________________________________________________________________________________________________________

// this protocol can provide information about cells before they are displayed on screen.


@protocol UITableViewDataSourcePrefetching <NSObject>


@required


// indexPaths are ordered ascending by geometric distance from the table view

- (void)tableView:(UITableView *)tableView prefetchRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths;


@optional


// indexPaths that previously were considered as candidates for pre-fetching, but were not actually used; may be a subset of the previous call to -tableView:prefetchRowsAtIndexPaths:

- (void)tableView:(UITableView *)tableView cancelPrefetchingForRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths;


@end



//_______________________________________________________________________________________________________________


// This category provides convenience methods to make it easier to use an NSIndexPath to represent a section and row

@interface NSIndexPath (UITableView)


+ (instancetype)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;


@property (nonatomic,readonly) NSInteger section;

@property (nonatomic,readonly) NSInteger row;


@end


NS_ASSUME_NONNULL_END


我们来解析一下:

typedef NS_ENUM(NSInteger, UITableViewStyle) {

    UITableViewStylePlain,          // regular table view

    UITableViewStyleGrouped         // preferences style table view

};

这里面的Plain和Grouped是UITableView的两个样式.默认就是Plain.

*这里再介绍一下UITableView的SB约束全屏的问题.大家在约束上面的时候可以点开旁边的倒三角.然后有两个View.选择哪个数值大的.大家是不是发现那个数值大的比数值小的大22.这是因为顶上的状态条就是22.

默认的Plain是白色的(不修改背景颜色的话).如果是Grouped的话就是灰色的.

如果是Plain样式的话.Section header就会悬停.如果是Grouped就不会.

而且Plain样式的看起来就是一个只能向下滚动的ScrollView上面划了几条横线.而且横线还没画到两边.而Grouped会用一个框框把这些东西包裹起来.

typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {

    UITableViewRowAnimationFade,

    UITableViewRowAnimationRight,           // slide in from right (or out to right)

    UITableViewRowAnimationLeft,

    UITableViewRowAnimationTop,

    UITableViewRowAnimationBottom,

    UITableViewRowAnimationNone,            // available in iOS 3.0

    UITableViewRowAnimationMiddle,          // available in iOS 3.2.  attempts to keep cell centered in the space it will/did occupy

    UITableViewRowAnimationAutomatic = 100  // available in iOS 5.0.  chooses an appropriate animation style for you

};

这个里面的Automatic要讲一下.这个属性是用来自动计算行高的.大家如果开了右侧的滚动条的话.在自动计算行高的如果设置了一个不"正确"的数.滚动条就会"异常",不过滑动到最下面就好了.

header for section是每组的cell的头部的视图.foot for section就是尾部了.

还有.UITableView能有一个大的头部视图(是一个UIView).和一个尾部视图.

@property (nonatomicstrongnullableUIView *tableHeaderView;                           // accessory view for above row content. default is nil. not to be confused with section header

@property (nonatomicstrongnullableUIView *tableFooterView;                           // accessory view below content. default is nil. not to be confused with section footer

是一个UIView类型的.头部视图可以设置高度.设置其他的对其没影响.但是如果是尾部的话.除了设置高度.还能设置X(不知道苹果是怎么想的).

IndexPath.row就是行.indexPath.section就是组.

UITableView里面有两个协议.一个是DataSource.另一个是delegate.DataSource是负责显示数据的.而delegate是负责具体行为的.

使用UITableView的时候一定要注意复用的问题.不然你的UITableView一上来就加载所有的Cell.十分耗费内存

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

#warning Incomplete implementation, return the number of sections

    return 0;

}


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

#warning Incomplete implementation, return the number of rows

    return 0;

}

如果自己创建一个UITableViewController就是这样的.里面有两个重要的方法.上面一个是一共有多少组.

下面的是一共有多少行.

咱们需要在viewDidLoad里面写:

self.tableView registerClass:<#(nullable Class)#> forCellReuseIdentifier:<#(nonnull NSString *)#>


来注册一下单元格.registerClass里面写UITableViewCell与继承与UITableViewCell的类[UITableViewCell Class].后面的id写一个字符串就行@"cellid".

既然注册了单元格,那么我们就要在后面使用:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"cellid"forIndexPath:indexPath];

    return cell;

}

这个方法就是设置每一个cell具体长什么样.

如果想调整UITableViewCell的样式.就需要写一个一个继承与UITableViewCell的类.然后重写initwithStyle方法.return [super initwithstyleXXX]就行了.cell一共有四中样式.这里就不展开了


你可能感兴趣的:(UIKit)