如何从数据库sqlite向uipickerview添加值?

时间:2011-02-16 06:35:20

标签: uipickerview

是sqlite和编程的新手, 我试了一个星期而且不能向前移动一点。我见过你帮了很多人,我想我可以解决你的问题。我发了很多,但我没有得到任何回复这个 。 你可以联系我:smrafiqsmd@gmail.com

首先,我必须在同一个视图上创建两个单独的uipickerview。在该pickerview中,我必须从sqlite数据库表中插入值。 在第一个视图中,我必须从sqlite的第一个表中获取值。在第二个视图中,我必须通过与id进行比较来获取第二个表中的值。 观点必须在同一视图上。

表1:鉴定

CREATE TABLE限定(id INTEGER PRIMARY KEY,名称VARCHAR(50),描述TEXT);

插入“资格”评级(1,'P.G','毕业后');

插入“资格”评级(1,'学位','在毕业典礼');

插入“资格”值(1,'Ten + Two','college');

表2:当然

CREATE TABLE课程(Id整数主键,数字整数,name1 varchar(20),description1文本(30));

插入“课程”价值观(1,1,'MCA','计算机硕士'); 插入“课程”价值观(2,1,'MBA','商业硕士'); 插入“课程”价值观(3,2,'BSc','sscience sscience'); 插入“课程”价值观(4,2,'BCom','会计学士'); 插入“课程”价值观(5,3,'MPC','数学'); 插入“课程”价值观(5,3,'BPC','生物学');

参见第一张表格,我会得到价值P.G,Degree,Ten + 两个

当我点击P.G时,它必须显示在textfield和第二个pickerview中我必须得到唯一匹配的值 资格的“id” 当然是“数字”。

当我们选择值时,它必须显示在文本字段中 谢谢你

1 个答案:

答案 0 :(得分:0)

我已经制作了一个项目,向您展示如何做您要求的事情。

您可以从此处下载项目:

http://www.crowsoft.com.ar/downloads/twoPickerViews.zip

可以在这里找到一个非常好的iPhone中sqlite教程:

http://www.icodeblog.com/2008/08/19/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-1/

要管理PickerView,您需要一个数组来保存数据。

我已经声明了两个类(Qualification和Course)来表示你的表和两个NSMutableArray变量来保存数据。

要链接两个PickerView,您需要在视图控制器中实现UIPickerViewDataSource和UIPickerViewDelegate:

@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;
   }    
}