Saiprabhakar
Saiprabhakar

Reputation: 85

How to set UITableViewCell height depending on device screen objective-c

Below is the code I had tried but it's not helpful please check.

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

Upvotes: 0

Views: 141

Answers (3)

Let's_Create
Let's_Create

Reputation: 3613

You should use heightForRowAtIndexPath delegate method of UITableView.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [UIScreen mainScreen].bounds.size.width;
}

Upvotes: 0

Faysal Ahmed
Faysal Ahmed

Reputation: 7669

In Swift

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

In Objective 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;
    }

Upvotes: 1

Taimoor Suleman
Taimoor Suleman

Reputation: 1616

its a logical error Try this.

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;

Upvotes: 0

Related Questions