如何获取UITableView中cell的frame值

简介:

如何获取UITableView中cell的frame值

这个可以用来处理UITableView弹出键盘的问题

本人视频教程系类   iOS中CALayer的使用

效果:

源码:

//
//  ViewController.m
//  TableViewCellFrame
//
//  Created by YouXianMing on 14/12/24.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "ViewController.h"

static NSString *YOU_XIAN_MING = @"REUSED_FLAG";

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView  *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 关闭状态栏
    [UIApplication sharedApplication].statusBarHidden = YES;

    // 创建tableView
    [self createTableView];
}

#pragma mark - TableView相关
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds
                                                  style:UITableViewStylePlain];
    self.tableView.delegate   = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:YOU_XIAN_MING];
    [self.view addSubview:self.tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell    = [tableView dequeueReusableCellWithIdentifier:YOU_XIAN_MING];
    cell.textLabel.text      = [NSString stringWithFormat:@"YouXianMing - %02ld", (long)indexPath.row];
    cell.textLabel.textColor = [UIColor grayColor];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 获取对应cell的rect值(其值针对于UITableView而言)
    CGRect rect = [self.tableView rectForRowAtIndexPath:indexPath];
    
    // 做动画效果
    NSLog(@"%@", NSStringFromCGRect(rect));
    [self.tableView setContentOffset:CGPointMake(rect.origin.x, rect.origin.y)
                            animated:YES];
}

@end

关键的地方:

目录
相关文章
UITextView根据NSString计算Size
UITextView根据NSString计算Size
42 0
|
iOS开发
iOS开发 - UITableView的tableHeaderView注意事项(遮挡cell,内容重复等等)
iOS开发 - UITableView的tableHeaderView注意事项(遮挡cell,内容重复等等)
269 0
TableView列表展开不同cell
TableView列表展开不同cell
87 0
TableView列表展开不同cell
Cell里面10个cell只想展示6条
Cell里面10个cell只想展示6条
95 0
Cell里面10个cell只想展示6条
自定义tableView的section header/footerView时的view复用问题
自定义tableView的section header/footerView时的view复用问题
323 0
|
iOS开发
iOS学习笔记--tableView中如何获取cell上textfiled的值
iOS学习笔记--tableView中如何获取cell上textfiled的值 最近在项目中遇到了这样一个问题,在tableView的cell上添加textfiled,然后获取cell上textfiled的值。
1444 0