怎么用pickview实现三个component关联的效果
uipickerviewdatasource协议里面有个方法.numberofcomponentsinpickerview,这个方法返回uipickerview需要多少个组件。 其相关性实现如下:@implementation fkviewcontrollernsdictionary* books;nsarray* authors;// selectedauthor保存当前选中的作者nsstring*selectedauthor;- (void)viewdidload{[super viewdidload];// 创建并初始化nsdictionary对象books = [nsdictionary dictionarywithobjectsandkeys: [nsarrayarraywithobjects:@\\\\\\\\\\\\\\\"飞鸟集\\\\\\\\\\\\\\\", @\\\\\\\\\\\\\\\"吉檀迦利\\\\\\\\\\\\\\\", nil] , @\\\\\\\\\\\\\\\"泰戈尔\\\\\\\\\\\\\\\", [nsarrayarraywithobjects:@\\\\\\\\\\\\\\\"醒世恒言\\\\\\\\\\\\\\\",@\\\\\\\\\\\\\\\"喻世明言\\\\\\\\\\\\\\\" , @\\\\\\\\\\\\\\\"警世通言\\\\\\\\\\\\\\\", nil] , @\\\\\\\\\\\\\\\"冯梦龙\\\\\\\\\\\\\\\", [nsarrayarraywithobjects:@\\\\\\\\\\\\\\\"疯狂android讲义\\\\\\\\\\\\\\\", @\\\\\\\\\\\\\\\"疯狂ios讲义\\\\\\\\\\\\\\\", @\\\\\\\\\\\\\\\"疯狂ajax讲义\\\\\\\\\\\\\\\" , @\\\\\\\\\\\\\\\"疯狂xml讲义\\\\\\\\\\\\\\\", nil] , @\\\\\\\\\\\\\\\"李刚\\\\\\\\\\\\\\\" ,nil];// 使用authors保存books所有key组成的nsarray排序后的结果authors = [[books allkeys] sortedarrayusingselector:@selector(compare:)];// 设置默认选中的作者authors中的第一个元素selectedauthor = [authors objectatindex:0];self.picker.datasource = self;self.picker.delegate = self;}//uipickerviewdatasource中定义的方法,该方法的返回值决定该控件包含多少列-(nsinteger)numberofcomponentsinpickerview:(uipickerview*)pickerview{return 2; // 返回2表明该控件只包含2列}//uipickerviewdatasource中定义的方法,该方法的返回值决定该控件指定列包含多少个列表项-(nsinteger)pickerview:(uipickerview *)pickerviewnumberofrowsincomponent:(nsinteger)component{// 如果是第一列,返回authors中元素的个数// 即authors包含多少个元素,第一列包含多少个列表项if (component == 0) {return authors.count;}// 如果是其他列(只有第二列),// 返回books中selectedauthor对应的nsarray中元素的个数return [[booksobjectforkey:selectedauthor] count];}// uipickerviewdelegate中定义的方法,该方法返回的nsstring将作为// uipickerview中指定列和列表项上显示的标题- (nsstring*)pickerview:(uipickerview *)pickerview titleforrow:(nsinteger)row forcomponent:(nsinteger)component{// 如果是第一列,返回authors中row索引处的元素// 即第一列的元素由authors集合元素决定if (component == 0) {return [authors objectatindex:row];}// 如果是其他列(只有第二列),// 返回books中selectedauthor对应的nsarray中row索引处的元素return [[booksobjectforkey:selectedauthor] objectatindex:row];}// 当用户选中uipickerviewdatasource中指定列和列表项时激发该方法-(void)pickerview:(uipickerview *)pickerview didselectrow:(nsinteger)row incomponent:(nsinteger)component{if(component == 0){// 改变被选中的作者selectedauthor = [authors objectatindex:row];// 控制重写加载第二个列表,根据选中的作者来加载第二个列表[self.picker reloadcomponent:1];}nsarray* tmp = component== 0 ? authors:[books objectforkey:selectedauthor];nsstring* tip = component == 0 ? @\\\\\\\\\\\\\\\"作者\\\\\\\\\\\\\\\": @\\\\\\\\\\\\\\\"图书\\\\\\\\\\\\\\\";// 使用一个uialertview来显示用户选中的列表项uialertview* alert = [[uialertview alloc]initwithtitle:@\\\\\\\\\\\\\\\"提示\\\\\\\\\\\\\\\"**:[nsstring stringwithformat:@\\\\\\\\\\\\\\\"你选中的%@是:%@,\\\\\\\\\\\\\\\", tip , [tmp objectatindex:row]]delegate:nilcancelbuttontitle:@\\\\\\\\\\\\\\\"确定\\\\\\\\\\\\\\\"otherbuttontitles:nil];[alert show];}// uipickerviewdelegate中定义的方法,该方法返回的nsstring将作为// uipickerview中指定列的宽度-(cgfloat)pickerview:(uipickerview *)pickerview widthforcomponent:(nsinteger)component{// 如果是第一列,宽度为90if (component == 0) {return 90;}return 210; // 如果是其他列(只有第二列),宽度为210}@end 20210311