一般來說 在iOS 中若UITableViewCell 固定行高, 會(huì)通過
self.tableView.rowHeight = 44;
這樣來設(shè)置.
- 若不是固定行高, 可能出現(xiàn)多種高度, 可以通過tableView的代理方法實(shí)現(xiàn),在下面方法中實(shí)現(xiàn):
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ( ...條件1...) {
return 20.f;
}
if ( ...條件2...) {
return 40.f;
}
return 44.f;
}
- 如果需要由系統(tǒng)自動(dòng)估算行高, 可以通過設(shè)置以下代碼實(shí)現(xiàn):
self.tableView.estimatedRowHeight = 55;
self.tableView.rowHeight = UITableViewAutomaticDimension;
問題: 一般來說, 當(dāng)用戶實(shí)現(xiàn)了heightForRow的代理方法, 系統(tǒng)會(huì)跟據(jù)代理方法的返回值設(shè)置行高, 如果沒有實(shí)現(xiàn)代理方法, 系統(tǒng)會(huì)根據(jù)self.tableView.rowHeight的值設(shè)置行高; 那么如果我們既想讓系統(tǒng)自動(dòng)估算行高, 又想指定滿足一定條件下的行高, 我們?cè)撛趺崔k呢? 我們可以通過下面的方法實(shí)現(xiàn):
- 設(shè)置估算行高:
self.tableView.estimatedRowHeight = 55;
self.tableView.rowHeight = UITableViewAutomaticDimension;
- 通過代理方法指定滿足一定條件的行高:(重點(diǎn): 代理方法的最后返回值一定要是UITableViewAutomaticDimension), 這樣系統(tǒng)才知道這一行需要估算,代碼如下:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ( ...條件1...) {
return 20.f;
}
if ( ...條件2...) {
return 40.f;
}
return UITableViewAutomaticDimension;
}
以上方法實(shí)際可用, 供參考.