如何从数据库sqlite中将值添加到uipickerview?
我是新来的sqlite和编程, 我试了这个从一个星期,并没有能够前进一点。我见过你有帮助很多,我想我可以通过你解决我的问题。我发布了很多,但是我没有得到任何回复。 U可以联系我:[email protected]如何从数据库sqlite中将值添加到uipickerview?
首先,我必须在同一视图上创建两个单独的uipickerviews。在该pickerview中,我必须从sqlite数据库表中插入值。 在第一个视图中,我必须从sqlite的第一个表中获取值。第二个视图中,我必须通过与id进行比较来从第二个表中获取值。 意见必须在相同的观点。
表1:资格
CREATE TABLE资格(ID INTEGER PRIMARY KEY,名称VARCHAR(50),描述TEXT);
INSERT INTO“qualification”VALUES(1,'P.G','Post Graduation');
INSERT INTO“qualification”VALUES(1,'Degree','Under Graduation');
INSERT INTO“qualification”VALUES(1,'Ten + Two','college');
表2:当然
CREATE TABLE场(ID整数的PrimaryKey,数整数,NAME1 VARCHAR(20),描述1文字(30));
INSERT INTO“Course”VALUES(1,1,'MCA','Master in computers'); INSERT INTO“Course”VALUES(2,1,'MBA','Master in Bussiness'); INSERT INTO“Course”VALUES(3,2,'BSc','Bachelor in sscience'); INSERT INTO“Course”VALUES(4,2,'BCom','Bachelor in accounts'); INSERT INTO“Course”VALUES(5,3,'MPC','Mathematics'); INSERT INTO“Course”VALUES(5,3,'BPC','Biology');
见第一个表,我将获得价值PG,学位,十+ 两个
当我PG单击它必须显示在文本框,并在第二pickerview我得只值与 的“ID”匹配当然, “数字”的资格。
当我们选择价值则必须在文本框 显示感谢ü
我做了一个项目,向您展示如何做你所要求的。
你可以从这里下载该项目:
http://www.crowsoft.com.ar/downloads/twoPickerViews.zip
一个很好的教程在iPhone源码可以在这里找到:
要管理一个PickerView您需要数组来保存数据。
我已经声明了两个类(Qualification和Course)来表示您的表和两个NSMutableArray变量来保存数据。
要连结你需要实现UIPickerViewDataSource和UIPickerViewDelegate在您的视图CONTROLER两个PickerViews:
@interface twoPickerViewsViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {
响应当用户选择你需要编写didSelectRow资格:
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
// And now the final part of the UIPickerView, what happens when a row is selected.
if (thePickerView == qualificationsPicker) {
if (row >= 0 && row < [m_qualifications count]) {
m_selectedQualification = [m_qualifications objectAtIndex:row];
qualificationName.text = m_selectedQualification.qualificationName;
[self getCourses];
[coursesPicker reloadAllComponents];
}
}
}
你需要编写另外三种方法,numberOfComponentsInPickerView,numberOfRowsInComponent,titleForRow:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
// This method needs to be used. It asks how many columns will be used in the UIPickerView
return 1; // We only need one column so we will return 1.
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
// This method also needs to be used. This asks how many rows the UIPickerView will have.
if (thePickerView == qualificationsPicker) {
return [m_qualifications count]; // We will need the amount of rows that we used in the pickerViewArray, so we will return the count of the array.
}
else {
return [self getCourses];
}
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
// This method asks for what the title or label of each row will be.
if (thePickerView == qualificationsPicker) {
Qualification *qualification = [m_qualifications objectAtIndex:row];
return qualification.qualificationName; // We will set a new row for every string used in the array.
}
else {
Course *course = [m_selectedCourses objectAtIndex:row];
return course.courseName;
}
}