首先,我是 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/