0

I have a UITableView and I want the first cell of this UITableView to be a description of what is being showed so this will take up a few lines and then the rest of the cells are the hours of operation. I have the table view set up like this. And I am wondering how to set the first cell so that the height of it matches the amount of room need to fit the text:

- (void)viewDidLoad {
[super viewDidLoad];
array = @[@{@"days":@"sdalkfjsadklfjlak8 aslfkjsakljf sdakljfsalkdjfs dfkljsdakfjkjlfjsakkjja"},
                            @{@"days":@"Mon-Fri", @"time":@"9-10"},
                            @{@"days":@"Sat", @"time":@"10-3", @"tag":@"0"},
                            @{@"days":@"Sun", @"time":@"3-3"} ];
 }

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return diningHallTimes.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

switch (indexPath.row) {
    case 0:
        return 60;
        break;
    default:
        return 40;
        break;
}

}

So how would the best way to set it so that the text always fits in the height of the first cell?

Thanks for the help

iqueqiorio
  • 1,149
  • 2
  • 35
  • 78
  • If you want to use a cell for this, that's fine - but it seems like a section header might be more appropriate. Is that not an option? – Acey Nov 11 '14 at 01:46
  • What iOS version are you targeting? This is much easier in iOS 8 using autoLayout and automatic cell heights. http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights – Acey Nov 11 '14 at 01:48
  • @Acey well then when scrolling it would stay at the top, iOS 7 and 8 – iqueqiorio Nov 11 '14 at 01:48
  • @Acey I am not using auto layout – iqueqiorio Nov 11 '14 at 01:53
  • @Acey how can this be done without auto layout – iqueqiorio Nov 11 '14 at 02:02
  • you want to basically calculate the height of a string in a rectangle of a certain font size. there's a variety of posts on this. For example http://stackoverflow.com/questions/2698131/how-to-calculate-the-height-of-the-text-rectangle-from-an-nsstring – LanternMike Nov 11 '14 at 02:07
  • call a method in heightforrow to calculate the height of the label and then return the height of the cell as the height of the label for the first row. once check this for label height http://stackoverflow.com/questions/1054558/vertically-align-text-within-a-uilabel/1054681#1054681 – Smile Nov 11 '14 at 09:37

0 Answers0