我在代理方法中設置UITableViewCell
的寬度和高度,但當我檢查cellForRowAtIndexPath
方法中的單元框架時,它的完全不同。以下是我的代碼。我正在以編程方式處理所有事情。UITableViewCell框架不是根據給定的寬度和高度
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
。有人能指導我在這裏做錯了什麼嗎?
在heightForRowAtIndexPath中添加日誌:並檢查它是否返回正確的高度 – Dev 2014-12-03 08:28:25
是的,它返回正確的高度。 ( – 2014-12-03 08:33:30
)你正在改變tableview風格在這裏。cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@「cell」]。試着不要改變 – Dev 2014-12-03 08:35:06