2017-07-27 184 views
1

我的表格視圖中有多個單元格;例如貓細胞,狗細胞,雞細胞。在貓細胞中,我有兩個視圖:圖形視圖和圖像視圖。表格單元格高度

如果圖表視圖隱藏並且圖像視圖可見,那麼我想將單元格高度設置爲200,在相反的情況下設置350。

我該怎麼做到這一點?我在表格視圖的heightForRowAt委託方法中註冊了我的筆尖文件,並且我嘗試了不同的東西而無濟於事。這些都是在我的細胞中的兩種觀點:

@property (weak, nonatomic) IBOutlet UIView *imagesView; 
@property (weak, nonatomic) IBOutlet UIView *graphView; 

...這是我heightForRowAt方法:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    TrainingImageCell *cell = (TrainingImageCell*)[self.tableView dequeueReusableCellWithIdentifier:@"TrainingImageCell"]; 
    if (cell.isGraphViewOnTop == YES) { 
     return 350; 
    } 
    else { 
     return 200; 
    } 
    return 200; 
} 
+0

爲什麼要在heightForRowAt中註冊nib文件。您正在註冊新單元,因此無法訪問退出的單元。如果你希望它在你的樣式中工作,而不是在IndexPath中爲行註冊新的單元格,你可以使用indexPath作爲TrainingImageCell * currentCell =(TrainingImageCell *)[tableView cellForRowAtIndexPath:indexPath];來訪問現有的單元格。並檢查屬性。 – luckyShubhra

+0

在heightForRowAtIndexPath中使用dequeueReusableCellWithIdentifier方法並非有效。因爲首先調用heightForRowAtIndexPath方法,然後調用cellForRowAtIndexPath方法。如果你向我展示一些cellForRowAtIndexPath方法的代碼,我可以幫助你。 –

+0

我得到了那部分@YagneshDobariya我必須使用它像在答案中提到的,但我現在有問題重新加載tableview。我已經定義了一個bool屬性isShowingGraphView,並在註冊nib後隱藏和取消隱藏cellForRowAt中的視圖。即時調用heightforRowAt這個屬性和布爾返回高度,但如果我打電話重新加載TableView整個視圖消失。 –

回答

2

而不是啓動新的細胞,你需要訪問一個什麼已經是在tableView

而不是

TrainingImageCell *cell = (TrainingImageCell*)[self.tableView dequeueReusableCellWithIdentifier:@"TrainingImageCell"]; 

TrainingImageCell *currentCell = (TrainingImageCell*)[tableView cellForRowAtIndexPath:indexPath]; 

而不是採取對物業的樣子。

編輯: 望着我們有長時間的討論,這裏是你可以在斯威夫特使用的方法非常可靠的例子。

import UIKit 

class RightAlignedCell: UITableViewCell { 
    @IBOutlet weak var titleLabel: UILabel! 
    @IBOutlet weak var subLabel: UILabel! 
    // Your boolean property you would like to map 
    var randomBool: Bool = false 
} 

class RightAlignedTableViewController: UIViewController { 
    // This is just an example for the dataSource, you should have this somewhere already 
    lazy var dataSource: [Bool] = { 
     var dataSource: [Bool] = [] 
     for index in 0..<10 { 
      dataSource.append(index % 2 == 0) 
     } 
     return dataSource 
    }() 

    @IBOutlet weak var tableView: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     tableView.delegate = self 
     tableView.dataSource = self 
    } 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 
     tableView.reloadData() 
    } 
} 

extension RightAlignedTableViewController: UITableViewDelegate {} 

extension RightAlignedTableViewController: UITableViewDataSource { 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 10 
    } 

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
     // Take a look in the !!!dataSource!!!, what is value for isHidden 
     let isHidden = self.dataSource[indexPath.row] 
     // return the right height 
     return isHidden ? 60 : 100 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell: RightAlignedCell = tableView.dequeueReusableCell(withIdentifier: "RightAlignedCell", for: indexPath) as! RightAlignedCell 
     cell.titleLabel.text = "Something" 
     cell.subLabel.text = "Nothing" 
     cell.randomBool = self.dataSource[indexPath.row] 

     return cell 
    } 
} 
+0

是的,我能夠訪問的屬性,我想要做的是 if(currentCell.graphView setHidden:YES){ return 350; } else { return 200; }但它給了我一個錯誤,ps isGraphViewOnTop只是一個布爾所以它自己不知道該怎麼做,如果設置爲是或否。我在一個函數裏面使用它。請原諒我的無知,但這是否正是我想要達到的目標? –

+0

不,這不是正確的做法。這裏所有你應該做的就是查看標誌,如果它是'true'或'false'並返回一個值。 做某處 - >'[currentCell.graphView setHidden:YES]'並重新加載tableview([tableView realodData]),所以高度將被再次詢問。 – dirtydanee

+0

我打電話([tableView realodData])在我的cellForRowAt但問題是,我的整個視圖消失,每當我取消註釋它。我也嘗試reloadRowsatindexpath與0,0,因爲這個單元格只獲得執行indexPath = 0,但應用程序崩潰,如果我這樣做。 –

0

如果您dequeueReusableCellWithIdentifier你只是得到一個新的細胞,它不會有任何數據,以檢查是否isGraphViewOnTop

我的建議將返回UITableViewAutomaticDimension和設置單元格的觀點與常量,所以當元素隱藏起來,一個單元格的尺寸會更小。

1

爲什麼要在heightForRowAt中註冊nib文件。您正在註冊新的單元,因此無法訪問現有單元。如果你想在你的風格的工作,而不是在IndexPath登記爲行高度新細胞,你可以訪問使用indexPath現有小區

TrainingImageCell *currentCell = (TrainingImageCell*)[tableView cellForRowAtIndexPath:indexPath]; 

和訪問檢查屬性。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
TrainingImageCell *currentCell = (TrainingImageCell*)[tableView cellForRowAtIndexPath:indexPath]; 
    if (cell.isGraphViewOnTop == YES) { 
    return 350; 
    } 
return 200; 
} 
+0

是的,我能夠訪問這樣的屬性,我想要做的是如果(currentCell.graphView setHidden:YES){return 350; } else {return 200; }但它給了我一個錯誤,ps isGraphViewOnTop只是一個布爾所以它自己不知道該怎麼做,如果設置爲是或否。我在一個函數裏面使用它。請原諒我的無知,但這是否正是我想要達到的目標? –

+0

你在哪裏使用它?您必須將標誌值設置爲cellForRowAtIndexPath中的isGraphViewOnTop,然後才能在此處使用它。 – luckyShubhra

+0

您可以使用適當的AutoLayout和UIAutomatic Dimension來保存行的高度。 – luckyShubhra