一、屬性
dataSource ---設置UITableViewDataSource的代理
delegate ---設置UItableViewDelegate的代理
/*當header、footer、cell的高度是固定值的話,用該方法直接設置表的高度,無需調用表的delegate方法設置它們的高度 eg:_table.rowHeight = 60 */
sectionHeaderHeight、sectionFooterHeight、rowHeight -- 設置表頭、尾、cell的高度sectionIndexColor -- 設置sectionIndexTitle(表索引子母)的顏色
@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 *sectionIndexColor NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR; // color used for text of the section index
@property (nonatomic, strong, nullable) UIColor *sectionIndexBackgroundColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR; // the background color of the section index while not being touched
@property (nonatomic, strong, nullable) UIColor *sectionIndexTrackingBackgroundColor NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR; // the background color of the section index while it is being touched
估算元素的高度 // NS_AVAILABLE_IOS(7_0)
/*
連接文章 介紹了estimatedRowHeight的用的場景。(適用動態的cell.height的適配)
*/
理解iOS 8中的Self Sizing Cells和Dynamic Type
estimatedRowHeight --- 設置表格行的估算高度以改善性能
estimatedSectionHeaderHeight、estimatedSectionFooterHeight ----- 設置Section頭和Section尾估算高度以改善性能
- (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);
separatorEffect -------- 表的分割線(毛玻璃效果)//默認的分割線的色調暗
效果圖:
/*
UIImageView *backImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"4.jpg"]];
self.myTableView.backgroundView = backImageView;
UIBlurEffect *blureffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVibrancyEffect *vinb = [UIVibrancyEffect effectForBlurEffect:blureffect];
self.myTableView.separatorEffect = vinb;
//cell.backgroundColor = [UIColor clearColor];
*/
二、方法
UITableViewHeaderFooterView的使用說明
- (nullable UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (nullable UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
static NSString *HeaderIdentifier = @"header";
//表頭、尾UITableViewHeaderFooterView的重用機制
//CustomHeaderView *myHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
if(!myHeader) {
// [tableView registerClass:[CustomHeaderView class] forHeaderFooterViewReuseIdentifier:HeaderIdentifier];
myHeader = [[[NSBundle mainBundle] loadNibNamed:@"CustomHeaderView"
owner:self
options:nil] objectAtIndex:0];
}
[myHeader.btnSomething setTitle:@"-" forState:UIControlStateNormal];
[myHeader.lblSomething setText:[NSString stringWithFormat:@"Section: %d",section]];
return myHeader;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomHeaderView *theHeaderView = (CustomHeaderView*)[tableView <span style="color:#FF0000;">headerViewForSection</span>:indexPath.section];
NSLog(@"%@",theHeaderView);
[theHeaderView.lblSomething setAlpha:theHeaderView.lblSomething.alpha-0.1];
[theHeaderView.btnSomething setTitle:@"+" forState:UIControlStateNormal];
}
重用機制(2個)
/**< 使用這個方法之前必須是使用了registerNib:forCellReuseIdentifier:<span>或者</span>registerClass:forCellReuseIdentifier:方法註冊了Cell
*/
dequeueReusableHeaderFooterViewWithIdentifier -------獲取重用隊列裏的UITableViewHeaderFooterView的單元格- (void)registerNib:(nullable UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0);
- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
- (void)registerNib:(nullable UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
- (void)registerClass:(nullable Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
/*
beginUpdates and endUpdates兩個方法,是配合起來使用的,標記了一個tableView的動畫塊。分別代表動畫的開始開始和結束。兩者成對出現,可以嵌套使用。
一般,在添加,刪除,選擇 tableView中使用,並實現動畫效果。在動畫塊內,不建議使用reloadData方法,如果使用,會影響動畫。
*/
beginUpdates--------只添加或刪除纔會更新行數
tableView:indentationLevelForRowAtIndexPath ------- 返回行層次的深度
效果圖:
/*
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [indexPath row];
}
*/
//NS_AVAILABLE_IOS(8_0)
tableView:editActionsForRowAtIndexPath ----- 自定義左滑動的編輯功能樣式(UITableViewRowAction)
tableView:titleForDeleteConfirmationButtonForRowAtIndexPath ------ 改變默認左滑動出現的字樣 eg:改變默認默認 Delete 字爲 下載 (return @“下載”;)
效果圖:
/*
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{ return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{ return UITableViewCellEditingStyleDelete;
}
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewRowAction *layTopRowAction1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了刪除");
[tableView setEditing:NO animated:YES];
}];
layTopRowAction1.backgroundColor = [UIColor redColor];
UITableViewRowAction *layTopRowAction2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"置頂" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了置頂");
[tableView setEditing:NO animated:YES];
}];
layTopRowAction2.backgroundColor = [UIColor greenColor];
UITableViewRowAction *layTopRowAction3 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"更多" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了更多");
[tableView setEditing:NO animated:YES];
}];
layTopRowAction3.backgroundColor = [UIColor blueColor];
NSArray *arr = @[layTopRowAction1,layTopRowAction2,layTopRowAction3];
return arr;
}
*/
在iOS8以上tableView:willDisplayFooterView:forSection:,和tableView:willDisplayHeaderView:forSection:可以正常被調用
在iOS7(iOS6沒測試)上卻沒有被調用
原來iOS7必須同時實現了Header和Footer這個delegate纔會被調用所以
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 0.f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [UIView new];
view.backgroundColor = [UIColor clearColor];
return view;
}
tableView: willDisplayHeaderView: forSection: -------- 設置當前section頭的樣式/*
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.transform = CGAffineTransformMakeTranslation(320, 0);
[UIView animateWithDuration:indexPath.row*0.2 animations:^{
cell.transform = CGAffineTransformIdentity;
}];
}
*/
/*
UIImage *image= [ UIImage imageNamed:@"man" ];
UIButton *button = [ UIButton buttonWithType:UIButtonTypeCustom ];
CGRect frame = CGRectMake( 0.0 , 0.0 , image.size.width , image.size.height );
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal ];
button.backgroundColor = [UIColor clearColor ];
[button addTarget:self
action:@selector(accessoryButtonIsTapped:event:) forControlEvents:UIControlEventTouchUpInside];
<pre name="code" class="objc">- (void)accessoryButtonIsTapped:(id)sender event:(id)event{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
if(indexPath != nil)
{
[self tableView:self.tableView <span style="color:#FF0000;">accessoryButtonTappedForRowWithIndexPath</span>:indexPath];
}
}
NS_AVAILABLE_IOS(9_0)
remembersLastFocusedIndexPath -------- 使用Apple TV遙控器控制屏幕上的用戶界面
- (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);
cellLayoutMarginsFollowReadableWidth ------- 判斷是否需要根據內容留有空白(Ipad)