2014-12-03 55 views
0

我在代理方法中設置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。有人能指導我在這裏做錯了什麼嗎?

+0

在heightForRowAtIndexPath中添加日誌:並檢查它是否返回正確的高度 – Dev 2014-12-03 08:28:25

+0

是的,它返回正確的高度。 ( – 2014-12-03 08:33:30

+0

)你正在改變tableview風格在這裏。cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@「cell」]。試着不要改變 – Dev 2014-12-03 08:35:06

回答

1

什麼都沒有,你的代碼沒問題。你的細胞會有你想要的大小。問題是你的細胞不知道他們在這個方法的最終尺寸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)); 
} 
0
- (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. 
} 
2

但是,如果你仍然需要你給出的(預定)大小的電池,你可以隨時使用,


對於寬度,

有了一個自定義單元格的情況下,可以做出這樣的方法,

- (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使用,與細胞類名(自定義單元格) ,[cellClassName myCellHeight][self myCellHeight](表格的- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath數據源方法中的默認單元格UIViewController)。


你有你的細胞,這些方法精確寬度/高度只要你想! :)

相關問題