I am making a tableview programmatically for a calendar app, and and need to resize my tableviewcells according to the text they hold. I am trying to use auto layout but I can't get that to work for some reason
- (void)viewDidLoad
{
......
// Set up the table view
self.tableView.tableHeaderView = imageView;
self.tableView.tableHeaderView.backgroundColor = [UIColor whiteColor];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.estimatedRowHeight = 80.0f;
self.tableView.rowHeight = UITableViewAutomaticDimension;
[self.tableView registerClass:UITableViewCell.class
forCellReuseIdentifier:@"cell"];
}
And here is my cellForRowAtIndexPath :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"
forIndexPath:indexPath];
if(cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
cell.detailTextLabel.textColor = [UIColor darkGrayColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if(indexPath.row == TableViewRowTitleAndDate)
{
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = self.event.title;
cell.detailTextLabel.numberOfLines = 0;
//Get repeating status
// Set date and time
NSString *dateString = [NSDateFormatter localizedStringFromDate:self.event.startDate
dateStyle:NSDateFormatterFullStyle
timeStyle:NSDateFormatterNoStyle];
if(self.event.allDay)
{
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@\nAll Day\nRepeats", dateString];
}
else
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"h:mm a"];
NSString *startTimeString = [formatter stringFromDate:self.event.startDate];
NSString *endTimeString = [formatter stringFromDate:self.event.endDate];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@\n%@ - %@\nRepeats", dateString, startTimeString, endTimeString];
}
}
}
But for some reason I cannot get autolayout to work. I also tried adding hightForRowAtIndexPath: method and returning UITableViewAutomaticDimension, but still no luck.
Also it might be helpful knowing I'm trying to set an image as the tableViewHeaderView which is downloaded using SDWebImage.
Here's the ScreenShot of the app : http://manikkalra.com/screen.png http://manikkalra.com/screen2.png
Any help would be greatly appreciated!