![](/img/trans.png)
[英]How to programmatically have UITableView scroll to a specific section
[英]UITableView scroll to specific section using 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];
}
在ViewDidLoad中初始化的“ pickerArray”:
self.pickerArray = [[NSArray alloc]initWithObjects:@"Watching", @"Completed", @"On Hold", @"Dropped", @"Planned", nil];
这是我的didSelectRow方法:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
[self.tableView scrollToRowAtIndexPath:[self.pickerArray objectAtIndex:row] atScrollPosition:UITableViewScrollPositionNone animated:YES];
}
我注意到没有“ scrollTo * 部分 * AtIndexPath”方法,这会有所帮助。 苹果公司的文档是关于“ indexpath”参数的:
indexPath
An index path that identifies a row in the table view by its row index and its section index.
调用方法(在选择器中进行选择)会引发以下错误:
*由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“-[__ NSCFConstantString节]:无法识别的选择器已发送到实例0x4bdb8”
知道我应该做什么吗?
scrollToRowAtIndexPath
方法将NSIndexPath
作为第一个参数,但是代码传递了一个NSString
导致异常。
如文档所述, NSIndexPath
既包含节又包含行(您必须知道这一点,因为您在表格视图中填充了节)。
您需要创建一个与表视图中该节的第一行相对应的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];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.