首页 > 解决方案 > 添加到 UITableView 时在 Storyboard 中使用 UITableViewCells 的正确方法是什么

问题描述

希望有人可以提供帮助,因为我遇到了一些障碍。我从一个 UIScrollview 开始,它包含动态高度切换的内容。在 AutoLayout 出现一些问题后,我转而使用 UITableView。

目前我在主 UIView 中有一个 UITableView,然后在情节提要中分离 UITableViewCells。每个 UITableViewCell 都通过一个 IBOutlet 连接。

现在切换内容我在 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;

    }

}

现在这似乎改变了 UITableView 的内容,但我在更改后立即遇到了滚动位置的奇怪问题。我正在使用以下内容尝试回到 UITableView 的顶部。

- (IBAction)showFAQScreen {

    contentToShow = 2;


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

    [self.tableViewContent reloadData];

    [self showMenu:nil];

}

如果滚动没有移动,则内容显示正确,如果稍微移动,则内容按预期滚动。但是,如果滚动条明显向下移动,滚动条会跳到底部,直到我触摸滚动条,它才会自行纠正。任何想法正在发生什么以及对另一种方法采取的建议?

谢谢

标签: iosobjective-c

解决方案


你能试一下吗

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

推荐阅读