2013-02-03 68 views

回答

0

heightForRowAtIndexPath不用於獲取特定單元格的高度。雖然這是UITableView的數據源的方法和用於設置UITableViewCell的高度加載時:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 60.0; // Set the cell height 
} 
0

我覺得你這樣的事情後,

-(IBAction)buttonPressed:(id)sender { 

    UIButton *myButton = (UIButton *)sender; 
    NSLog(@"%0.2f", myButton.superview.frame.size.height); 

} 

從文檔:「視圖可以嵌入其他視圖,並創建複雜的視覺層次這就造成這樣的嵌入嵌入的觀點(被稱爲子視圖)和父視圖之間的父子關係(稱爲超級觀點)。「

此外,您需要將按鈕分配給上述方法。這可以在界面生成器來完成,也可以在cellForRowAtIndexPath做這將類似於此:

UIButton *heightButton = (UIButton *)[cell viewWithTag:1]; 
[heightButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside]; 
1
  • (CGFloat的)的tableView:(UITableView的*)的tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    //這裏計算你的身高 返回高度;

}

現在,只要你想找到的單元格高度

先找到indexpath

NSIndexPath *indexPath = [tableView indexPathForCell:cell] 

然後找到小區的高度是索引路徑類似下面

CGFloat cellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath]; 
0

你可以試試這個。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Calculate a height based on a cell 
    if (!self.cutomCell) 
    { 
     self.cutomCell = [self.myTableView dequeueReusableCellWithIdentifier:@"CustomCell2"]; 
    } 

    // Configure the cell 
    self.cutomCell.introductionLabel.text = self.infoArray[indexPath.row]; 


    // Layout the cell 
    [self.cutomCell layoutIfNeeded]; 


    // Get the height for the cell 
    CGFloat height = [self.cutomCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; 

    return height; 
} 
相關問題