2012-10-29 171 views
0

我想從UitableViewController類訪問自定義UiTableViewCell(子類UITableViewCell)上的標籤屬性。例如我有heightForRowAtIndexPath方法,我需要訪問標籤。如何從UiTableViewController訪問自定義UITableViewCell上的屬性?

這裏是我的代碼:

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

    // I need access to the custom UITableView Cell property here 

} 

任何提示嗎?此外,它甚至可以在uiviewcontroller中聲明插座並將其鏈接到自定義uitableviewcell上的標籤?

感謝

回答

1

對於來自heightForRowAtIndexPath:接入小區,檢查以下內容:


如果您需要訪問電池的標籤,它被再初始化後,你需要添加tag在自定義的UITableViewCell類的標籤,您可以訪問標籤如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"cell"]; 
    UILabel *cellLabel = (UILabel *)[cell viewWithTag: 1]; 
} 
+0

但是,這是怎麼給你在'tableView:heightForRowAtIndexPath:'方法中的單元格?這將需要從單元訪問標籤。 – rmaddy

+0

我已經更新了我對一些可能有助於從heightForRowAtIndexPath訪問的鏈接的答案:method – user427969

0

高度計算應與你的數據模型,不是視圖的數據來完成。請記住,該單元格是使用數據模型中的數據創建的。

您不能致電[tv cellForRowAtIndexPath:indexPath],因爲這會導致tableView:cellForRowAtIndexPath:和此tableView:heightForRowAtIndexPath:方法之間的無限遞歸。

您可以嘗試直接調用tableView:cellForRowAtIndexPath:方法來獲取細胞,但可能會產生不良副作用。