您的位置:首页 > 产品设计 > UI/UE

如何获取UITableView中cell的frame值

2014-12-24 21:28 375 查看
如何获取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


关键的地方:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: