UITableView

目录
    1.1 不显示无用cell
    1.2 分割线位置调整
    1.3 刷新局部
    1.4 滚动到底部
    1.5 详情按钮点击
    1.6 头图放大功能
    1.7 侧滑删除
    1.8 索引
1.1 不显示无用cell

self.tableView.tableFooterView = [[UIView alloc]init];

1.2 分割线位置调整

[self.mainTableView setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 15)];
1.3 刷新局部

NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];
[self.mainTableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
1.4 滚动到底部

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:3];
[self.mainTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
1.5 详情按钮点击

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

    NSLog(@"%ld",(long)indexPath.row);

}
1.6 头图放大功能

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width //屏幕宽度

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height //屏幕高度

#define IMAGE_HEIGHT 200 //屏幕高度


@interface ViewController ()< UITableViewDataSource,UITableViewDelegate >

@property (weak, nonatomic) IBOutlet UITableView *mainTableView;

@property (nonatomic , strong)UIImageView *headerImageView;

@property (nonatomic , strong)UIView *headerView;

@end

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 
    SCREEN_WIDTH, IMAGE_HEIGHT)];
    
    UIImage *image = [UIImage imageNamed:@"1"];
    
    self.headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake
   (0, 0, SCREEN_WIDTH, IMAGE_HEIGHT)];
    
    self.headerImageView.image = image;
    
    [self.headerView addSubview: self.headerImageView];
    
    self.mainTableView.tableHeaderView = self.headerView;
    
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat yOffset = self.mainTableView.contentOffset.y;
    
    //向上偏移量变正 向下偏移量变负
    
    if (yOffset <= 0) {
        
        CGFloat factor = ABS(yOffset)+ IMAGE_HEIGHT;
        
        CGRect f = CGRectMake(-([[UIScreen mainScreen] bounds].size.width
        *factor/(IMAGE_HEIGHT)-[[UIScreen mainScreen] bounds].size.width)/2,
        -ABS(yOffset), [[UIScreen mainScreen] bounds].size.width*factor/
        (IMAGE_HEIGHT), factor);
        
        self.headerImageView.frame = f;
        
    }else {
        
        CGRect f = _headerView.frame;
        
        f.origin.y = 0;
        
        _headerView.frame = f;
        
        self.headerImageView.frame = CGRectMake(0, f.origin.y, 
        [[UIScreen mainScreen] bounds].size. width, 200);
        
    }
}

1.7.1 侧滑删除

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView 
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
    //return UITableViewCellEditingStyleInsert;
    
}


- (void)tableView:(UITableView *)tableView 
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
 forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if(editingStyle == UITableViewCellEditingStyleDelete)
        
        
    {
        
        [self readButtonClicked:indexPath];
        
    }
    
}

// 设置返回的字段名字
- (NSString *)tableView:(UITableView *)tableView 
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
 {    
    return @"标为已读";
    
}


1.7.2 自定义侧滑删除

- (NSArray *)tableView:(UITableView *)tableView 
editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewRowAction *deleteAction=[UITableViewRowAction 
rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删
除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        
        NSLog(@"删除!");    
    }];
    
    deleteAction.backgroundColor=[UIColor redColor];
    
    UITableViewRowAction *otherAction=[UITableViewRowAction 
rowActionWithStyle:UITableViewRowActionStyleDefault title:@"其他"
handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        
        NSLog(@"其他");
        
    }];
    
    otherAction.backgroundColor=[UIColor blueColor];
    
    return @[deleteAction,otherAction];
    
}

1.8 索引

//索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    
    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
    
    for (int i=0; i < self.firstNumArray.count; i++) {
        
        [arr addObject:self.firstNumArray[i][0]];
        
    }
    
}

//索引Index
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
    
    return index;
    
}

你可能感兴趣的:(UITableView)