ios - 当表格 View 向上滚动时,表格 View 第一部分(第 0 部分)没有向上移动

标签 ios objective-c iphone uitableview uitableviewsectionheader

首先,我是 iOS 的新开发人员,它在我的第一个项目中,并且出现了第一个问题。我的问题解释如下。

我有一个包含两个部分的 tableView。问题是,当我向上滚动 tableView 时,除了第一节标题之外的整个 tableView 向上移动。但是向下滚动时没有问题。

我的代码是:

//----------------Table view delegate method numberOfSectionsInTableView.

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

//----------------Table view delegate method numberOfRowsInSection.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   if (section==0)
   {
       return [tableSection1 count];
   }
   else
   {
       return [tableSection2 count];
   }
}

//----------------Table view delegate method titleForHeaderInSection.

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
   if(section==0)
   {
       return @"Mandatory Details";
   }
   else
   {
       return @"Optional Details";
   }
}

//----------------Table view delegate method cellForRowAtIndexPath.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *simpleTableIdentifier = @"SimpleTableItem";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: simpleTableIdentifier];
   cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];

   if(indexPath.section==0)
   {
       // Set the data for this cell:

       cell.textLabel.text = [tableSection1 objectAtIndex:indexPath.row];
       cell.detailTextLabel.text = [tableSection1Subtitle objectAtIndex:indexPath.row];
   }
   if(indexPath.section==1)
   {
       // Set the data for this cell:

       cell.textLabel.text = [tableSection2 objectAtIndex:indexPath.row];
       cell.detailTextLabel.text = [tableSection2Subtitle objectAtIndex:indexPath.row];
   }
   return cell;
}

//---------------Table view delegate method didSelectRowAtIndexPath.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.section==0)
    {
        if(indexPath.row==0)
        {
            //....my code here....
        }
        if(indexPath.row==1)
        {
            //....my code here....
        }
        if(indexPath.row==2)
        {
            //....my code here....
        }
        if(indexPath.row==3)
        {
            //....my code here....
        }
    }
    if(indexPath.section==1)
    {
        if(indexPath.row==1)
        {
            //....my code here....
        }
        if(indexPath.row==2)
        {
            //....my code here....
        }
        if(indexPath.row==3)
        {
            //....my code here....
        }
    }
}

所以从我的代码来看,第 0 节标题,即“强制详细信息”,并没有根据整个 tableView 的移动向上移动。第 0 节在 View 顶部停止,而 tableView 的其余部分即使在 View 顶部之后也会向上移动。但是,如果我们向下滚动,即使对于第 0 部分,表格 View 中的任何其他组件也没有其他问题。

请帮我。它对每个人来说都像是一个错误。所以我被我的领导解雇了。请帮助我。

最佳答案

解决方案一:您可以将 tableview 样式更改为 UITableViewStyleGrouped (可以从界面进行)以实现您想要的。

方案二:
您可以使用此调整:

CGFloat dummyViewHeight = 40;
UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, dummyViewHeight)];
self.tableView.tableHeaderView = dummyView;
self.tableView.contentInset = UIEdgeInsetsMake(-dummyViewHeight, 0, 0, 0);

部分标题现在将像单元格一样滚动。

关于ios - 当表格 View 向上滚动时,表格 View 第一部分(第 0 部分)没有向上移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36277611/

相关文章:

iOS 启动事件指示器只有 1 行

objective-c - 弹出 ABAddressBook 模式,仅包含有电子邮件的联系人

iphone - 在 UIViewController loadView 中使用什么尺寸?

iphone - UIImageWriteToSavedPhotosAlbum 保存为具有透明度的 PNG 吗?

php - HTTPS POST 工作成功,但变量为空

ios - iPhone : Animate circle with UIKit

ios - Xcode 6.1 和 Xctool 在运行测试时失败

ios - 单词的输入警报自动大写?

ios - 根据 segue 重定向到不同的 ViewControllers

iphone - 使用 iPhone Sharekit 在一篇文章中发布多个网址