2011-02-16 101 views
0

我是新來的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」匹配當然, 「數字」的資格。

當我們選擇價值則必須在文本框 顯示感謝ü

回答

0

我做了一個項目,向您展示如何做你所要求的。

你可以從這裏下載該項目:

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

一個很好的教程在iPhone源碼可以在這裏找到:

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

要管理一個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; 
    }  
} 
相關問題