iOS動態改變TableView Cell高度

2021-08-27 22:05:53 字數 1386 閱讀 7156

我們知道tableview的heightforrowatindexpath

會在 cellforrowatindexpath

方法之前執行,因此在計算cell高度的時候就不能通過**的cell來計算,這樣就導致動態計算高度變得有點困難。今天在網上找到下面的一種方法:

建立**的cell

#pragma mark **-建立cell

-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath

nsinteger section = indexpath.section;

nsinteger width = [tool loadscreensize].width;

nsinteger height =100+50*section-10;

uiview* view = [[uiview alloc]initwithframe:cgrectmake(0+1, 0+1, width-2, height)];

view.backgroundcolor=[uicolor graycolor];

uibutton* btn = [tool createbutton:cgrectmake(5, 5, 100, 40) withdelegate:self withaction:nil withtitle:@"button" withbgcolor:[uicolor yellowcolor]];

[view addsubview:btn];

[cell.contentview addsubview:view];

[cell setselectionstyle:uitableviewcellselectionstylenone];

[[heights_ objectatindex:indexpath.section] setobject:[nsstring stringwithformat:@"%f",view.frame.size.height] atindex:indexpath.row];

return cell;

}

**高度設定,在方法heightforrowatindexpath

重寫中先呼叫下方法:uitableviewcell *cell = [self tableview:tableview cellforrowatindexpath:indexpath];

先把**的cell手動生成出來,然後獲取其高度。

#pragma mark **-配置cell高度

-(cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath

iOS開發 建立乙個簡單的TableView

1,建立工程 在workspace中新增乙個工程 table 建立完成後,設定此專案為當前除錯 執行工程 product scheme 2,在viewcontroller中新增tableview 開啟main.storyboard,在右下角的控制項視窗中拖動table view到viewcontro...

Ext labelfield 動態改變

在ext form 中的 field 中的fieldlabel,只提供了初始化賦值,並沒有提供動態改變其值的方法,經過大量的實驗,終於找出了乙個比較簡單的解決方案,1.首先,需要動態賦值的field需要有個id 如果沒有手動指定,則ext會自動生成乙個,可以獲取到 2.ext.domquery.se...

iOS 如何改變section header

希望這個從uitableviewdelegate協議裡得到的方法可以對你有所幫助 uiview tableview uitableview tableview viewforheaderinsection nsinteger section 使用任何你喜歡uicolor代替 uicolor redc...