Skip to main content

UITable view cell dynamic height - ios7 and above

To set the dynamic height of custom table view cell without creating IBOutlet of height constraint, follow the below code: 

    __weak IBOutlet UITableView *tblView;
@property (nonatomic, strong) DataViewCustomCell *prototypeCell;


#pragma mark - PrototypeCell
- (DataViewCustomCell *)prototypeCell
    if (!_prototypeCell) {
            _prototypeCell = [[[NSBundle mainBundle]loadNibNamed:@"DataViewCustomCell" owner:self options:nil] lastObject];
    return _prototypeCell;

#pragma mark - Configure
//Here I put the dummy static string on lblDetailLabel. You can put here the dynamic string

- (void)configureCell:(DataViewCustomCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    if (indexPath.row==0)
        cell.lblTitle.text = @"Mobile Insights - Entertainment Report";
        cell.unitedStatsTextLbl.text = @"United States, Q2, 2014";
        cell.lblDetailLabel.text = @"The entertainment industry is a diverse industry covering a wide range of sectors like TV, home entertainment, movies, live events & concerts and digital goods companies. In the US, the audience for entertainment content is large and growing at a rapid rate, thanks to Smartphones and Tablets, which provide anytime -anywhere access to a variety of entertainment content. Smartphones advertising has a significant impact on the way that consumers interact with various forms of entertainment.";
    else  if (indexPath.row==1)
        cell.lblTitle.text = @"Mobile Insights - Entertainment Report";
        cell.unitedStatsTextLbl.text = @"United States, Q1, 2014";
        cell.lblDetailLabel.text = @"Focusing on African Americans and Hispanics, the report takes an in-depth look at the mobile advertising ecosystem, including data related to mobile advertising trends, habits, spending power and more. It shows the importance of multicultural marketing for mobile advertisers, with African Ameri cans and Hispanics showing tremendous growth in population and spending power. The report also emphasizes the significance of accounting for these cultural groups’ unique attitudes and beliefs when implementing mobile video and rich media campaigns.";
    else  if (indexPath.row==2)
        cell.lblTitle.text = @"Mobile Insights - Automotive Industry Report";
        cell.unitedStatsTextLbl.text = @"United States, Q3, 2013";
        cell.lblDetailLabel.text = @"The automotive industry is now the third largest vertical in terms of mobile ad spend on the Vdopia network. This report provides an in-depth analysis of how automotive advertisers have embraced mobile video advertising to target consumers. Focusing on the US market, the report takes a detailed look at a broad spectrum of areas related to auto mobile advertising including demographics (age, gender, ethnicity), auto campaign objectives, type of vehicle advertised, targeting (geo, demo, behavioral) and advertiser type.";

#pragma mark - TableView datasource and delegate methods

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        DataViewCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([DataViewCustomCell class])];
        if (cell == nil)
            cell = [[[NSBundle mainBundle]loadNibNamed:@"DataViewCustomCell" owner:self options:nil] lastObject];

        [self configureCell:cell forRowAtIndexPath:indexPath];
        return cell;  

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
    return UITableViewAutomaticDimension;

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    if (indexPath.row == 2) {
        return 270;
        int currSysVer = [[[UIDevice currentDevice] systemVersion] intValue];
        if (currSysVer >= 8) {
            return UITableViewAutomaticDimension;
        [self configureCell:self.prototypeCell forRowAtIndexPath:indexPath];

        [self.prototypeCell updateConstraintsIfNeeded];
        [self.prototypeCell layoutIfNeeded];
        return [self.prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

///*** Custom table view cell Implementation class
@implementation DataViewCustomCell

- (void)layoutSubviews
    [super layoutSubviews];
    [self.contentView updateConstraintsIfNeeded];
    [self.contentView layoutIfNeeded];
    self.lblDetailLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.lblDetailLabel.frame);

Note: The label(sef.lblDetailLabel) who's height will be dynamic, set the below properties in the xib file:
Content hugging priority: Vertical: 250
Content compression resistance priority: Vertical: 751


Popular posts from this blog

Play youtube video in iOS app

Youtube video can be played in iOS app using webview. MPMoviePlayerController is not able to open youtube video automatically. Integrate the below script to show thumbnail, clicking on it the video will play in full screen.

NSString *url = @""; NSString* embedHTML = @"\     <html><head>\     <style type=\"text/css\">\     body {\     background-color: transparent;\     color: white;\     }\     </style>\     </head><body style=\"margin:0\">\     <iframe id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \     width=\"%0.0f\" height=\"%0.0f\" frameborder=\"0\" feature=player_detailpage allowfullscreen></iframe>\     </body></html>"; NSString* html = [NSStringstringWithFormat:embedHTML, url, self.view.frame.size.width, 200.0];
    [webviewloadHTMLString:html baseURL:nil];


Disabling print for Production In Swift Project

You'll need to set up a compiler flag to use the Swift preprocessor - go to the Swift Compiler - Custom Flags section of Build Settings to set up a -D DEBUG flag:

Then in your code you can define a DLog() function and only print your message if the DEBUG flag is set:
func DLog(message: String, function: String = __FUNCTION__) { #if DEBUG     println("\(function): \(message)") #endif }

Return multiple values from a function in objective C

We can return tuples in swift as follows:-

func getData()->(Int,Int,Int){//...code herereturn( hour, minute, second)}You can't do that in objective-c. Best option is using parameters by reference. Something like this.