簡體   English   中英

UITableView使用UIPIckerView滾動到特定部分嗎?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM