昨天我问了一个疑问,here at this link
我得到了4个答案。这4个答案帮助我解决了我的问题。但现在在同样的情况下,我有另一个问题。当我打开第三个单元格时,它在同一个地方打开。
我想关闭那个单元格,所以我按下按钮然后成功关闭。但问题是关闭后,它会走到最高位置。
如何解决这个问题。我想我错误地调用了这个方法scrollToRowAtIndexPath
。在我之前的问题中,(参考第1行链接)我在下面描述的方法中改变了一行。
- (void)method_Expand:(UIButton*)sender
{
int_SelectedIndex = sender.tag;
[tbl_CalendarList scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:int_SelectedIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
NSLog(@"Selected Button : %ld",(long)int_SelectedIndex);
if ( int_TempSelectedIndex != int_SelectedIndex)
{
int_TempSelectedIndex = int_SelectedIndex;
}
else
{
int_TempSelectedIndex = -1;
}
[tbl_CalendarList reloadData];
}
答案 0 :(得分:1)
根据您的previous question点击自定义单元内按钮时调用method_Expand:
。尝试将代码更改为:
- (void)method_Expand:(UIButton*)sender
{
int_SelectedIndex = sender.tag;
NSLog(@"Selected Button : %ld",(long)int_SelectedIndex);
if ( int_TempSelectedIndex != int_SelectedIndex)
{
int_TempSelectedIndex = int_SelectedIndex;
[tbl_BlogList scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:int_SelectedIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
else
{
int_TempSelectedIndex = -1;
}
[tbl_CalendarList reloadData];
}
这种方式,tapped cell只有在展开时才滚动到顶部,而不是在它被折叠时滚动到顶部。
编辑:还有另一种方式
- (void)method_Expand:(UIButton*)sender
{
int_SelectedIndex = sender.tag;
NSLog(@"Selected Button : %ld",(long)int_SelectedIndex);
if ( int_TempSelectedIndex != int_SelectedIndex)
{
int_TempSelectedIndex = int_SelectedIndex;
}
else
{
int_TempSelectedIndex = -1;
}
[tbl_CalendarList reloadData];
if (int_TempSelectedIndex >= 0)
{
dispatch_async(dispatch_get_main_thread(), ^{
[tbl_BlogList scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:int_SelectedIndex inSection:0]
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
});
}
}
来源:this answer