我在详细视图中的导航栏右侧创建了一个向上/向下箭头分段控制按钮。在基于表的应用程序中,如何使用这些向上/向下箭头在父表中移动单元格?
苹果“NavBar”示例代码有一个例子,但控件不起作用。
iBird程序也具有此功能,非常好用。免费下载“iBird 15”程序。
有什么想法吗?
谢谢!
答案 0 :(得分:1)
我已经解决了一些小问题。这是我目前的代码。它不是很优雅,但它的工作原理。 segmentActionPressed是.h文件中设置的NSInteger,我用它来指定段控件是否用于移动到下一个视图。
- (IBAction)segmentAction:(id)sender{
NSIndexPath *selectedRow = CurrentIndexPath; //CurrentIndexPath is an NSIndexPath object that holds the current indexpath.
NSIndexPath *scrollIndexPath;
self.segmentActionPressed = 1;
self.tableView.delegate = self;
if([sender selectedSegmentIndex] == 0)
{
[self.navigationController popViewControllerAnimated:NO];
if(selectedRow.row == 0) //If on the first row when up button is pressed, send us to the last row.
{
scrollIndexPath = [NSIndexPath indexPathForRow:tableDataSource.count -1 inSection:selectedRow.section];
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:tableDataSource.count -1 inSection:selectedRow.section] animated:NO scrollPosition:UITableViewScrollPositionMiddle];
[self tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:tableDataSource.count -1 inSection:selectedRow.section]];
}
if(selectedRow.row > 0) //If on any other row when up button is pressed, go to the next row above.
{
scrollIndexPath = [NSIndexPath indexPathForRow:selectedRow.row -1 inSection:selectedRow.section];
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:scrollIndexPath.row inSection:selectedRow.section] animated:NO scrollPosition:UITableViewScrollPositionMiddle];
[self tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:scrollIndexPath.row inSection:selectedRow.section]];
}
}
else if([sender selectedSegmentIndex] == 1)
{
[self.navigationController popViewControllerAnimated:NO];
if(selectedRow.row == tableDataSource.count -1) //If on the last row when up down is pressed, go to the first row.
{
scrollIndexPath = [NSIndexPath indexPathForRow:0 inSection:selectedRow.section];
[self.tableView selectRowAtIndexPath:scrollIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
[self tableView:self.tableView didSelectRowAtIndexPath:scrollIndexPath];
}
if(selectedRow.row < tableDataSource.count -1) //If on any other row when up down is pressed, go to the next row below.
{
scrollIndexPath = [NSIndexPath indexPathForRow:selectedRow.row +1 inSection:selectedRow.section];
[self.tableView selectRowAtIndexPath:scrollIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
[self tableView:self.tableView didSelectRowAtIndexPath:scrollIndexPath];
}
}}
在我的-didSelectRowAtIndexPath方法中,如果使用向上/向下箭头,我使用以下代码移动到下一个没有动画的视图。
if (segmentActionPressed == 0)
{
[self.navigationController pushViewController:tvc animated:YES];
}
else if (segmentActionPressed == 1)
{
[self.navigationController pushViewController:tvc animated:NO];
}
最后,我设置以下代码,在父视图出现时将segmentActionPressed设置为“0”。我还取消选择此处的行,以便您在返回父视图时可以看到选择了哪一行。
- (void)viewWillAppear:(BOOL)animated {
self.segmentActionPressed = 0;
[self.tableView deselectRowAtIndexPath:CurrentIndexPath animated:YES];}
主要问题是段按钮永远不会被按下,触摸时会立即发送动作,而不是像“触摸内部”动作一样释放按钮。
答案 1 :(得分:0)
您应该可以通过在与分段控件关联的操作中调用selectRowAtIndexPath:animated:scrollPosition来实现此目的