iOS TableVIew获取屏幕中间的cell

释放双眼,带上耳机,听听看~!

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文章

iOS iPhoneXR和全面屏iPad判断

2019-12-23 14:26:24

iOS文章

解决.pcm No such file or directory报错

2019-12-23 15:35:23

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索