ORStudios
ORStudios

Reputation: 3233

What is the correct way to use UITableViewCells In Storyboard When Adding To UITableView

Hope someone can help as I've hit a bit of a roadblock. I started off with a UIScrollview that would have content with dynamic height switch in and out. After a few issues with AutoLayout I have switched to using a UITableView instead.

At the minute I have a UITableView in the main UIView and then separate UITableViewCells within storyboard. Each UITableViewCell is connected via an IBOutlet.

Now to switch the content I use the following on reloadData

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if(contentToShow == 1){

        UITableViewCell *cell = self.cellHome;

        self.viewIcon1.layer.cornerRadius = 40;
        self.viewIcon1.layer.masksToBounds = true;
        self.viewIcon2.layer.cornerRadius = 40;
        self.viewIcon2.layer.masksToBounds = true;
        self.viewIcon3.layer.cornerRadius = 40;
        self.viewIcon3.layer.masksToBounds = true;
        self.viewIcon4.layer.cornerRadius = 40;
        self.viewIcon4.layer.masksToBounds = true;


        [self addUnderline:self.imageViewTitle];
        [self addUnderline:self.imageViewTitle2];
        [self addUnderline:self.imageViewTitle3];

        return cell;

    }else {

        UITableViewCell *cell = self.cellCustInfo;

        [self addUnderline:self.imageViewTitle4];

        return cell;

    }

}

Now this seems to change the content of the UITableView but I am having strange issues with the position of the scroll immediately after the change. I am using the following to try and get back to the top of the UITableView.

- (IBAction)showFAQScreen {

    contentToShow = 2;


    [UIView animateWithDuration:0.4 animations:^{
        self.tableViewContent.contentOffset = CGPointZero;}];

    [self.tableViewContent reloadData];

    [self showMenu:nil];

}

If the scroll hasn't moved then the content is displayed correct, if it has slightly moved then the content scrolls as expected. However if the scroll has moved down significantly the scroll jumps to the bottom and it is not until I touch the scroll that it corrects itself. Any ideas what is going on and suggestions on another approach to take?

Thanks

Upvotes: 0

Views: 43

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

Can you try

UIView animateWithDuration:duration  animations:^{
  self.tableViewContent.contentOffset = CGPointZero;
} completion:^(BOOL finished) {
     [self.tableViewContent reloadData]; 
     [self showMenu:nil];
}];

Upvotes: 1

Related Questions