美文网首页
iOS TableVIew获取屏幕中间的cell

iOS TableVIew获取屏幕中间的cell

作者: 邓布利多教授 | 来源:发表于2019-07-30 14:29 被阅读0次

1、首先创建一个TableView

@interface MainController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tvList;

@end

@implementation MainController

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.title = @"iOS基础知识";
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.tableView];
    
}

#pragma mark - TableViewLazy
-(UITableView *)tableView{
    
    if (!_tvList) {
        _tvList = [[UITableView alloc]initWithFrame:self.view.bounds];
        _tvList.delegate = self;
        _tvList.dataSource = self;
    }
    return _tvList;
    
}

#pragma mark - TableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self dataBase].count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *ident = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ident];
    }
    
    cell.textLabel.text = [self dataBase][indexPath.row][@"title"];
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *vcName = [self dataBase][indexPath.row][@"class"];
    [self.navigationController pushViewController:[NSClassFromString(vcName) new] animated:YES];
    
}

#pragma mark - DataBase
-(NSArray *)dataBase{
    
    return @[@{@"title":@"runtime",
               @"class":@"NSArrayController"},
             @{@"title":@"呼吸灯动画",
               @"class":@"AnimationViewController"},
             @{@"title":@"自定义时间选择器",
               @"class":@"DateSelectorController"},
             @{@"title":@"Masonry+RAC+BlocksKit",
               @"class":@"MasonryRACBlocksKitController"},
             @{@"title":@"基础算法",
               @"class":@"BasicAlgorithmController"},
             @{@"title":@"PNChartController",
               @"class":@"PNChartController"},
             @{@"title":@"YYModelController",
               @"class":@"YYModelController"},
             @{@"title":@"KVCController",
               @"class":@"KVCController"},
             @{@"title":@"IQKM",
               @"class":@"IQKMController"},
             @{@"title":@"UseNSOperation",
               @"class":@"UseNSOperationController"},
             @{@"title":@"文件下载",
               @"class":@"DownLoadFileController"},
             @{@"title":@"小动画",
               @"class":@"AnimationController"},
             @{@"title":@"VideoPlayerController",
               @"class":@"VideoPlayerController"},
             @{@"title":@"runtime",
               @"class":@"NSArrayController"},
             @{@"title":@"呼吸灯动画",
               @"class":@"AnimationViewController"},
             @{@"title":@"自定义时间选择器",
               @"class":@"DateSelectorController"},
             @{@"title":@"Masonry+RAC+BlocksKit",
               @"class":@"MasonryRACBlocksKitController"},
             @{@"title":@"基础算法",
               @"class":@"BasicAlgorithmController"},
             @{@"title":@"PNChartController",
               @"class":@"PNChartController"},
             @{@"title":@"YYModelController",
               @"class":@"YYModelController"},
             @{@"title":@"KVCController",
               @"class":@"KVCController"},
             @{@"title":@"IQKM",
               @"class":@"IQKMController"},
             @{@"title":@"UseNSOperation",
               @"class":@"UseNSOperationController"},
             @{@"title":@"文件下载",
               @"class":@"DownLoadFileController"},
             @{@"title":@"小动画",
               @"class":@"AnimationController"},
             @{@"title":@"VideoPlayerController",
               @"class":@"VideoPlayerController"}];
    
}

2、添加UIScrollView的代理方法

//tableView自动停止
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self tableViewScroll:scrollView];
}

/**
 手指拖动滑行
 @param decelerate 滑动明确位移返回NO,自动滑行一段位移返回YES
 */
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (!decelerate) [self tableViewScroll:scrollView];
}

3、获取屏幕中间cell

- (void)tableViewScroll:(UIScrollView *)scrollView{
    
    /*
     获取处于屏幕中心的cell
     系统方法返回处于tableView某坐标处的cell的indexPath
    */
    
    //scrollView的Y轴偏移量+tableView高度的一半为整个scrollView内容高度的一半
    CGFloat offSetY = scrollView.contentOffset.y + CGRectGetHeight(_tvList.frame) / 2;
    NSIndexPath *indexPath = [_tvList indexPathForRowAtPoint:CGPointMake(0, offSetY)];
    UITableViewCell *cell = [_tvList cellForRowAtIndexPath:indexPath];
    
    NSLog(@"section: %ld - row: %ld - cellName = %@",indexPath.section, indexPath.row, cell.textLabel.text);
    
}

4、全剧终

相关文章

网友评论

      本文标题:iOS TableVIew获取屏幕中间的cell

      本文链接:https://www.haomeiwen.com/subject/efcnrctx.html