UITableView使用UIPIckerView滚动到特定部分?

Jos*_*nto 2 objective-c uitableview uipickerview

我有一个UITableView具有固定数量的部分,但每个部分中的行数可能会有所不同,具体取决于服务器结果.

我想实现一个拣选轮来"跳"到每个部分.这是我在UITableViewController中的UIPickerView委托方法:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

return 1;

}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return 5;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [self.pickerArray objectAtIndex:row];
}
Run Code Online (Sandbox Code Playgroud)

在ViewDidLoad中初始化的"pickerArray":

self.pickerArray = [[NSArray alloc]initWithObjects:@"Watching", @"Completed", @"On Hold", @"Dropped", @"Planned", nil];
Run Code Online (Sandbox Code Playgroud)

这是我的didSelectRow方法:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
[self.tableView scrollToRowAtIndexPath:[self.pickerArray objectAtIndex:row] atScrollPosition:UITableViewScrollPositionNone  animated:YES];
}
Run Code Online (Sandbox Code Playgroud)

我注意到没有"scrollTo*section*AtIndexPath"方法,这会有所帮助.Apple的文档说这是关于"indexpath"参数:

indexPath
An index path that identifies a row in the table view by its row index and its section index.
Run Code Online (Sandbox Code Playgroud)

调用方法(在选择器中拾取内容)会引发此错误:

*由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [__ NSCFConstantString section]:无法识别的选择器发送到实例0x4bdb8'

知道我应该做什么吗?

小智 5

scrollToRowAtIndexPath方法将a NSIndexPath作为第一个参数,但代码传递的NSString结果是异常.

正如文档所说,a NSIndexPath包括一个部分和一行(你必须知道这一点,因为你填充了一个带有部分的表视图).

您需要创建一个NSIndexPath对应于表视图中与row选择器视图中所选内容相关的部分的第一行.

因此,假设row选择器视图的对应直接对应于表视图中的部分:

//"row" below is row selected in the picker view
NSIndexPath *ip = [NSIndexPath indexPathForRow:0 inSection:row];

[self.tableView scrollToRowAtIndexPath:ip 
                      atScrollPosition:UITableViewScrollPositionNone 
                              animated:YES];
Run Code Online (Sandbox Code Playgroud)