如何根据设备屏幕的物镜设置UITableViewCell高度

时间:2018-08-08 07:24:27

标签: ios objective-c uitableview uitableviewautomaticdimension

下面是我尝试过的代码,但没有帮助,请检查。

static CGFloat cellDefaultHeight = 180;
CGFloat screenDefaultHeight = [UIScreen mainScreen].bounds.size.height;
CGFloat factor = cellDefaultHeight/screenDefaultHeight;
return factor * [UIScreen mainScreen].bounds.size.height;

3 个答案:

答案 0 :(得分:1)

Swift

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
    let screenHeight = UIScreen.main.bounds.height  
    return (cellDefaultHeight/screenDefaultHeight) * screenHeight
}

目标C

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
       CGFloat screenDefaultHeight = [UIScreen mainScreen].bounds.size.height;
        CGFloat factor = cellDefaultHeight/screenDefaultHeight;
        return factor * [UIScreen mainScreen].bounds.size.height;
    }

答案 1 :(得分:0)

您应使用heightForRowAtIndexPath的{​​{1}}委托方法。

UITableView

答案 2 :(得分:0)

存在逻辑错误 试试这个。

static CGFloat cellDefaultHeight = 180; //lets suppose its a defualt height for iphone6
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
CGFloat factor = cellDefaultHeight * (screenHeight / 667) //667 is height of iphone 6
return factor * cellDefaultHeight;