1

首先,我是 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 部分在视图顶部停止,而 tableView 的其余部分即使在视图顶部之后也会向上移动。但是,如果我们向下滚动,即使对于第 0 节,表格视图中的任何其他组件也没有其他问题。

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

4

2 回答 2

3

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

解决方案 2: 您可以使用此调整:

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);

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

于 2016-03-29T07:24:06.823 回答
1

您可能需要更改表头的默认行为

您可以参考: Change Default Scrolling Behavior of UITableView Section Header

于 2016-03-29T07:22:51.320 回答