Manik Kalra
Manik Kalra

Reputation: 41

UITableViewAutomaticDimension Not working

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!

Upvotes: 2

Views: 3767

Answers (2)

Artem Sherbachuk
Artem Sherbachuk

Reputation: 131

just call tableView.layoutSubviews()

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    ///fix bug in xcode UITableViewAutomaticDimension
    tableView.layoutSubviews()
}

Upvotes: 0

Benjamin
Benjamin

Reputation: 8248

Although this should be in my opinion automatically called by [tableView reloadData] (with the possibility to be overridden), it isn't. So here is the solution.

You must call :

[YOUR_TABLE_VIEW reloadSections:[NSIndexSet indexSetWithIndex:YOUR_SECTION_INDEX] withRowAnimation:YOUR_UITABLEVIEW_ANIMATION];

Right BEFORE (this is important)

[YOUR_TABLE_VIEW reloadData]

Options for YOUR_TABLE_VIEW_ANIMATION are : UITableViewRowAnimationBottom, UITableViewRowAnimationNone, UITableViewRowAnimationTop…

YOUR_SECTION_INDEX is an integer.

Hope this helps

Upvotes: 3

Related Questions