UITableView 使用 UIPIckerView 滚动到特定部分? [英] UITableView scroll to specific section using UIPIckerView?

查看:47
本文介绍了UITableView 使用 UIPIckerView 滚动到特定部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableView 具有固定数量的部分,但是每个部分中的行数可能会因服务器结果而异.

I have a UITableView that has a fixed number of sections, however the number of rows in each section can vary depending on server results.

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

I would like to implement a picker wheel to "jump" to each section. Here are my UIPickerView delegate methods in the UITableViewController:

- (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];
}

在 ViewDidLoad 中初始化的pickerArray":

The "pickerArray" which is initialized in ViewDidLoad:

self.pickerArray = [[NSArray alloc]initWithObjects:@"Watching", @"Completed", @"On Hold", @"Dropped", @"Planned", nil];

这是我的 didSelectRow 方法:

and here's my didSelectRow method:

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

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

I noticed there's no "scrollTo*section*AtIndexPath" method, which would be helpful. Apple's docs say this about the "indexpath" parameter:

indexPath
An index path that identifies a row in the table view by its row index and its section index.

调用方法(在选择器中挑选东西)抛出这个错误:

Calling the method (picking something in the picker) throws this fault:

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

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString section]: unrecognized selector sent to instance 0x4bdb8'

知道我应该做什么吗?

推荐答案

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

The scrollToRowAtIndexPath method takes an NSIndexPath as the first parameter but the code is passing an NSString resulting in the exception.

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

As the docs say, an NSIndexPath includes both a section and row (you must know this since you populated a table view with sections).

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

You need to create an NSIndexPath that corresponds to the first row of the section in the table view that relates to the row selected in the picker view.

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

So assuming that the row of the picker view corresponds directly to the sections in your table view:

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

[self.tableView scrollToRowAtIndexPath:ip 
                      atScrollPosition:UITableViewScrollPositionNone 
                              animated:YES];

这篇关于UITableView 使用 UIPIckerView 滚动到特定部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆