【问题标题】:UITableView scroll to specific section using UIPIckerView?UITableView 使用 UIPIckerView 滚动到特定部分?
【发布时间】:2013-12-11 01:20:32
【问题描述】:

我有一个 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*section*AtIndexPath”方法,这会很有帮助。 Apple 的文档对“indexpath”参数这么说:

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

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

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

知道我应该做什么吗?

【问题讨论】:

    标签: objective-c uitableview uipickerview


    【解决方案1】:

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

    【讨论】:

    • 感谢您的帮助。它工作得很好。我需要更仔细地阅读。
    猜你喜欢
    • 1970-01-01
    • 2014-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    相关资源
    最近更新 更多