简体   繁体   English

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

[英]UITableView scroll to specific section using UIPIckerView?

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. 我有一个具有固定数目的节的UITableView,但是每个节中的行数可以根据服务器结果而有所不同。

I would like to implement a picker wheel to "jump" to each section. 我想实现一个拨轮“跳”到每个部分。 Here are my UIPickerView delegate methods in the UITableViewController: 这是我在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];
}

The "pickerArray" which is initialized in ViewDidLoad: 在ViewDidLoad中初始化的“ pickerArray”:

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

and here's my didSelectRow method: 这是我的didSelectRow方法:

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

I noticed there's no "scrollTo* section *AtIndexPath" method, which would be helpful. 我注意到没有“ scrollTo * 部分 * AtIndexPath”方法,这会有所帮助。 Apple's docs say this about the "indexpath" parameter: 苹果公司的文档是关于“ indexpath”参数的:

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: 调用方法(在选择器中进行选择)会引发以下错误:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString section]: unrecognized selector sent to instance 0x4bdb8' *由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“-[__ NSCFConstantString节]:无法识别的选择器已发送到实例0x4bdb8”

Any idea what I should be doing? 知道我应该做什么吗?

The scrollToRowAtIndexPath method takes an NSIndexPath as the first parameter but the code is passing an NSString resulting in the exception. scrollToRowAtIndexPath方法将NSIndexPath作为第一个参数,但是代码传递了一个NSString导致异常。

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既包含节又包含行(您必须知道这一点,因为您在表格视图中填充了节)。

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. 您需要创建一个与表视图中该节的第一行相对应的NSIndexPath ,该NSIndexPath与在选择器视图中选择的row

So assuming that the row of the picker view corresponds directly to the sections in your table view: 因此,假设选择器视图的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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM