我在委托方法中设置UITableViewCell的宽度和高度,但是当我在cellForRowAtIndexPath方法中检查单元格帧时,它完全不同。下面是我的密码。我每件事都是按程序来做的。
self.myTable = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 0.05,self.view.frame.size.height * 0.08,self.view.frame.size.width * 0.90,
(self.view.frame.size.height * 0.90) - tabBarHeight ) style:UITableViewStylePlain];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1.0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row == 0 || indexPath.row == 4){
return self.myTable.frame.size.height * 0.08;
}
return (self.myTable.frame.size.height * 0.28);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if(!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
if(indexPath.row != 4){
cell.userInteractionEnabled = NO;
}
switch (indexPath.row) {
case 0:{
NSLog(@"0th cell Frame : %@", NSStringFromCGRect(cell.frame));
break;
}
case 1:{
NSLog(@"1st cell Frame : %@", NSStringFromCGRect(cell.frame));
break;
}
case 2:{
NSLog(@"2nd cell Frame : %@", NSStringFromCGRect(cell.frame));
break;
}
case 3:{
NSLog(@"3rd cell Frame : %@", NSStringFromCGRect(cell.frame));
break;
}
case 4:{
NSLog(@"4rth cell Frame : %@", NSStringFromCGRect(cell.frame));
break;
}
default:
break;
}
return cell;
}我得到的单元格帧的输出是{{0, 0}, {320, 44}}。但这个细胞框架不正确。高度应该是129,宽度应该类似于288。有人能指点我这里做错了什么吗?
发布于 2014-12-03 08:33:03
没什么,你的代码没问题。你的细胞会有你想要的大小。问题是您的单元格不知道它们在这个方法中的最终大小,cellForRowAtIndexPath。在这种方法中,他们认为,它们具有默认大小。但在它们出现之前,它们将被调整大小。如果通过代码定义单元格,则需要使用所期望的de维度,这是方法的tableView宽度和高度(heightForCell或estimateHeightForCell)。您可以通过注释这一行并按下委托:(也可以看到)来检查这一点。行以进行评论以检查:
....
if(indexPath.row != 4){
// cell.userInteractionEnabled = NO;
}
.....执行检查:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"cell pressed Frame : %@", NSStringFromCGRect(cell.bounds));
}发布于 2014-12-03 11:35:58
但是,如果您仍然需要单元格的给定(预定义)大小,则始终可以使用
表示宽度,
使用自定义单元格的,您可以创建这样的方法,
- (CGFloat)cellWidth {
return [UIScreen mainScreen].bounds.size.width;
}如果没有自定义单元格,则始终可以在创建
UITableView**,**的同一个类中创建一个函数。
- (CGFloat)cellWidth {
return yourTableView.frame.size.width;
}它将返回您的表格单元格的确切大小。(我总是喜欢第二种方法,因为有时候你只是不创建设备宽度单元格!)
表示高度,
使用自定义单元格的,您可以创建这样的类方法,
+(CGFloat)myCellHeight {
return 45.f;
}如果没有自定义单元格,则始终可以在创建
UITableView**,**的同一个类中创建一个函数。
-(CGFloat)myCellHeight {
return 45.f;
}它可以在UIViewController中使用,在表的- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath数据源方法中具有单元格类名(用于自定义单元格)、[cellClassName myCellHeight]或[self myCellHeight] (UIViewController中的默认单元格)。
这些方法可以让你的细胞在你想要的时候得到精确的width/height,!:)
发布于 2014-12-03 11:09:01
- (void)viewDidLoad
{
self.myTable = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 0.05,self.view.frame.size.height * 0.08,self.view.frame.size.width * 0.90, // set the frame for your tableview
(self.view.frame.size.height * 0.90) - tabBarHeight ) style:UITableViewStylePlain];
self.myTable.delegate = self;
self.myTable.dataSource = self;
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}https://stackoverflow.com/questions/27266556
复制相似问题