Daniel van der Merwe
Daniel van der Merwe

Reputation: 1940

Making custom, dynamic tableview cells

I am struggling to figure out how to get complete control over my tableview cells. I want them to look something like this:

App mockup

Right now I need to know how to properly manage my cells. Should I make a table view cells subclass? Should I be doing this all within the storyboard of the tableview? That's what I'm doing now. Also, how do I implement dynamic cell heights based on the amount of lines of text?

Thanks

Upvotes: 0

Views: 1056

Answers (3)

Mrigraj
Mrigraj

Reputation: 137

I will try to answer your question in three parts :

  1. For Dynamic cell height which is based on text content : you have a table view delegate called heightForRowAtIndexPath, you should calculate the height of the text based on its font and font size characteristics, and of course by providing the available width, for this you can use method "sizeWithFont" of NSString.

  2. For more control on the cell appearance : you should make a table view cell subclass and use it in the cellForRowAtIndexPath.

  3. Should you be doing this using storyboard : It is not necessary to do it using storyboard.

Upvotes: 0

Chandru
Chandru

Reputation: 247

U can create a custom view and use the followingin the cellForRowAtIndex

static NSString * cellIdentifier=@"MyTableView";
UITableViewCell * cell;
if(cell== nil)
{
    cell = [myTableView dequeueReusableCellWithIdentifier:cellIdentifier];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    contentCell.tag =100;
     contentCell=[[ContentOfCell alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
    [cell.contentView addSubview:contentCell];

    }
    else{
        // Reusable part. Reuse the UI controls here from existing cell
        contentCell = (ContentOfCell *)[cell.contentView viewWithTag:100];
    }
    //Assign all the data here

    contentCell.nameField.text=[arr objectAtIndex:indexPath.section];
    //same way for other fields
   }

Where contentCell is a custom view

Upvotes: 1

ZeMoon
ZeMoon

Reputation: 20274

You should subclass the UITableViewCell class and create your own custom cell using XIB. This will give you a lot of leg room for dynamism.

Refer to this tutorial for how to do so: http://www.appcoda.com/customize-table-view-cells-for-uitableview/

Upvotes: 1

Related Questions