UITableView属性和方法

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

1、初始化一个UITableView

1- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style

1structCGRect {2CGPoint origin;3CGSize size;4};5typedefstructCGRect CGRect;

1typedefenum{2UITableViewStylePlain,//平铺样式3UITableViewStyleGrouped//分组样式4} UITableViewStyle;

2、配置一个TableView

1)返回这个TableView的样式(只读属性)

1@PRoperty(nonatomic,readonly) UITableViewStyle style

2)返回指定section内的Cell的行数

1- (NSInteger)numberOfRowsInSection:(NSInteger)section

当TableView在UITableViewStylePlain下section应该为0

3)返回TableView的section数量

1- (NSInteger)numberOfSections

4)设置TableView中所有cell的高度

1@property(nonatomic) CGFloat rowHeight

Apple建议我们使用代理方法tableView:heightForRowAtIndexPath:代替rowHeight方法使TableView的性能更高

5)设置TableView的分隔线的样式

1@property(nonatomic) UITableViewCellSeparatorStyle separatorStyle

1typedefenum: NSInteger {2UITableViewCellSeparatorStyleNone,//无分隔线3UITableViewCellSeparatorStyleSingleLine,//单分割线4UITableViewCellSeparatorStyleSingleLineEtched//被侵蚀的但分隔线5} UITableViewCellSeparatorStyle;

6)设置TableView的分隔线颜色

1@property(nonatomic, retain) UIColor *separatorColor

7)设置TableView的背景视图

1@property(nonatomic, readwrite, retain) UIView *backgroundView

8)设置TableView的分隔线偏移量

1@property (nonatomic) UIEdgeInsets separatorInset2//Available in iOS 7.0 and later.

Apple的例子

1tableView.separatorInset = UIEdgeInsetsMake(0,3,0,11);2//上、左、下、右

3、创建TableView的Cell

1)注册一个包含指定标示符TableView的Cell的nib对象

1- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier//两个参数不能是nil2//Available in iOS 5.0 and later.

2)注册一个类用来创建新的Cell

1- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier2//Available in iOS 6.0 and later.

3)使用指定的标示符返回可重用的Cell

1- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath2//Available in iOS 6.0 and later.

Apple重要提示:使用这个方法之前必须是使用了registerNib:forCellReuseIdentifier: 或者registerClass:forCellReuseIdentifier:方法注册了Cell

4)使用指定的标示符返回可重用的Cell

1- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier

4、访问表头和表尾的视图

1)注册一个包含表头或表尾的指定标示符表视图的nib对象

1- (void)registerNib:(UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier2//Available in iOS 6.0 and later.

2)注册一个类,用来创建新的包含表头或表尾的表视图

1- (void)registerClass:(Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier2//Available in iOS 6.0 and later.

3)返回一个指定标识符的可重用的附带表头表尾的视图

1- (id)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier2//Available in iOS 6.0 and later.

4)设置或返回该表格的表头视图

1@property(nonatomic, retain) UIView *tableHeaderView

5)设置或返回该表格的表尾视图

1@property(nonatomic, retain) UIView *tableFooterView

6)设置或返回该表格的表头高度

1@property(nonatomic) CGFloat sectionHeaderHeight

7)设置或返回该表格的表尾高度

1@property(nonatomic) CGFloat sectionFooterHeight

8)返回指定section的表头视图

1- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section2//Available in iOS 6.0 and later.

9)返回指定section的表尾视图

1- (UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section2//Available in iOS 6.0 and later.

5、访问Cell和Section

1)返回指定indexPath的Cell

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

2)返回指定Cell的IndexPath

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

3)返回指定点的IndexPath

1- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point

4)返回指定区域内的IndexPath组成的数组

1- (NSArray *)indexPathsForRowsInRect:(CGRect)rect

5)返回可见的UITableViewCell组成的数组

1- (NSArray *)visibleCells

6)返回可见表格行的IndexPath组成的数组

1- (NSArray *)indexPathsForVisibleRows

6、估算元素的高度

1)设置表格行的估算高度以改善性能

1@property (nonatomic) CGFloat estimatedRowHeight2//The default value is 0, which means there is no estimate.3//Available in iOS 7.0 and later.

2)设置Section头的估算高度以改善性能

1@property(nonatomic) CGFloat estimatedSectionHeaderHeight2//The default value is 0, which means there is no estimate.3//Available in iOS 7.0 and later.

3)设置Section尾的古都按高度以改善性能

1@property(nonatomic) CGFloat estimatedSectionFooterHeight2//The default value is 0, which means there is no estimate.3//Available in iOS 7.0 and later.

7、滚动TableView

1)滚动到指定的位置

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

1typedefenum{2UITableViewScrollPositionNone,3UITableViewScrollPositionTop,4UITableViewScrollPositionMiddle,5UITableViewScrollPositionBottom6} UITableViewScrollPosition;

To be Continue...

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航