![](/img/trans.png)
[英]How to access UITableViewController variable from within a subclass'ed UITableViewCell?
[英]How to access properties on a custom UITableViewCell from a UiTableViewController?
我正在嘗試從UitableViewController類訪問自定義UiTableViewCell(子類化UITableViewCell)上的label屬性。 例如,我有heightForRowAtIndexPath方法,我需要訪問標簽。
這是我的代碼:
- (CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// I need access to the custom UITableView Cell property here
}
有小費嗎? 還可以在uiviewcontroller中聲明出口並將其鏈接到自定義uitableviewcell上的標簽嗎?
謝謝
要從heightForRowAtIndexPath:
訪問單元格,請檢查以下內容:
如果在初始化后需要訪問單元格的標簽,則需要在自定義UITableViewCell類的標簽中添加tag
,然后可以按以下方式訪問標簽:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"cell"];
UILabel *cellLabel = (UILabel *)[cell viewWithTag: 1];
}
高度計算應使用數據模型中的數據(而不是視圖)完成。 記住,該單元是用數據模型中的數據創建的。
您不能調用[tv cellForRowAtIndexPath:indexPath]
因為這可能導致tableView:cellForRowAtIndexPath:
和此tableView:heightForRowAtIndexPath:
方法之間的無限遞歸。
您可以嘗試直接調用tableView:cellForRowAtIndexPath:
方法來獲取單元格,但這可能會有不希望的副作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.